2014-01-22 119 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 

由於計算完成,然後數據集較小,應該提高性能。