Postgres 13.6使用顺序扫描,Postgres 10.18仅使用索引扫描

发布于 2025-02-09 18:56:54 字数 1274 浏览 2 评论 0原文

我们已经将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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖兔^ω^ 2025-02-16 18:56:54

可能是因为您没有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 to on and off.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文