postgresql create statistics

os: centos 7.4
db: postgresql 12.2

CREATE STATISTICS will create a new extended statistics object tracking data about the specified table, foreign table or materialized view.
The statistics object will be created in the current database and will be owned by the user issuing the command.

A statistics kind to be computed in this statistics object.
Currently supported kinds are
ndistinct, which enables n-distinct statistics,
dependencies, which enables functional dependency statistics, and
mcv which enables most-common values lists(postgresql 12 新增加).
If this clause is omitted, all supported statistics kinds are included in the statistics object.

版本

# cat /etc/centos-release
CentOS Linux release 7.4.1708 (Core) 
#
# su - postgres
Last login: Wed Mar  4 17:02:54 CST 2020 on pts/2

$ psql -c "select version();"
                                                 version                                                 
---------------------------------------------------------------------------------------------------------
 PostgreSQL 12.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit
(1 row)

create table

postgres=# create table tmp_t0 ( id int8,name varchar(100),memo1 varchar(200),memo2 varchar(200));

postgres=# insert into tmp_t0 select id,md5(id::varchar),md5(md5(id::varchar)),md5(md5(md5(id::varchar))) from generate_series(1,100000) as id;

postgres=# select * from tmp_t0 limit 5;

 id |               name               |              memo1               |              memo2               
----+----------------------------------+----------------------------------+----------------------------------
  1 | c4ca4238a0b923820dcc509a6f75849b | 28c8edde3d61a0411511d3b1866f0636 | 40f5888b67c748df7efba008e7c2f9d2
  2 | c81e728d9d4c2f636f067f89cc14862c | 665f644e43731ff9db3d341da5c827e1 | 4a9bd19b3b8676199592a346051f950c
  3 | eccbc87e4b5ce2fe28308fd9f2a7baf3 | 38026ed22fc1a91d92b5d2ef93540f20 | 771409a8fb1543788fe7d91f1ea0987f
  4 | a87ff679a2f3e71d9181a67b7542122c | 011ecee7d295c066ae68d4396215c3d0 | 1a1f289428bd7ab3beb8a89d4c90b22f
  5 | e4da3b7fbbce2345d7772b0674a318d5 | 4e44f1ac85cd60e3caa56bfd4afb675e | eb4fe264c10eb7a528b047aa983a4829
(5 rows)

postgres=# analyze verbose tmp_t0;
INFO:  analyzing "public.tmp_t0"
INFO:  "tmp_t0": scanned 1725 of 1725 pages, containing 100000 live rows and 0 dead rows; 30000 rows in sample, 100000 estimated total rows
ANALYZE

create statistics

postgres=# \h create statistics
Command:     CREATE STATISTICS
Description: define extended statistics
Syntax:
CREATE STATISTICS [ IF NOT EXISTS ] statistics_name
    [ ( statistics_kind [, ... ] ) ]
    ON column_name, column_name [, ...]
    FROM table_name

URL: https://www.postgresql.org/docs/12/sql-createstatistics.html

dependencies

postgres=# create statistics s0_tmp_t0 (dependencies) on id, name, memo1, memo2 from tmp_t0;

postgres=# analyze verbose tmp_t0;
INFO:  analyzing "public.tmp_t0"
INFO:  "tmp_t0": scanned 1725 of 1725 pages, containing 100000 live rows and 0 dead rows; 30000 rows in sample, 100000 estimated total rows
ANALYZE

ndistinct

postgres=# create statistics s1_tmp_t0 (ndistinct) on id, name, memo1, memo2 from tmp_t0;

postgres=# analyze verbose tmp_t0;
INFO:  analyzing "public.tmp_t0"
INFO:  "tmp_t0": scanned 1725 of 1725 pages, containing 100000 live rows and 0 dead rows; 30000 rows in sample, 100000 estimated total rows
ANALYZE

mcv

postgres=# create statistics s2_tmp_t0 (mcv) on id, name, memo1, memo2 from tmp_t0;

postgres=# analyze verbose tmp_t0;
INFO:  analyzing "public.tmp_t0"
INFO:  "tmp_t0": scanned 1725 of 1725 pages, containing 100000 live rows and 0 dead rows; 30000 rows in sample, 100000 estimated total rows
ANALYZE

或者合起來 create statistics

postgres=# create statistics s3_tmp_t0 (ndistinct,dependencies,mcv) on id, name, memo1, memo2 from tmp_t0;

postgres=# analyze verbose tmp_t0;
INFO:  analyzing "public.tmp_t0"
INFO:  "tmp_t0": scanned 1725 of 1725 pages, containing 100000 live rows and 0 dead rows; 30000 rows in sample, 100000 estimated total rows
ANALYZE

postgres=# select * from pg_stats;

postgres=# select * from pg_stats_ext;

postgres=# select * from pg_statistic;

postgres=# select * from pg_statistic_ext;

postgres=# select * from pg_statistic_ext_data;

參考:
https://www.postgresql.org/docs/12/sql-createstatistics.html
https://www.postgresql.org/docs/12/planner-stats.html#PLANNER-STATS-EXTENDED
https://www.postgresql.org/docs/12/multivariate-statistics-examples.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章