postgresql:记录表膨胀引起的io问题的处理

news/2024/7/9 20:10:00 标签: postgresql, 数据库

文章目录

  • 1. io异常
  • 2.查看profile报告
    • 2.1 生成事发时间段的pgprofile
    • 2.2 查看报告
  • 3.检查table是否膨胀
  • 4.执行vacuum full
  • 5.总结

1. io异常

iostat -x 1 20
Device            r/s     w/s     rkB/s     wkB/s   rrqm/s   wrqm/s  %rrqm  %wrqm r_await w_await aqu-sz rareq-sz wareq-sz  svctm  %util
loop0            0.00    0.00      0.00      0.00     0.00     0.00   0.00   0.00    0.00    0.00   0.00     0.00     0.00   0.00   0.00
loop1            0.00    0.00      0.00      0.00     0.00     0.00   0.00   0.00    0.00    0.00   0.00     0.00     0.00   0.00   0.00
loop2            0.00    0.00      0.00      0.00     0.00     0.00   0.00   0.00    0.00    0.00   0.00     0.00     0.00   0.00   0.00
sdb            526.00   62.00   8896.00    976.00     2.00     0.00   0.38   0.00    5.18    5.68   3.04    16.91    15.74   1.70 100.00
sda              0.00    3.00      0.00     16.00     0.00     1.00   0.00  25.00    0.00    0.00   0.00     0.00     5.33   0.00   0.00

2.查看profile报告

2.1 生成事发时间段的pgprofile

