PostgreSQL表膨脹監(jiān)控案例(精確計(jì)算)
膨脹率的精確計(jì)算
PostgreSQL自帶了pgstattuple模塊,可用于精確計(jì)算表的膨脹率。譬如這里的tuple_percent字段就是元組實(shí)際字節(jié)占關(guān)系總大小的百分比,用1減去該值即為膨脹率。
#插入1000W數(shù)據(jù) postgres=# insert into t select id,id from generate_series(1,10000000) as id; INSERT 0 10000000 #表膨脹系數(shù)為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個(gè)page postgres=# select * from pg_relpages('t'); pg_relpages ------------- 54055 (1 row) #刪除數(shù)據(jù) postgres=# delete from t where id<>10000000; DELETE 9999999 #仍然占用54055個(gè)page postgres=# select * from pg_relpages('t'); pg_relpages ------------- 54055 (1 row) #膨脹率已經(jīng)為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
補(bǔ)充:pg索引膨脹問題---重建索引
問題:
發(fā)現(xiàn)數(shù)據(jù)庫(kù)中很多表的索引大小超過數(shù)據(jù)大小。經(jīng)檢查,生產(chǎn)CA、CZ、MU、HU、PSG、RIUE庫(kù)都存在這個(gè)現(xiàn)象。
原因:據(jù)運(yùn)行同事介紹索引膨脹問題無法避免,頻繁更新就會(huì)帶來這個(gè)問題。
解決方法:
對(duì)于大的索引可以采用重建的方式解決。以下兩種方法推薦第一種。
方法一:停止應(yīng)用(這個(gè)操作會(huì)鎖表),重建索引(注:重建完索引名稱不變)
sql:reindex index 索引名稱
時(shí)間:速度較快。2G大小的表,基本上1分鐘左右可以建完索引。
還可以針對(duì)表重建索引,這個(gè)操作會(huì)加排他鎖 :
reindex table 表名
方法二:在線建新索引,再把舊索引刪除
sql:根據(jù)不同索引采用不同的建索引命令,例如:
普通索引
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;
主鍵索引
create unique index concurrently user_info_pkey_1 on user_info(id); begin; alter table user_info drop constraint user_info_pkey; alter table user_info add constraint user_info_pkey primary key using index user_info_pkey_1; end;
時(shí)間:不停應(yīng)用的話,業(yè)務(wù)忙的時(shí)候可能會(huì)非常長(zhǎng)的時(shí)間。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
PostgreSQL模式匹配與正則表達(dá)式方法總結(jié)
在postgresql中使用正則表達(dá)式時(shí)需要使用關(guān)鍵字“~”,以表示該關(guān)鍵字之前的內(nèi)容需匹配之后的正則表達(dá)式,這篇文章主要給大家介紹了關(guān)于PostgreSQL模式匹配與正則表達(dá)式的相關(guān)資料,需要的朋友可以參考下2022-11-11PostgreSQL存儲(chǔ)過程用法實(shí)戰(zhàn)詳解
這篇文章主要介紹了PostgreSQL存儲(chǔ)過程用法,結(jié)合具體實(shí)例詳細(xì)分析了PostgreSQL數(shù)據(jù)庫(kù)存儲(chǔ)過程的定義、使用方法及相關(guān)操作注意事項(xiàng),并附帶一個(gè)完整實(shí)例供大家參考,需要的朋友可以參考下2018-08-08Windows下Postgresql數(shù)據(jù)庫(kù)的下載與配置方法
這篇文章主要介紹了Windows下Postgresql數(shù)據(jù)庫(kù)的下載與配置方法 ,需要的朋友可以參考下2014-06-06