CREATE STATISTICS

用于创建扩展统计信息对象的 SQL 命令

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

CREATE STATISTICS 添加于 PostgreSQL 10

更改历史记录

示例

使用功能依赖统计信息(此处 max_parallel_workers_per_gather 设置为 0)的 CREATE STATISTICS 用法示例

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”的任何评论、建议或更正 此处