CREATE STATISTICS 是一个用于创建扩展统计信息的 DDL 命令。
CREATE STATISTICS 在 PostgreSQL 10 中被添加。
变更历史
- PostgreSQL 16
- 统计信息名称现在是可选的(提交 624aa2a1)
- PostgreSQL 14
- 支持对表达式创建扩展统计信息(提交 a4d75c86)
- PostgreSQL 10
- 已添加 (commit 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 的博客文章
- 您不知道的 Postgres 10 功能:CREATE STATISTICS - 2018 年 3 月 Samay Sharma / CitusData 的博客文章
