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

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

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

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

下面做一个实验:

先建立一个表

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

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

  1. postgres=# \d pg_stats;
  2. View "pg_catalog.pg_stats"
  3. Column | Type | Modifiers
  4. -------------------+----------+-----------
  5. schemaname | name |
  6. tablename | name |
  7. attname | name |
  8. inherited | boolean |
  9. null_frac | real |
  10. avg_width | integer |
  11. n_distinct | real |
  12. most_common_vals | anyarray |
  13. most_common_freqs | real[] |
  14. histogram_bounds | anyarray |
  15. correlation | real |
  16.  
  17. postgres=# select * from pg_stats where tablename='test';
  18. (No rows)

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

  1. postgres=# insert into test values(1);
  2. INSERT 0 1
  3. postgres=# select * from pg_stats where tablename='test';
  4. (No rows)
  5. postgres=# insert into test values(2);
  6. INSERT 0 1
  7. postgres=# select * from pg_stats where tablename='test';
  8. (No rows)

非得anaylize 一下,才可以:

  1. postgres=# analyze;
  2. ANALYZE
  3. postgres=# select * from pg_stats where tablename='test';
  4. -[ RECORD 1 ]-----+-------
  5. schemaname | public
  6. tablename | test
  7. attname | id
  8. inherited | f
  9. null_frac | 0
  10. avg_width | 4
  11. n_distinct | -1
  12. most_common_vals |
  13. most_common_freqs |
  14. histogram_bounds | {1,2}
  15. 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的值也表明了这一点。

  1. postgres=# insert into test values(2);
  2. INSERT 0 1
  3. postgres=# select * from pg_stats where tablename='test';
  4. -[ RECORD 1 ]-----+-------
  5. schemaname | public
  6. tablename | test
  7. attname | id
  8. inherited | f
  9. null_frac | 0
  10. avg_width | 4
  11. n_distinct | -1
  12. most_common_vals |
  13. most_common_freqs |
  14. histogram_bounds | {1,2}
  15. correlation | 1
  16.  
  17. postgres=# analyze;
  18. ANALYZE
  19. postgres=# select * from pg_stats where tablename='test';
  20. -[ RECORD 1 ]-----+-----------
  21. schemaname | public
  22. tablename | test
  23. attname | id
  24. inherited | f
  25. null_frac | 0
  26. avg_width | 4
  27. n_distinct | -0.666667
  28. most_common_vals | {2}
  29. most_common_freqs | {0.666667}
  30. histogram_bounds |
  31. correlation | 1
  32.  
  33. postgres=#

接着观察 correlation :

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

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

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

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

  1. postgres=# select * from pg_stats where tablename='test';
  2. -[ RECORD 1 ]-----+-----------
  3. schemaname | public
  4. tablename | test
  5. attname | id
  6. inherited | f
  7. null_frac | 0
  8. avg_width | 4
  9. n_distinct | -0.666667
  10. most_common_vals | {2}
  11. most_common_freqs | {0.666667}
  12. histogram_bounds |
  13. correlation | 1
  14.  
  15. postgres=# insert into test values(10);
  16. INSERT 0 1
  17. postgres=# insert into test values(9);
  18. INSERT 0 1
  19. postgres=# select * from pg_stats where tablename='test';
  20. -[ RECORD 1 ]-----+-----------
  21. schemaname | public
  22. tablename | test
  23. attname | id
  24. inherited | f
  25. null_frac | 0
  26. avg_width | 4
  27. n_distinct | -0.666667
  28. most_common_vals | {2}
  29. most_common_freqs | {0.666667}
  30. histogram_bounds |
  31. correlation | 1
  32.  
  33. postgres=# analyze;
  34. ANALYZE
  35. postgres=# select * from pg_stats where tablename='test';
  36. -[ RECORD 1 ]-----+---------
  37. schemaname | public
  38. tablename | test
  39. attname | id
  40. inherited | f
  41. null_frac | 0
  42. avg_width | 4
  43. n_distinct | -0.8
  44. most_common_vals | {2}
  45. most_common_freqs | {0.4}
  46. histogram_bounds | {1,9,10}
  47. correlation | 0.9
  48.  
  49. postgres=# insert into test values(5);
  50. INSERT 0 1
  51. postgres=# insert into test values(6);
  52. INSERT 0 1
  53. postgres=# analyze;
  54. ANALYZE
  55. postgres=# select * from pg_stats where tablename='test';
  56. -[ RECORD 1 ]-----+-------------
  57. schemaname | public
  58. tablename | test
  59. attname | id
  60. inherited | f
  61. null_frac | 0
  62. avg_width | 4
  63. n_distinct | -0.857143
  64. most_common_vals | {2}
  65. most_common_freqs | {0.285714}
  66. histogram_bounds | {1,5,6,9,10}
  67. correlation | 0.678571
  68.  
  69. 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. javaservlet处理四种常用api请求get,put,post,delete

    一般在网站搭建中servlet只需处理post,get请求便足已.本篇注重使用javaweb编写restful风格api,在servlet中对四种常用请求进行处理. 在api中对于一个请求要做的通常是 ...

  2. css3动画相关笔记

    1.$(".aa").delay(2500).animate({width:0}); // 延迟 2.setTimeout(function(){ --> css3 anim ...

  3. python中的装饰函数

    在面向对象(OOP)的设计模式中,decorator被称为装饰模式.OOP的装饰模式需要通过继承和组合来实现,而Python除了能支持OOP的decorator外,直接从语法层次支持decorator ...

  4. python3之安装mysql问题

    python3是不能通过pip install mysql或pipinstall mysqldb这样的形式来安装mysql. 只能 pip install PyMySQL 至于如何在文件中引用? 答曰 ...

  5. curl http code 0

    使用curl进行post请求后,接收status code ,结果返回的结果是0 ,但是请求返回的数据是正常的. 检查后发现是执行顺序问题: $response = [ 'statusCode' =& ...

  6. php编译安装报错

    Cannot find OpenSSL's <evp.h> 解决方法: 下载openssl-1.1.0h.tar  包 [root@localhost ~]# cd openssl-1.1 ...

  7. SpringBoot+MyBatis中自动根据@Table注解和@Column注解生成ResultMap

    其实我一点都不想用mybatis,好多地方得自己写,比如这里. 使用mybatis要写大量的xml,烦的一批.最烦人的莫过于写各种resultmap,就是数据库字段和实体属性做映射.我认为这里应该是m ...

  8. Mysql实现主从同步

    根据网上众多参考案例,继续在VM虚拟机里实现MySQL主从同步功能.步骤如下: * 首先明确下环境 主库本地windows ip192.168.0.103 从库虚拟机mysql5.6 ip192.16 ...

  9. 获取某商家当前每个月销量sql语句。

    用两个mysql函数 FROM_UNIXTIME( ordertime )将日期格式转换成时间戳 month( FROM_UNIXTIME( ordertime ) ) 获取当前日期的月 select ...

  10. python class用法

    创建一个名为 Restaurant 的类,其方法 __init__() 设置两个属性: name 和 type  1.创建一个名为 describe_restaurant() 的方法,前者打印前述两项 ...