gpt4 book ai didi

postgresql - 为什么这个 COUNT() 在 PostgreSQL 9.2 中需要这么长时间?

转载 作者:行者123 更新时间:2023-11-29 11:41:44 25 4
gpt4 key购买 nike

我的表是(还有一些其他列):

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 .为什么这么慢?

最佳答案

尝试通过 analyze 更新您的统计信息.当您将估计行数 (1.620.001) 与实际行数 (2.584.317) 进行比较时,您会发现您的优化器做得不好。

关于postgresql - 为什么这个 COUNT() 在 PostgreSQL 9.2 中需要这么长时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18800501/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com