Slow select distinct query on postgres

后端 未结 4 1455
自闭症患者
自闭症患者 2020-12-30 09:55

I\'m doing the following two queries quite frequently on a table that essentially gathers up logging information. Both select distinct values from a huge number of rows but

4条回答
  •  遥遥无期
    2020-12-30 10:40

    On PostgreSQL 9.3, starting from the answer from Denis:

        select bundles.bundle_id
        from bundles
        where exists (
          select 1 from audit_records
          where audit_records.bundle_id = bundles.bundle_id
          );
    

    just by adding a 'limit 1' to the subquery, I got a 60x speedup (for my use case, with 8 million records, a composite index and 10k combinations), going from 1800ms to 30ms:

        select bundles.bundle_id
        from bundles
        where exists (
          select 1 from audit_records
          where audit_records.bundle_id = bundles.bundle_id limit 1
          );
    

提交回复
热议问题