2014-01-22 88 views
4

我有一个表格articles 500k行。一篇文章有​​一个作者列表。我正在尝试创建一个查询以获取最新发布的作者列表文章。从子句中使用HQL子查询

我用下面的HQL查询这让我我想要的东西,但速度很慢(〜4S)

  select author, article 
      from Article article inner join article.authors author 
      where (author.id, article.publishedAt) in 
      (select author.id, max(article.publishedAt) 
      from Article article join article.authors author 
      where author.id in (authors_list)) 
      group by author.id 

在普通的SQL一个可能更好的查询将是:

   select * from (
       select articles.id, author.id 
       from articles, article_authors, authors 
       where articles.id = article_authors.article_id and 
        article_authors.author_id=authors.id  
        and author.id in (author_list) 
        order by articles.publishedAt desc 
      ) b 
       group by authors.id; 

但是从Hibernate文档声明HQL子查询只能出现在select或where子句中。 http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-subqueries

有没有一种方法来模仿这种使用HQL或其他方式来提高查询性能的查询?

+0

几个月前,我回答了一个非常类似的问题。应该帮助你。 http://stackoverflow.com/questions/32486923/how-to-increase-performance-in-sql-query/32487550#32487550 – jswan

回答

0

在任何一种情况下,您都想尝试隔离您用来比较的数据是否大。在上面的第一个查询,那就是你有:

in 
      (select author.id, max(article.publishedAt) 
      from Article article join article.authors author 
      where author.id in (authors_list)) 

尝试把这种说法到一个临时表,然后再使用小集合的效率数据。 因此,它看起来像:

select author.id, max(article.publishedAt) into #temp1 
       from Article article join article.authors author 
       where author.id in (authors_list)) 

select author, article 
      from Article article inner join article.authors author 
      where (author.id, article.publishedAt) in 
      (select author.id, article.publishedAt 
      from #temp1) 
      group by author.id 

由于计算完成,然后数据集较小,应该提高性能。