2014-01-22 2 views
4

테이블이 articles이고 행 수가 500k입니다. 기사에는 저자 목록이 있습니다. 저자 목록에 대한 최신 게시 된 기사를 얻기 위해 쿼리를 작성하려고합니다.HQL을 사용하는 from 절의 하위 쿼리

   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; 

만에서 :

나는 일반 SQL에서 가능한 더 나은 쿼리가 될 것 내가 원하는 걸 얻을 수 있지만 매우 느린 실행하는 다음 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 

을 사용 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 

: 같은 은 그래서 보일 것이다.

관련 문제