0

我将 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我还能做些什么来提高代码的性能吗?

基因

4

2 回答 2

0

也许您可以使用本机 sql 调用优化数据检索操作

def sql = '''
  select distinct d.*, m.* 
  from 
    document d 
      join document_metadata m on m.metadata = d.id
      join post p on p.id = d.post_id
  where
    post.topic = :topic'''

def query = session.createSQLQuery(sql)
query.addEntity(com.yourdomain.Document.class)
query.setString("topic", "grails")
def documents = query.list()

显然选择是不完整的,因为我在猜测帖子方案和关系

于 2012-08-31T01:04:00.857 回答
0

您可以尝试将元数据放在关联域类中,并依赖 Grails 的默认延迟加载关联。然后,您可以更快地加载每个核心信息,Document并且只加载特定的元数据访问Document

class Document {
    // Core non-Map data

    DocumentMetadata metadata

    def propertyMissing(String key) { return metadata?.data?.get(key) }
    def propertyMissing(String key, String value) { 
        if (!metadata) { metadata = new DocumentMetadata() }
        metadata.data[key] = value
    }
}

class DocumentMetadata {
    Map data = new HashMap()
}
于 2012-08-31T03:03:04.450 回答