CREATE STATISTICS

用于创建扩展统计对象的一个 SQL 命令

CREATE STATISTICS 是用于创建扩展统计对象的 DDL 命令。

CREATE STATISTICSPostgreSQL 10 中添加。

更改历史记录

示例

使用函数依赖统计信息的 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)

分类

DDL性能规划器SQL 命令

另请参阅

ALTER STATISTICSDROP STATISTICSpg_stats_extpg_stats_ext_exprs

反馈

提交任何关于“CREATE STATISTICS”的评论、建议或更正 在此处