2011-09-07 3 views
0

많은 조인 된 테이블에 계수 항목을 다음과 같이 양식이있는 경우 공동 테이블의 항목을 계산할 수있는 쿼리를 만듭니다. 그래서 저는 다음과 같은 것을 가지고 있습니다 :MySQL은 : 나는 테이블 '이벤트'여러 공동 테이블이

SELECT t.*, COUNT(distinct t1.id) visible_tags, COUNT(distinct t2.id) 
     invisible_tags, ..., COUNT(distince tn.id) approved_videos 
FROM events t 
LEFT JOIN tags t1 ON ... 
LEFT JOIN tags t2 ON ... 
LEFT JOIN tags t3 ON ... 
... 
LEFT JOIN videos tn ON ... 

문제는 모든 인덱스가있는 쿼리도 꽤 오랜 시간이 걸리는 것입니다. 다르게 양식하는 방법이 있습니까?

감사합니다.

답변

1

@sanmai가 'Explain Select'에 대해 말한 것과 더불어 하위 쿼리를 조인 할 수도 있습니다.

SELECT t.*, t1_count, t2_count 
FROM events t 
LEFT JOIN (select count(distinct id) as t1_count from tags) as t1 
LEFT JOIN (select count(distinct id) as t2_count from tags) as t2 
... 
1

쿼리에 시간이 오래 걸리면 문제가 쿼리에없는 것입니다.

이 쿼리에는 어떤 인덱스가 사용됩니까? 보여주십시오. EXPLAIN SELECT ...

관련 문제