postgresql 的统计信息

postgresql 的统计信息

os: centos7.4
postgresql:9.6.8

查看os进程

$ ps -ef|grep -i post |grep -i stat
postgres 10782 10770  0 May09 ?        00:02:42 postgres: stats collector process

表的信息

mondb=# select pg_size_pretty(pg_relation_size('t_gather_pgsql_space_table')), pg_size_pretty(pg_total_relation_size('t_gather_pgsql_space_table'));
 pg_size_pretty | pg_size_pretty 
----------------+----------------
 807 MB         | 807 MB
(1 row)

mondb=# vacuum full t_gather_pgsql_space_table;
VACUUM

mondb=# h analyze
Command:     ANALYZE
Description: collect statistics about a database
Syntax:
ANALYZE [ VERBOSE ] [ table_name [ ( column_name [, ...] ) ] ]

mondb=# analyze verbose t_gather_pgsql_space_table;
INFO:  analyzing "public.t_gather_pgsql_space_table"
INFO:  "t_gather_pgsql_space_table": scanned 30000 of 103335 pages, containing 783353 live rows and 0 dead rows; 30000 rows in sample, 2698619 estimated total rows
ANALYZE
mondb=# show default_statistics_target;
 default_statistics_target
---------------------------
 100
(1 row)
select pc.*
from pg_class pc
where 1=1
and pc.relname = 't_gather_pgsql_space_table'
order by pc.relname
;

select *
from pg_stats ps
where 1=1
and ps.tablename='t_gather_pgsql_space_table'
;

select *
from pg_statistic ps
where 1=1
and ps.starelid=16636
;

default_statistics_target (integer)

为没有通过ALTER TABLE SET STATISTICS设置列相关目标的表列设置默认统计目标。更大的值增加了需要做ANALYZE的时间,但是可能会改善规划器的估计质量。默认值是 100。
有关PostgreSQL查询规划器使用的统计信息的更多内容, 请参考第 14.2 节。

mondb=# set default_statistics_target = 1000;
SET
mondb=# analyze verbose t_gather_pgsql_space_table;
INFO:  analyzing "public.t_gather_pgsql_space_table"
INFO:  "t_gather_pgsql_space_table": scanned 103335 of 103335 pages, containing 2697644 live rows and 0 dead rows; 300000 rows in sample, 2697644 estimated total rows
ANALYZE


mondb=# d+ t_gather_pgsql_space_table

SET STATISTICS

这种形式为后续的ANALYZE操作设置针对每列 的统计收集目标。目标可以被设置在范围 0 到 10000 之间,还可以 把它设置为 -1 来恢复到使用系统默认的统计目标( default_statistics_target)。更多有关 PostgreSQL查询规划器使用统计 信息的内容可见第 14.2 节。

SET STATISTICS要求一个SHARE UPDATE EXCLUSIVE锁。

mondb=# ALTER TABLE t_gather_pgsql_space_table ALTER COLUMN tab_name SET STATISTICS 200;
ALTER TABLE

mondb=# analyze verbose t_gather_pgsql_space_table;
INFO:  analyzing "public.t_gather_pgsql_space_table"
INFO:  "t_gather_pgsql_space_table": scanned 103335 of 103335 pages, containing 2697644 live rows and 0 dead rows; 300000 rows in sample, 2697644 estimated total rows
ANALYZE

SET (n_distinct)

当前,已定义的针对每个属性的 选项只有n_distinct和n_distinct_inherited, 它们会覆盖后续ANALYZE操作所得到的可区分值数量估计。
n_distinct影响该表本身的统计信息,
而 n_distinct_inherited影响为该表外加其继承子女收集的统计信息。
当被设置为一个正值时,ANALYZE将假定该列刚好包含指定 数量的可区分非空值。当被设置为一个负值(必须大于等于 -1)时, ANALYZE将假定可区分非空值的数量与表的尺寸成线性比例,
确切的计数由估计的表尺寸乘以给定数字的绝对值计算得到。例如,值 -1 表示 该列中所有的值都是可区分的,而值 -0.5 则表示每一个值平均出现两次。当表 的尺寸随时间变化时,这会有所帮助,
因为这种计算只有在查询规划时才会被 执行。指定值为 0 将回到正常的估计可区分值数量的做法。更多有关 PostgreSQL查询规划器使用统计 信息的内容可见第 14.2 节。

更改针对每个属性的选项要求一个 SHARE UPDATE EXCLUSIVE锁。

mondb=# ALTER TABLE t_gather_pgsql_space_table ALTER COLUMN tab_name SET (n_distinct = 500);
ALTER TABLE

mondb=# analyze verbose t_gather_pgsql_space_table;
INFO:  analyzing "public.t_gather_pgsql_space_table"
INFO:  "t_gather_pgsql_space_table": scanned 103335 of 103335 pages, containing 2697644 live rows and 0 dead rows; 300000 rows in sample, 2697644 estimated total rows
ANALYZE