PostgreSQL的pg_stats学习

本文涉及的产品
PolarClaw,2核4GB
简介:

对于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=# 
复制代码
相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍如何基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
关系型数据库 数据库 C语言
PostgreSQL服务端开发学习 -- Datum
在使用C语言开发PostgreSQL后端、客户端应用时,Datum无处不在,所以必须要对Datum有很清楚的了解。
|
关系型数据库 分布式数据库 数据库
沉浸式学习PostgreSQL|PolarDB 19: 体验最流行的开源企业ERP软件 odoo
本文主要教大家怎么用好数据库, 而不是怎么运维管理数据库、怎么开发数据库内核.
1527 2
|
SQL 关系型数据库 测试技术
沉浸式学习PostgreSQL|PolarDB 20: 学习成为数据库大师级别的优化技能
在上一个实验《沉浸式学习PostgreSQL|PolarDB 19: 体验最流行的开源企业ERP软件 odoo》 中, 学习了如何部署odoo和polardb|pg. 由于ODOO是非常复杂的ERP软件, 对于关系数据库的挑战也非常大, 所以通过odoo业务可以更快速提升同学的数据库优化能力, 发现业务对数据库的使用问题(如索引、事务对锁的运用逻辑问题), 数据库的代码缺陷, 参数或环境配置问题, 系统瓶颈等.
1272 1
|
存储 关系型数据库 物联网
沉浸式学习PostgreSQL|PolarDB 14: 共享单车、徒步、旅游、网约车轨迹查询
本文的目的是帮助你了解如何设计轨迹表, 如何高性能的写入、查询、分析轨迹数据.
935 0
|
人工智能 关系型数据库 分布式数据库
沉浸式学习PostgreSQL|PolarDB 16: 植入通义千问大模型+文本向量化模型, 让数据库具备AI能力
本文将带领大家来体验一下如何将“千问大模型+文本向量化模型”植入到PG|PolarDB中, 让数据库具备AI能力.
26486 21
沉浸式学习PostgreSQL|PolarDB 16: 植入通义千问大模型+文本向量化模型, 让数据库具备AI能力
|
关系型数据库 C语言 PostgreSQL
PostgreSQL服务端开发学习 --- 常用结构及宏定义1
本篇主要讲解使用C语言开发PostgreSQL服务端应用(libpq、自定义函数、扩展)常用到的结构及宏定义。
|
关系型数据库 定位技术 分布式数据库
沉浸式学习PostgreSQL|PolarDB 18: 通过GIS轨迹相似伴随|时态分析|轨迹驻点识别等技术对拐卖、诱骗场景进行侦查
本文主要教大家怎么用好数据库, 而不是怎么运维管理数据库、怎么开发数据库内核.
1673 1
|
人工智能 关系型数据库 分布式数据库
沉浸式学习PostgreSQL|PolarDB 17: 向量数据库, 通义大模型AI的外脑
本文所涉及的实验体验的就是怎么建设AI的外脑?向量数据库的核心价值:AI外脑
1863 4
|
关系型数据库 C语言 PostgreSQL
PostgreSQL服务端开发学习 -- fmgr.h
fmgr按官方的解释就是Postgres函数管理器和函数调用接口,在使用C语言开发PostgreSQL后端应用时,所以与backend交互时必须遵循fmgr.h中定义的一些规范。
|
SQL 存储 关系型数据库
PostgreSQL核心之SQL基础学习
PostgreSQL核心之SQL基础学习
531 3

推荐镜像

更多