磨砺技术珠矶,践行数据之道,追求卓越价值

回到上一级页面: PostgreSQL统计信息索引页     回到顶级页面:PostgreSQL索引页

对于pg_stas,说明文档在这里:

http://www.postgresql.org/docs/9.1/static/view-pg-stats.html

下面做一个实验:

先建立一个表

postgres=# create table test(id integer);
CREATE TABLE
postgres=# \x
Expanded display is on.
postgres=#

此后,观察 pg_stats 中与test表相关的数据,结果是还没有数据。

postgres=# \d pg_stats;
View "pg_catalog.pg_stats"
Column | Type | Modifiers
-------------------+----------+-----------
schemaname | name |
tablename | name |
attname | name |
inherited | boolean |
null_frac | real |
avg_width | integer |
n_distinct | real |
most_common_vals | anyarray |
most_common_freqs | real[] |
histogram_bounds | anyarray |
correlation | real | postgres=# select * from pg_stats where tablename='test';
(No rows)

然后,插入两条数据后看看有何变化:

postgres=# insert into test values(1);
INSERT 0 1
postgres=# select * from pg_stats where tablename='test';
(No rows)
postgres=# insert into test values(2);
INSERT 0 1
postgres=# select * from pg_stats where tablename='test';
(No rows)

非得anaylize 一下,才可以:

postgres=# analyze;
ANALYZE
postgres=# select * from pg_stats where tablename='test';
-[ RECORD 1 ]-----+-------
schemaname | public
tablename | test
attname | id
inherited | f
null_frac | 0
avg_width | 4
n_distinct | -1
most_common_vals |
most_common_freqs |
histogram_bounds | {1,2}
correlation | 1

然后,再插入一条数据,进行对比:也是必须得使用analyze后,才会起变化:

可以看到:当表test中只有 1 和 2 两条数据的时候,其 n_distinct 为 -1,表示它们的值现在是唯一的,就是说没有重复值。

此时:

most_common_vals 和 most_common_freqs的值都是空的。

histogram_bounds 的值是 {1,2},正好是刚才输入的值。

当再插入一条 id=2 的记录之后,状况发生了变化:

由于此id列的值不再是unique的了{1,2,2},所以n_distinct 不再是-1了。

由于2出现的最多,所以n_distinct 变为了2 出现的比率: -0.66667,most_common_vals 和 most_common_freqs的值也表明了这一点。

postgres=# insert into test values(2);
INSERT 0 1
postgres=# select * from pg_stats where tablename='test';
-[ RECORD 1 ]-----+-------
schemaname | public
tablename | test
attname | id
inherited | f
null_frac | 0
avg_width | 4
n_distinct | -1
most_common_vals |
most_common_freqs |
histogram_bounds | {1,2}
correlation | 1 postgres=# analyze;
ANALYZE
postgres=# select * from pg_stats where tablename='test';
-[ RECORD 1 ]-----+-----------
schemaname | public
tablename | test
attname | id
inherited | f
null_frac | 0
avg_width | 4
n_distinct | -0.666667
most_common_vals | {2}
most_common_freqs | {0.666667}
histogram_bounds |
correlation | 1 postgres=#

接着观察 correlation :

correlation 表达的是 逻辑顺序与物理顺序的关系。

由于我插入数据按由小到大来作的,分别插入了 1,2,2,故逻辑顺序与物理顺序目前线性正相关,所以 correlation 为1。

而当我再插入 10,9,5,6之后,逻辑顺序与物理顺序开始发生不一致。

逻辑顺序:{1,2,2,10,9,5,6},故correlation 变成了 0.678571

postgres=# select * from pg_stats where tablename='test';
-[ RECORD 1 ]-----+-----------
schemaname | public
tablename | test
attname | id
inherited | f
null_frac | 0
avg_width | 4
n_distinct | -0.666667
most_common_vals | {2}
most_common_freqs | {0.666667}
histogram_bounds |
correlation | 1 postgres=# insert into test values(10);
INSERT 0 1
postgres=# insert into test values(9);
INSERT 0 1
postgres=# select * from pg_stats where tablename='test';
-[ RECORD 1 ]-----+-----------
schemaname | public
tablename | test
attname | id
inherited | f
null_frac | 0
avg_width | 4
n_distinct | -0.666667
most_common_vals | {2}
most_common_freqs | {0.666667}
histogram_bounds |
correlation | 1 postgres=# analyze;
ANALYZE
postgres=# select * from pg_stats where tablename='test';
-[ RECORD 1 ]-----+---------
schemaname | public
tablename | test
attname | id
inherited | f
null_frac | 0
avg_width | 4
n_distinct | -0.8
most_common_vals | {2}
most_common_freqs | {0.4}
histogram_bounds | {1,9,10}
correlation | 0.9 postgres=# insert into test values(5);
INSERT 0 1
postgres=# insert into test values(6);
INSERT 0 1
postgres=# analyze;
ANALYZE
postgres=# select * from pg_stats where tablename='test';
-[ RECORD 1 ]-----+-------------
schemaname | public
tablename | test
attname | id
inherited | f
null_frac | 0
avg_width | 4
n_distinct | -0.857143
most_common_vals | {2}
most_common_freqs | {0.285714}
histogram_bounds | {1,5,6,9,10}
correlation | 0.678571 postgres=#

