2012-05-22 4 views
1

나는이 쿼리를 사용하고 있습니다 :가장 빠른 그룹당 n 개의 쿼리를 만드는 방법은 무엇입니까?

SELECT district, id 
FROM adverts ls 
GROUP BY district, id 
HAVING (

SELECT count(*) 
FROM adverts 
WHERE district = ls.district 
AND id > ls.id 
) <5 
ORDER BY district, id DESC ; 

LIMIT 0 , 30 

은 약 35 초 tooks. 설명은 다음과 같습니다.

id select_type  table type possible_keys key  key_len  ref  rows Extra 
1 PRIMARY  ls range NULL i_id 5 NULL 16166 Using index for group-by; Using temporary; Using f... 
2 DEPENDENT SUBQUERY ilan_genel ref  PRIMARY,i_id,i_ozellik_id,i_tip_fiyat,i_tip_id,i_d... i_durum_id 2 func 25 Using where; Using index 

더 빠른 방법이 있습니까? Joinssubqueries보다 빠른입니다 정의에 의해

SELECT district, id, COUNT(b.district) 
FROM  adverts a INNER JOIN adverts b 
       ON a.district = b.district 
WHERE  b.id > a.id 
GROUP BY district, id 
HAVING COUNT(b.district) < 5 
ORDER BY district, id DESC 

:

답변

1

이 시도.

관련 문제