您现在的位置是:首页 > 博客网站制作 > 个人博客搭建个人博客搭建
PostgreSQL表膨胀监控案例(精确计算)
杨青青2022-02-20【个人博客搭建】人已围观
简介膨胀率的精确计算PostgreSQL自带了pgstattuple模块,可用于精确计算表的膨胀率。譬如这里的tuple_percent字段就是元组实际字节占关系总大小的百分比,用1减去该值即为膨胀率。#
膨胀率的精确计算
PostgreSQL自带了pgstattuple模块,可用于精确计算表的膨胀率。譬如这里的tuple_percent字段就是元组实际字节占关系总大小的百分比,用1减去该值即为膨胀率。
#插入1000W数据
postgres=#insertintotselectid,idfromgenerate_series(1,10000000)asid;
INSERT010000000
#表膨胀系数为0.097
postgres=#select*,1.0-tuple_len::numeric/table_lenasbloatfrompgstattuple('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
(1row)
#占用54055个page
postgres=#select*frompg_relpages('t');
pg_relpages
-------------
54055
(1row)
#删除数据
postgres=#deletefromtwhereid<>10000000;
DELETE9999999
#仍然占用54055个page
postgres=#select*frompg_relpages('t');
pg_relpages
-------------
54055
(1row)
#膨胀率已经为0.999999
postgres=#select*,1.0-tuple_len::numeric/table_lenasbloatfrompgstattuple('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":found5372225removable,2nonremovablerowversionsin54055pages
DETAIL:0deadrowversionscannotberemovedyet.
CPU:user:0.89s,system:0.00s,elapsed:0.89s.
INFO:analyzing"public.t"
INFO:"t":scanned1of1pages,containing2liverowsand0deadrows;2rowsinsample,2estimatedtotalrows
VACUUM
补充:pg索引膨胀问题---重建索引
问题:
发现数据库中很多表的索引大小超过数据大小。经检查,生产CA、CZ、MU、HU、PSG、RIUE库都存在这个现象。
原因:据运行同事介绍索引膨胀问题无法避免,频繁更新就会带来这个问题。
解决方法:
对于大的索引可以采用重建的方式解决。以下两种方法推荐第一种。
方法一:停止应用(这个操作会锁表),重建索引(注:重建完索引名称不变)
sql:reindexindex索引名称
时间:速度较快。2G大小的表,基本上1分钟左右可以建完索引。
还可以针对表重建索引,这个操作会加排他锁:
reindextable表名
方法二:在线建新索引,再把旧索引删除
sql:根据不同索引采用不同的建索引命令,例如:
普通索引
createindexconcurrentlyidx_tbl_2ontbl(id);
dropindexidx_tbl_1;
唯一索引
createuniqueindexconcurrentlyuser_info_username_key_1onuser_info(username);
begin;
altertableuser_infodropconstraintuser_info_username_key;
altertableuser_infoaddconstraintuser_info_username_keyuniqueusingindexuser_info_username_key_1;
end;
主键索引
createuniqueindexconcurrentlyuser_info_pkey_1onuser_info(id);
begin;
altertableuser_infodropconstraintuser_info_pkey;
altertableuser_infoaddconstraintuser_info_pkeyprimarykeyusingindexuser_info_pkey_1;
end;
时间:不停应用的话,业务忙的时候可能会非常长的时间。
Tags:
很赞哦! ()
相关文章
随机图文
个人博客模板青于蓝搭建使用技巧一
越来越多的人都开始搭建自己的个人博客了,个人博客搭建也越来越简单了,其实已经形成了模板化、机械化,真正属于自己原创的个人博客太少太少了,那么没有关系,其实最重要的是如何把个人博客模板设计成适合自己想要的就显得尤为重要了。阿里云拼团活动应该如何找最新链接?
阿里云Hi拼购,最新新用户阿里云hi拼团链接。为了给大家试水,我就先开团了,没错,团长是我,拼团购买的链接已经给大家准备好了,之前在阿里云购买的ecs服务器已经快到期了,其实还有两个月,阿里云活动一出来,考虑到还没有购买的朋友,找人拼团比较麻烦,所以,我就提前购买了服务器。蓝色门户网站模板《青于蓝》安装教程
蓝色门户网站模板《青于蓝》安装教程,将upload压缩包上传到根目录,解压.地址后面输入e/install/正确填写数据库帐号,有端口的填端口,一般都不填。注意修改表前缀menhu_2014年度优秀个人博客排名公布
经筛选合格的参赛个人博客网站,合计29个。评分标准按照网站的(内容10分、创意5分、原创5分、界面10分、兼容性10分、用户体验度10分)总分数50分来排名次。