Why does the following join increase the query time significantly?

前端 未结 3 1340
逝去的感伤
逝去的感伤 2020-12-07 04:01

I have a star schema here and I am querying the fact table and would like to join one very small dimension table. I can\'t really explain the following:

EXPL         


        
3条回答
  •  时光取名叫无心
    2020-12-07 04:28

    Rewritten with (recommended) explicit ANSI JOIN syntax:

    SELECT COUNT(impression_id), imp.os_id, os.os_desc 
    FROM   bi.impressions imp
    JOIN   bi.os_desc os ON os.os_id = imp.os_id
    GROUP  BY imp.os_id, os.os_desc;
    

    First of all, your second query might be wrong, if more or less than exactly one match are found in os_desc for every row in impressions.
    This can be ruled out if you have a foreign key constraint on os_id in place, that guarantees referential integrity, plus a NOT NULL constraint on bi.impressions.os_id. If so, in a first step, simplify to:

    SELECT COUNT(*) AS ct, imp.os_id, os.os_desc 
    FROM   bi.impressions imp
    JOIN   bi.os_desc     os USING (os_id)
    GROUP  BY imp.os_id, os.os_desc;
    

    count(*) is faster than count(column) and equivalent here if the column is NOT NULL. And add a column alias for the count.

    Faster, yet:

    SELECT os_id, os.os_desc, sub.ct
    FROM  (
       SELECT os_id, COUNT(*) AS ct
       FROM   bi.impressions
       GROUP  BY 1
       ) sub
    JOIN   bi.os_desc os USING (os_id)
    

    Aggregate first, join later. More here:

    • Aggregate a single column in query with many columns
    • PostgreSQL - order by an array

提交回复
热议问题