首页 > 解决方案 > Postgresql select count(*) 耗时太长

问题描述

我的 postgresql 表中有一个表。该表有大约 9.100.000 行。当我执行查询select count(*) from table时,执行时间约为 1.5 分钟。这是正常的吗?我能做些什么来减少这个时间?

标签: postgresqlpostgresql-9.5

解决方案


如果您想估计大小,可以使用 count_estimate。它要快得多。 https://wiki.postgresql.org/wiki/Count_estimate

另一种解决方法是使用统计字段,在每次添加新行时增加它。

另请阅读https://www.citusdata.com/blog/2016/10/12/count-performance/


推荐阅读