CREATE STATISTICS
是用于创建扩展统计对象的 DDL 命令。
CREATE STATISTICS
在 PostgreSQL 10 中添加。
更改历史记录
- PostgreSQL 16
- 统计信息名称现在是可选的(提交 624aa2a1)
- PostgreSQL 14
- 增加了对表达式扩展统计的支持(提交 a4d75c86)
- PostgreSQL 10
- 添加(提交 7b504eb2)
示例
使用函数依赖统计信息的 CREATE STATISTICS
用法示例(此处 max_parallel_workers_per_gather
设置为 0
)
postgres=# CREATE TABLE foo (c1 INT, c2 INT); CREATE TABLE postgres=# INSERT INTO foo SELECT i/100, i/500 FROM generate_series(1,1000000) s(i); INSERT 0 1000000 postgres=# ANALYZE foo; ANALYZE postgres=# EXPLAIN ANALYZE SELECT * FROM foo WHERE (c1 = 1) AND (c2 = 0); QUERY PLAN --------------------------------------------------------------------------------------------------- Seq Scan on foo (cost=0.00..19425.00 rows=1 width=8) (actual time=83.332..83.333 rows=0 loops=1) Filter: ((c1 = 1) AND (c2 = 0)) Rows Removed by Filter: 1000000 Planning Time: 0.162 ms Execution Time: 83.346 ms (5 rows)
创建统计对象后
postgres=# CREATE STATISTICS s1 (dependencies) ON c1, c2 FROM foo; CREATE STATISTICS postgres=# ANALYZE foo; ANALYZE
规划器得出的行数估计值更加准确
postgres=# EXPLAIN ANALYZE SELECT * FROM foo WHERE (c1 = 1) AND (c2 = 0); QUERY PLAN ---------------------------------------------------------------------------------------------------- Seq Scan on foo (cost=0.00..19425.00 rows=99 width=8) (actual time=71.801..71.801 rows=0 loops=1) Filter: ((c1 = 1) AND (c2 = 2)) Rows Removed by Filter: 1000000 Planning Time: 0.145 ms Execution Time: 71.815 ms (5 rows) postgres=# SELECT count(*) FROM foo WHERE (c1 = 1) AND (c2 = 0); count ------- 100 (1 row)
参考文献
- PostgreSQL 文档: CREATE STATISTICS
有用链接
- 使用 CREATE STATISTICS 提升 GROUP BY 性能 - 2023年1月 Hans-Jürgen Schönig / CyberTec 博客文章
- PostgreSQL:CREATE STATISTICS – 高级查询优化 - 2021年2月 Hans-Jürgen Schönig / CyberTec 博客文章
- 你不知道的 PostgreSQL 10 功能:CREATE STATISTICS - 2018年3月 Samay Sharma / CitusData 博客文章