(参阅:使用 pg_profile 在 Postgres 中生成性能报告

2.2 查看报告

这里主要查看产生physical read最多的sql:
a8da96b7d2的read占用了66.78%,且命中率仅12.33%
在这里插入图片描述
点击a8da96b7d2可获得sql内容:

select l.factoryno, l.buildno, l.opgroupno, l.arbpl, a.prodbatch as podr_no, a.zzmdmark as artic_no, a.mold_no as mold_no, l.deptno, a.stepname, a.part, a.componentdescen, a."action", a.sizeno as sizeno, substring(max(a.createon),$5,$6) as in_time, max(a.createon) as createon, coalesce(b.targetqty,$7) as t_qty, coalesce(sum(a.primaryquantity),$8) as a_qty from (select a.*, mda.mold_no from pp_outboundshoeprod a inner join (select distinct a.matnr, a.zzgendr, a.mold_no from pp_mda_data a) mda on substring(a.productname,$9,$10) = mda.matnr) a left join pp_mes_outboundaopgroupctrl l on a.opgroup = l.opgroupno left join pp_daily_target b on a.werks = b.fact_id and a.opgroup = b.dept_id and a.mold_no = b.mold_code and a.stepname = b.stepname and a.create_date = b.targetdate where a.vbeln <> $11 and a.stepname in ($12,$13,$14,$15,$16) and a."action" = $17 and a.create_date = $1 and a.stepname = $2 and l.factoryno = $3 and l.buildno = $4 group by l.factoryno, l.buildno, l.opgroupno, l.arbpl, a.prodbatch, a.zzmdmark, a.mold_no, l.deptno, a.stepname, a.part, a.componentdescen, a."action", a.sizeno, b.targetqty order by l.deptno

3.检查table是否膨胀

查看每個頁的freespace率,作為是否執行vacuum full的依據

select
schema,relname,count(1) as num_pages,
pg_relation_size(schema||'.'||relname::text) as bytes_in_table,
pg_size_pretty(pg_relation_size(schema||'.'||relname::text)) as mbytes_in_table,
round(avg(avail::bigint),2) as "avg.freespace_size",
round(avg(avail::bigint)/8192,2) as "av.freespace_ratio"
from
(
select 
	relnamespace::regnamespace as schema,
	relname,
	split_part(substring(pg_freespace(relnamespace::regnamespace::text||'.'||relname::text)::text,'\d+,\d+'),',',1) blkno,
	split_part(substring(pg_freespace(relnamespace::regnamespace::text||'.'||relname::text)::text,'\d+,\d+'),',',2) avail
from pg_class where relkind='r' and relnamespace::regnamespace::text='mes_report'
	) freespace
	group by schema,relname
	order by  bytes_in_table desc

output:
在这里插入图片描述

4.执行vacuum full

postgres=#vacuum full pcnidss.pp_mda_data

再次檢查,size已经由原来的2994M下降到771M

select pg_size_pretty(pg_relation_size('pcnidss.pp_mda_data')

在这里插入图片描述

5.总结

1.pg中,dml操作会产生所谓的dead rows,平常的auto vacuum仅仅将dead rows删除,但是不会释放这部分空间,这将会造成无效scan,特别在full table scan时,会scan所有的空间,包含这部分已删除但未release的空间
2.要真正的释放已删除空间,必须作vacuum full,但这不是一个自动的作业(在pg10版本),另外需要注意的是vacuum full会消耗双倍的空间,实际上它是将表内的行insert到一个临时表,然后将原表清空后再insert回去,所以做这个动作时要查看表所在的磁盘是否有足够的空间


http://www.niftyadmin.cn/n/5188597.html

相关文章

7.docker部署前端vue项目,实现反向代理

介绍&#xff1a;通过docker构建以nginx为基础的镜像&#xff1b;将vue生成的dist包拷贝至nginx目录下&#xff1b;实现项目的部署与反向代理。 1.前提条件 &#xff08;1&#xff09;由于是使用docker来构建镜像&#xff0c;所以本地需要有docker环境。 &#xff08;2&#…

TCP/IP详解卷一第三章“链路层”概要总结(未完编辑中)

本章内容还未编辑完&#xff0c;本章内容稍多&#xff0c;每天会更新一两个点的 在第一章中知道TCP/IP协议族中设计链路层的目的是为IP模块发送和接收IP数据报。它可用于携带一些支持IP的辅助性协议&#xff1b;例如ARP。TCP/IP支持多种不同的链路层&#xff0c;它依赖于使用…

ExoPlayer架构详解与源码分析(9)——TsExtractor

系列文章目录 ExoPlayer架构详解与源码分析&#xff08;1&#xff09;——前言 ExoPlayer架构详解与源码分析&#xff08;2&#xff09;——Player ExoPlayer架构详解与源码分析&#xff08;3&#xff09;——Timeline ExoPlayer架构详解与源码分析&#xff08;4&#xff09;—…

LeakCanary 内存检测

垃圾回收机制根据算法判断对象是否引用 通过引用计数法,被引用则次数1 可达性分析,如果被相互引用,而没有被Gc检测到则也会回收 Android 中 -- finalize 执行回收,System.Gc也是执行回收,一般Gc不会立即调用 会延时 RUntime.getRuntime.gc gc() 引入 调试使用debug…

NewStarCTF2023 Reverse Week3---Let‘s Go Wp

分析 程序打开后结合题目可以发现是 GO语言。 在GO语言中&#xff0c;main_init 要先于 main 函数运行。 在这里对一个iv做了处理。 用插件Signsrch发现AES加密 知道是AES后&#xff0c;就需要找密文&#xff0c;key和iv了。 密文应该就是前面的十六进制字符串。 key和i…

C#值类型设置为null

Nullable<DateTime> date null; 赋默认值防止报错&#xff1a; DateTime ? date new DateTime(3000,1,1); DateTime date2 new date.GetValueOrDefault();

ROS 学习应用篇(七)ROS中的坐标变换管理之tf广播与监听的编程实现

偶吼吼胜利在望&#xff0c;冲冲冲 老规矩新建功能包 工作空间目录下/src下开启终端输入 catkin_create_pkg learning_tf roscpp rospy tf turtlesim 如何实现tf广播 引入库 c python …

【开源】基于JAVA的服装店库存管理系统

项目编号&#xff1a; S 052 &#xff0c;文末获取源码。 \color{red}{项目编号&#xff1a;S052&#xff0c;文末获取源码。} 项目编号&#xff1a;S052&#xff0c;文末获取源码。 目录 一、摘要1.1 项目介绍1.2 项目录屏 二、功能模块2.1 数据中心模块2.2 角色管理模块2.3 服…