主頁(yè) > 知識(shí)庫(kù) > PostgreSQL表膨脹監(jiān)控案例(精確計(jì)算)

PostgreSQL表膨脹監(jiān)控案例(精確計(jì)算)

熱門標(biāo)簽:網(wǎng)站排名優(yōu)化 鐵路電話系統(tǒng) 百度競(jìng)價(jià)排名 地方門戶網(wǎng)站 AI電銷 服務(wù)外包 Linux服務(wù)器 呼叫中心市場(chǎng)需求

膨脹率的精確計(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索引膨脹問(wèn)題---重建索引

問(wèn)題:

發(fā)現(xiàn)數(shù)據(jù)庫(kù)中很多表的索引大小超過(guò)數(shù)據(jù)大小。經(jīng)檢查,生產(chǎn)CA、CZ、MU、HU、PSG、RIUE庫(kù)都存在這個(gè)現(xiàn)象。

原因:據(jù)運(yùn)行同事介紹索引膨脹問(wèn)題無(wú)法避免,頻繁更新就會(huì)帶來(lái)這個(gè)問(wèn)題。

解決方法:

對(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ò)誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:
  • postgresql修改自增序列操作
  • PostgreSQL 實(shí)現(xiàn)登錄及修改密碼操作
  • Postgresql 默認(rèn)用戶名與密碼的操作
  • postgresql 中的加密擴(kuò)展插件pgcrypto用法說(shuō)明
  • postgresql查看表和索引的情況,判斷是否膨脹的操作
  • 在PostgreSQL中設(shè)置表中某列值自增或循環(huán)方式

標(biāo)簽:黃山 仙桃 蘭州 湖南 衡水 崇左 銅川 湘潭

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PostgreSQL表膨脹監(jiān)控案例(精確計(jì)算)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266