使用count优化查询(不同)

时间:2014-08-13 16:57:45

标签: mysql sql database performance

我有一个SQL查询,其简化版本看起来像这样:

SELECT
      ff.f_id AS field_id,
      COUNT(DISTINCT pca.pc_id) AS num_distinct_bundles,
      COUNT(DISTINCT pca.p_id) AS num_distinct_programs
FROM
      sometable  ff,
      sometable2 b,
      sometable3 pca
WHERE
          ff.f_id = b.f_id
      AND b.pc_id = pca.pc_id
GROUP BY
      ff.f_id;

问题是COUNT(DISTINCT)。这需要花费很多时间。我修补并发现了

SELECT 
    COUNT(column)
FROM
    (SELECT DISTINCT column from tableX) as tableY

比直接使用COUNT(DISTINCT)更快。我能够将部分查询写成

SELECT
    ff.f_id AS field_id,
    COUNT(t1.pc_id) as num_distinct_bundles
FROM
    sometable ff,
    sometable2 b,
    (SELECT DISTINCT pca.pc_id FROM sometable3 AS pca) AS t1
WHERE
         ff.f_id = b.f_id
    AND  b.pc_id = t1.pc_id
GROUP BY
    ff.f_id

这将前两列的查询时间减少到初始时间的近1/4。但是我想不出有办法获得第二列num_distinct_programs

这可以用更好的方式完成吗?

1 个答案:

答案 0 :(得分:0)

尝试这会有什么不同:

SELECT
    ff.f_id AS field_id, t1.num_distinct_bundles, t2.num_distinct_programs
FROM
    sometable ff 
    INNER JOIN sometable2 b ON ff.f_id = b.f_id,
    (
    SELECT COUNT(t.pc_id) as num_distinct_bundles
    FROM (
        SELECT DISTINCT pca.pc_id FROM sometable3
        ) AS t
    ) as t1,    
    (
    SELECT COUNT(t.p_id) as num_distinct_programs
    FROM (
        SELECT DISTINCT pca.p_id FROM sometable3 
        ) AS t
    ) as t2 

GROUP BY
    ff.f_id