【发布时间】:2012-08-31 00:29:04
【问题描述】:
我正在使用 Grails 1.3.7 和休眠 1.3.7 和 MySQL 5.1。我有以下(简化的)域对象:
class Document {
String externalId
Date date
String url
String title
Map metadata = new HashMap()
def propertyMissing(String key) { return metadata[key] }
def propertyMissing(String key, String value) { metadata[key] = value }
}
当我必须加载一堆这些文档以返回给客户端时,系统最终不得不为每个文档运行单独的查询以获取关联的元数据。这需要在一台合理的机器上花费数十秒来检索数百个文档及其元数据。不用说,这对于交互式使用来说太慢了。由于我的应用程序希望将所有数据加载到浏览器中以供用户操作,因此我无法将查询划分为“页面”。
目前,我正在运行的查询如下所示:
Document.executeQuery("select distinct p.document from Posting p where p.topic = :topic", [topic: topic]);
然后,此查询会导致创建一堆 Document 实例,这需要很长时间。
Hibernate缓存配置如下:
hibernate {
cache.use_second_level_cache=true
cache.use_query_cache=true
cache.provider_class='net.sf.ehcache.hibernate.EhCacheProvider'
}
有问题的表格是:
mysql> describe document;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| version | bigint(20) | NO | | NULL | |
| date | datetime | YES | | NULL | |
| external_id | varchar(255) | NO | | NULL | |
| title | longtext | YES | | NULL | |
| url | longtext | NO | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
mysql> describe document_metadata;
+--------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------------+------+-----+---------+-------+
| metadata | bigint(20) | YES | | NULL | |
| metadata_idx | varchar(255) | YES | | NULL | |
| metadata_elt | varchar(4096) | YES | | NULL | |
+--------------+---------------+------+-----+---------+-------+
没有直接在Document 中转储元数据表和硬编码字段,我还能做些什么来提高我的代码性能?
基因
【问题讨论】: