怎么在PostgreSQL中對(duì)表膨脹進(jìn)行監(jiān)控-創(chuàng)新互聯(lián)

本篇文章為大家展示了怎么在PostgreSQL中對(duì)表膨脹進(jìn)行監(jiān)控,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

創(chuàng)新互聯(lián)建站專注于南通網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供南通營(yíng)銷(xiāo)型網(wǎng)站建設(shè),南通網(wǎng)站制作、南通網(wǎng)頁(yè)設(shè)計(jì)、南通網(wǎng)站官網(wǎng)定制、微信小程序定制開(kāi)發(fā)服務(wù),打造南通網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供南通網(wǎng)站排名全網(wǎng)營(yíng)銷(xiāo)落地服務(wù)。

膨脹率的精確計(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í)間。

多支持創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

上述內(nèi)容就是怎么在PostgreSQL中對(duì)表膨脹進(jìn)行監(jiān)控,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)頁(yè)名稱:怎么在PostgreSQL中對(duì)表膨脹進(jìn)行監(jiān)控-創(chuàng)新互聯(lián)
鏈接地址:http://bm7419.com/article4/dscsoe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航服務(wù)器托管、網(wǎng)站營(yíng)銷(xiāo)、網(wǎng)站設(shè)計(jì)公司、手機(jī)網(wǎng)站建設(shè)搜索引擎優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁(yè)設(shè)計(jì)公司