PostgreSQL系统表 pg_stats
发布时间:2020-12-13 17:20:20 所属栏目:百科 来源:网络整理
导读:pg_stats是由pg_statistic系统表扩展而来的系统视图,记录的是每个表每个字段的统计信息,用于PostgreSQL优化器做执行计划选择的时候提供参考。 名字 类型 引用 描述 schemaname name pg_namespace.nspname 包含此表的模式名字 tablename name pg_class.reln
pg_stats是由pg_statistic系统表扩展而来的系统视图,记录的是每个表每个字段的统计信息,用于PostgreSQL优化器做执行计划选择的时候提供参考。
下面通过实例来解释表中参数的意思:
create table tb13(id integer,name character varying,age integer);
postgres=# insert into tb13 select generate_series(1,10000),'john',(random()*1000)::integer;
INSERT 0 10000 postgres=# insert into tb13 select generate_series(10001,10100),'tom',(random()*1000)::integer;
INSERT 0 100
postgres=# ALTER TABLE tb13 ALTER COLUMN id set STATISTICS 10;
ALTER TABLE
postgres=# analyze tb13;
ANALYZE
postgres=# select * from pg_stats where tablename='tb13' and attname='id';
-[ RECORD 1 ]----------+-------------------------------------------------------
schemaname | public (表所在的schema)
tablename | tb13 (表名)
attname | id (字段名)
inherited | f (是否是继承而来的字段,t:是;f:否)
null_frac | 0 (null值的百分比,这里为0%)
avg_width | 4 (该字段的平均长度)
n_distinct | -1 (表示该字段的唯一值的个数,-1:表示该字段有唯一约束,大于0的整数,比如m:表示该字段有m个唯一值)
most_common_vals | (高频值,这里没有,因为是主键)
most_common_freqs | (高频值的出现的频率)
histogram_bounds | {1,1010,2020,3030,4040,5050,6060,7070,8080,9090,10100} (该字段除高频值以外值的的柱状图信息)
correlation | 1 (表中记录的逻辑顺序与存储的物理顺序的关系,-1到1之间,1表示逻辑顺序与存储的物理顺序相同,-1表示逻辑顺序与存储的物理顺序相反)
most_common_elems | (该字段是数组元素的统计信息,高频元素)
most_common_elem_freqs | (该字段是数组元素的统计信息,高频元素出现的频率)
elem_count_histogram | (该字段是数组元素的统计信息,该列元素唯一值个数平均分布柱状图)
解释histogram_bounds={1,10100}表示的意思: 如图所示,x轴表示id值,y轴表示的是记录数,表示PostgreSQL统计分析后的结果是,id字段值为1~1010的记录数有1009(1010-1)条,1010~2020的记录数有1010条,…. … ,展示的是id字段的值的分布区间图。 postgres=# alter table tb13 alter column id set statistics 100;
ALTER TABLE
postgres=#
postgres=# analyze tb13;
ANALYZE
postgres=# select * from pg_stats where tablename='tb13' and attname='id';
-[ RECORD 1 ]----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
schemaname | public
tablename | tb13
attname | id
inherited | f
null_frac | 0
avg_width | 4
n_distinct | -1
most_common_vals |
most_common_freqs |
histogram_bounds | {1,101,202,303,404,505,606,707,808,909,1111,1212,1313,1414,1515,1616,1717,1818,1919,2121,2222,2323,2424,2525,2626,2727,2828,2929,3131,3232,3333,3434,3535,3636,3737,3838,3939,4141,4242,4343,4444,4545,4646,4747,4848,4949,5151,5252,5353,5454,5555,5656,5757,5858,5959,6161,6262,6363,6464,6565,6666,6767,6868,6969,7171,7272,7373,7474,7575,7676,7777,7878,7979,8181,8282,8383,8484,8585,8686,8787,8888,8989,9191,9292,9393,9494,9595,9696,9797,9898,9999,10100}
correlation | 1
most_common_elems |
most_common_elem_freqs |
elem_count_histogram |
把statistics的值调成100,统计结果就有100个区间数。
postgres=# select * from pg_stats where tablename='tb13' and attname='name';
-[ RECORD 1 ]----------+----------------------
schemaname | public
tablename | tb13
attname | name
inherited | f
null_frac | 0
avg_width | 4
n_distinct | 2 (这里是2,表示该字段有2个唯一值)
most_common_vals | {john,tom} (高频值是‘john’和‘tom’)
most_common_freqs | {0.990099,0.00990099} (‘john’对应的频率是0.990099,‘tom’对应的频率是0.00990099)
histogram_bounds | (该字段除了高频值就没有其他值了,所有没有该字段的柱状图)
correlation | 1(表示逻辑顺序与存储的物理顺序相同)
most_common_elems |
most_common_elem_freqs |
elem_count_histogram |
postgres=# create table tb15(id integer);
CREATE TABLE postgres=# insert into tb15 select generate_series(1,5);
INSERT 0 5 postgres=# select * from tb15;
id
----
1
2
3
4
5
(5 rows)
postgres=# analyze tb15;
ANALYZE
postgres=# select tablename,attname,correlation from pg_stats where tablename ='tb15' and attname='id';
tablename | attname | correlation
-----------+---------+-------------
tb15 | id | 1
(1 row)
postgres=# truncate tb15;
TRUNCATE TABLE postgres=# insert into tb15 select generate_series(5,1,-1);
INSERT 0 5 postgres=# select * from tb15;
id
----
5
4
3
2
1
(5 rows)
postgres=# analyze tb15;
ANALYZE
postgres=# select tablename,correlation from pg_stats where tablename ='tb15' and attname='id';
tablename | attname | correlation
-----------+---------+-------------
tb15 | id | -1
(1 row)
postgres=# create table tb16 (id integer,info int[]);
CREATE TABLE postgres=# postgres=# insert into tb16 select generate_series(1,'{1,1,2,3,4,4}'::int[];
INSERT 0 10000 postgres=# insert into tb16 select generate_series(1,100),'{100,200}'::int[];
INSERT 0 100 postgres=# postgres=# analyze tb16;
ANALYZE
postgres=#
postgres=# select * from pg_stats where tablename ='tb16' and attname='info';
-[ RECORD 1 ]----------+---------------------------------------------------
schemaname | public
tablename | tb16
attname | info
inherited | f
null_frac | 0
avg_width | 52
n_distinct | 2
most_common_vals | {"{1,4}","{100,200}"}(高频字段值)
most_common_freqs | {0.990099,0.00990099}(高频字段值对应的频率)
histogram_bounds |
correlation | 1
most_common_elems | {1,100,200}(高频元素值)
most_common_elem_freqs | {0.990099,0.990099,0.00990099,0}(高频元素值对应的频率)
elem_count_histogram | {2,3.9802}(该列元素唯一值个数平均分布柱状图,末尾为平均唯一值个数. 本例说明大部分为4个元素(就是{1,1,2,2,3,3,4,4}这个数组))
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |