我是postgres新手。我们的应用程序有一个类似这样的查询。
select count(distinct pk_column) from table;
explain analyze select count(distinct pk_column) from table;
QUERY PLAN
Aggregate (cost=35797.30..35797.31 rows=1 width=8) (actual time=1251.631..1251.632 rows=1 loops=1)
-> Seq Scan on table (cost=0.00..34606.24 rows=476424 width=8) (actual time=0.006..420.212 rows=477889 loops=1)
Total runtime: 1251.676 ms
Query performance improves when distinct clause on primary key column is removed.
explain analyze select count(pk_column) from table;
QUERY PLAN
Aggregate (cost=35797.30..35797.31 rows=1 width=8) (actual time=817.994..817.995 rows=1 loops=1)
-> Seq Scan on table (cost=0.00..34606.24 rows=476424 width=8) (actual time=0.006..434.674 rows=477890 loops=1)
Total runtime: 818.040 ms
在不更改查询的情况下,是否可以告诉 postgres 忽略主键列上的 distinct 子句?
Version: PostgreSQL 9.1.9 on x86_64-unknown-linux-gnu
, 编译gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3, 64-bit