Postgres 13.6使用顺序扫描,Postgres 10.18仅使用索引扫描
我们已经将DB从10.18升级到13.6,而对于简单的计数,我注意到了不同的行为。
Postgres 13.6:
explain select count(id) from jobs;
QUERY PLAN
-----------------------------------------------------------------------------------------
Finalize Aggregate (cost=176452.97..176452.98 rows=1 width=8)
-> Gather (cost=176452.75..176452.96 rows=2 width=8)
Workers Planned: 2
-> Partial Aggregate (cost=175452.75..175452.76 rows=1 width=8)
-> Parallel Seq Scan on jobs (cost=0.00..173388.60 rows=825660 width=8)
Postgres 10.18:
explain select count(*) from jobs;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------
Finalize Aggregate (cost=162999.57..162999.58 rows=1 width=8)
-> Gather (cost=162999.35..162999.56 rows=2 width=8)
Workers Planned: 2
-> Partial Aggregate (cost=161999.35..161999.36 rows=1 width=8)
-> Parallel Index Only Scan using jobs_pkey on jobs (cost=0.43..155962.19 rows=2414866 width=0)
不确定为什么在较新版本上我们只有SEQ扫描而不是索引扫描
We have upgraded our db from version 10.18 to 13.6 and for simple count select I have noticed a different behaviour.
Postgres 13.6:
explain select count(id) from jobs;
QUERY PLAN
-----------------------------------------------------------------------------------------
Finalize Aggregate (cost=176452.97..176452.98 rows=1 width=8)
-> Gather (cost=176452.75..176452.96 rows=2 width=8)
Workers Planned: 2
-> Partial Aggregate (cost=175452.75..175452.76 rows=1 width=8)
-> Parallel Seq Scan on jobs (cost=0.00..173388.60 rows=825660 width=8)
Postgres 10.18:
explain select count(*) from jobs;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------
Finalize Aggregate (cost=162999.57..162999.58 rows=1 width=8)
-> Gather (cost=162999.35..162999.56 rows=2 width=8)
Workers Planned: 2
-> Partial Aggregate (cost=161999.35..161999.36 rows=1 width=8)
-> Parallel Index Only Scan using jobs_pkey on jobs (cost=0.43..155962.19 rows=2414866 width=0)
Not sure why on newer version we have only seq scan instead of index scan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能是因为您没有
vacuum
表,这就是使仅索引扫描快速的原因。其他说明可能是在V13数据库中,
Randos_page_cost
设置为更高的值。也许顺序扫描实际上更快。您可以通过使用
enable_seqscan
设置为和OFF
来测试该测试。Probably because you did not
VACUUM
the table, which is what makes index-only scans fast.Other explanations may be that on the v13 database,
random_page_cost
is set to a higher value.Perhaps the sequential scan is actually faster. You can test that by running with
enable_seqscan
set toon
andoff
.