加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 创业 > 正文

PostgreSQL表扩展监控案例(准确计较)

发布时间:2021-06-10 00:07:15 所属栏目:创业 来源:互联网
导读:PostgreSQL自带了pgstattuple模块,可用于准确计较表的膨胀率。譬如这里的tuple_percent字段就是元组现实字节占相关总巨细的百分比,用1减去该值即为膨胀率。 #

PostgreSQL自带了pgstattuple模块,可用于准确计较表的膨胀率。譬如这里的tuple_percent字段就是元组现实字节占相关总巨细的百分比,用1减去该值即为膨胀率。

#插入1000W数据
postgres=# insert into t select id,id from generate_series(1,10000000) as id;
INSERT 0 10000000
 
#表膨胀系数为0.097
postgres=# select *, 1.0 - tuple_len::numeric / table_len as bloat from pgstattuple('t');
 table_len | tuple_count | tuple_len | tuple_percent | dead_tuple_count | dead_tuple_len | dead_tuple_percent | free_space | free_percent |   bloat  
-----------+-------------+-----------+---------------+------------------+----------------+--------------------+------------+--------------+------------------------
 442818560 | 10000001 | 400000040 |   90.33 |    0 |    0 |     0 | 1304976 |   0.29 | 0.09669540499838127833
(1 row)
 
#占用54055个page
postgres=# select * from pg_relpages('t');
 pg_relpages
-------------
  54055
(1 row)
 
#删除数据
postgres=# delete from t where id<>10000000;
DELETE 9999999
 
#如故占用54055个page
postgres=# select * from pg_relpages('t');
 pg_relpages
-------------
  54055
(1 row)
 
#膨胀率已经为0.999999
postgres=# select *, 1.0 - tuple_len::numeric / table_len as bloat from pgstattuple('t');
 table_len | tuple_count | tuple_len | tuple_percent | dead_tuple_count | dead_tuple_len | dead_tuple_percent | free_space | free_percent |   bloat  
-----------+-------------+-----------+---------------+------------------+----------------+--------------------+------------+--------------+----------------------------
 442818560 |   2 |  80 |    0 |   9999999 |  399999960 |    90.33 | 1304976 |   0.29 | 0.999999819339099065766349
 
#vacuum表
postgres=# vacuum (verbose,full,analyze) t;
INFO: vacuuming "public.t"
INFO: "t": found 5372225 removable, 2 nonremovable row versions in 54055 pages
DETAIL: 0 dead row versions cannot be removed yet.
CPU: user: 0.89 s, system: 0.00 s, elapsed: 0.89 s.
INFO: analyzing "public.t"
INFO: "t": scanned 1 of 1 pages, containing 2 live rows and 0 dead rows; 2 rows in sample, 2 estimated total rows
VACUUM

增补:pg索引膨胀题目---重建索引

题目:

发明数据库中许多表的索引巨细高出数据巨细。经搜查,出产CA、CZ、MU、HU、PSG、RIUE库都存在这个征象。

缘故起因:据运行同事先容索引膨胀题目无法停止,频仍更新就会带来这个题目。

办理要领:

对付大的索引可以回收重建的方法办理。以下两种要领保举第一种。

要领一:遏制应用(这个操纵会锁表),重建索引(注:重建完索引名称稳固)

sql:reindex index 索引名称

时刻:速率较快。2G巨细的表,根基上1分钟阁下可以建完索引。

还可以针对表重建索引,这个操纵会加排他锁 :

reindex table 表名

要领二:在线建新索引,再把旧索引删除

sql:按照差异索引回收差异的建索引呼吁,譬喻:

平凡索引

create index concurrently idx_tbl_2 on tbl(id);
drop index idx_tbl_1;

独一索引

create unique index concurrently user_info_username_key_1 on user_info(username);
begin;
alter table user_info drop constraint user_info_username_key;
alter table user_info add constraint user_info_username_key unique using index user_info_username_key_1;
end;

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读