使用HQL的from子句中的子查询

joh*_*tam 6 sql hibernate hql

我有一个articles500k行的表.一篇文章有​​一份作者列表.我正在尝试创建一个查询,以获取最新发布的作者列表文章.

我使用了以下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
Run Code Online (Sandbox Code Playgroud)

在普通的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;
Run Code Online (Sandbox Code Playgroud)

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

有没有办法使用HQL或其他方式来模拟这种查询来增强查询的性能?

小智 0

在任何一种情况下,您都需要尝试隔离用于比较的数据(如果数据很大)。在上面的第一个查询中,您有:

in 
            (select author.id, max(article.publishedAt) 
            from Article article join article.authors author
            where author.id in (authors_list))
Run Code Online (Sandbox Code Playgroud)

尝试先将该语句放入临时表中,然后使用该小数据集以提高效率。所以它看起来像:

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
Run Code Online (Sandbox Code Playgroud)

因为计算完成后数据集较小,所以应该会提高性能。