我的表是(还有其他一些列):
id INTEGER
amount INTEGER
amount
上有一个索引。查询是:
explain analyze select count(amount) from receipt
输出是:
Aggregate (cost=215856.23..215856.23 rows=1 width=4) (actual time=180209.785..180209.787 rows=1 loops=1)
-> Index Only Scan using idx_amount on receipt (cost=0.00..215046.23 rows=1620001 width=4) (actual time=0.109..177443.189 rows=2584317 loops=1)
Heap Fetches: 2316761
Total runtime: 180209.868 ms
发生了什么事?使用仅索引扫描,它应该优化请求,如here所述。为什么这么慢?