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或其他方式来提高查询性能的查询?
几个月前,我回答了一个非常类似的问题。应该帮助你。 http://stackoverflow.com/questions/32486923/how-to-increase-performance-in-sql-query/32487550#32487550 – jswan