回到上一级页面: PostgreSQL统计信息索引页     回到顶级页面:PostgreSQL索引页

磨砺技术珠矶,践行数据之道,追求卓越价值

PostgreSQL的pg_stats学习的更多相关文章

  1. 《A Tour of PostgreSQL Internals》学习笔记——系统表和数据类型

    上周末学习了<A Tour of PostgreSQL Internals>的第一部分(View 1),今天我们继续打开书本,继续View 2 部分. View 2 Postgresql的 ...

  2. 《A Tour of PostgreSQL Internals》学习笔记——进程间通信

    中秋节假期这么快就没了,这几天还一直下雨,索性在家看看书.这次看的是Tom Lane的<A Tour of PostgreSQL Internals>.这篇小随笔就算做学习笔记了.园子里面 ...

  3. 《A Tour of PostgreSQL Internals》学习笔记——查询处理分析

           终于要迎来postgresql的<A Tour of PostgreSQL Internals>系列的最后一篇了.学习是不能拖延的事儿,越拖延事情越多.不废话,一起来看看吧~ ...

  4. 【PostgreSQL】入门学习笔记

      前言: 以下内容为前几天在备考PostgreSQL入门考试时候做的笔记,经过了全职的两天的奋战与实验,并最终顺利通过了PCA初级认证考试.现在把我学习的笔记分享给大家,文中有对应的思维导图图片可供 ...

  5. postgresql+ C#+ DHTMLX 学习汇总

    前台: dhtmlxgrid.显示数据   其格式为: { rows:[ {id:1,data:[1,2,3]} ,{} ]} 如果在postgesql里直接生成这样的串呢?? 这是就今天要做的事. ...

  6. PostgreSQL统计信息索引页

    磨砺技术珠矶,践行数据之道,追求卓越价值 返回顶级页:PostgreSQL索引页 本页记录所有本人所写的PostgreSQL的统计信息相关文摘和文章的链接: pg_stats:   --------- ...

  7. PostgreSQL学习手册

    事实上之前有很长一段时间都在纠结是否有必要好好学习它,但是始终都没有一个很好的理由说服自己.甚至是直到这个项目最终决定选用PostgreSQL 时,我都没有真正意识到学习它的价值,当时只是想反正和其它 ...

  8. PostgreSQL学习手册(目录)

    原文地址:http://www.cnblogs.com/stephen-liu74/archive/2012/06/08/2315679.html 事实上之前有很长一段时间都在纠结是否有必要好好学习它 ...

  9. 【PostgreSQL】PostgreSQL的安装

    到了新公司,新公司的数据库是使用PostgreSQL,第一次学习,第一次安装. 开始安装:

随机推荐

  1. 数据库连接池及并发库Theron

  2. 纯css3跑马灯demo

    我们写跑马灯一般都是用js控制定时器不断循环产生,但是定时器消耗比较大,特别是程序中很多用到定时器的时候,感觉有的时候比较卡.但是css3样式一般不会.这里主要的思路就是用css3代替js定时器实现一 ...

  3. 【[APIO2007]动物园】

    我好\(sb\)啊,把\(>>\)打成\(<<\)结果就写了两节课 那个一个人只能看到五个动物显然很鬼畜 那我们就可以压这一维了 \(dp[i][s]\)表示从第\(i\)个位 ...

  4. weblogic之CVE-2016-0638反序列化分析

    此漏洞是基于CVE-2015-4852漏洞进行黑名单的绕过,CVE-2015-4852补丁主要应用在三个位置上 weblogic.rjvm.InboundMsgAbbrev.class :: Serv ...

  5. Odoo启动配置文件

    转载请注明原文地址:https://www.cnblogs.com/cnodoo/p/9278687.html 1:--xmlrpc-port=<端口> 命令选项充许我们将服务器实例的侦听 ...

  6. 【bbs】logout.php

    字体大小通过js设定,并结合@media,可实现自适应. 图片自适应窗口 实现流水灯手机端不滚动,script嵌套 多余文字省略号显示  http://www.cnblogs.com/yujihang ...

  7. java 静态相关内容

    一般情况下,如果有些代码必须在项目启动的时候就执行的时候,需要使用静态代码块,这种代码是主动执行的;需要在项目启动的时候就初始化,在不创建对象的情况下,其他程序来调用的时候,需要使用静态方法,这种代码 ...

  8. PHP面试系列 之Linux(三)---- Vi/Vim编辑器

    vi 是 unix 家族下最功能强大的文字编辑器,而 vim 則是 vi 的加强版, 编辑模式   指令 說明 * i 在游標位置進入編輯模式   I 在游標行的第一個非空白字元進入編輯模式 * a ...

  9. PAT——1052. 卖个萌

    萌萌哒表情符号通常由“手”.“眼”.“口”三个主要部分组成.简单起见,我们假设一个表情符号是按下列格式输出的: [左手]([左眼][口][右眼])[右手] 现给出可选用的符号集合,请你按用户的要求输出 ...

  10. 快速排序及STL中的sort算法

    快速排序基本思想是,对待排序序列进行划分(Partition),一次划分,选择一个元素作为枢轴,然后将所有比枢轴小的元素放到枢轴的左边,将比枢轴大的元素放到枢轴的右边.然后对该枢轴划分的左右子序列分别 ...