注册
数据库及会话的运行时指标信息查看工具 MS
专栏/技术分享/ 文章详情 /

数据库及会话的运行时指标信息查看工具 MS

Hanson.T 2025/04/18 194 1 0
摘要

在数据库压测、运维及SQL优化过程中,有时需要对系统或某会话的运行时指标信息(V$SYSSTAT/V$SESSTAT)进行查看和分析,以便了解系统运行过程中产生的各种负载情况。

以往,查看系统运行时指标,主要依靠AWR报告。
不过AWR报告的约束略多:

  1. 因为AWR运行过程对数据库多少会产生些压力,所以现场数据库环境可能没有启用AWR服务;
  2. 生成AWR报告需要依赖已生成的快照数据,频繁创建快照也会消耗服务器资源;
  3. 生成的AWR报告中,主要体现系统级(V$SYSSTAT)统计指标信息内容,对会话级运行时信息体现方式并不好用。

对于指定会话的统计信息,在AWR里是个多列大表格,而且字段名与常用指标还需要自己做对照,用起来颇感不便。
所以以往常是后台自己记录某些指标起止值,然后手工计算差额和时间差,来估算相关指标平均值,或许有好办法但我不知道吧。。。

为了简化过程,提升效率及方便查看,所以我之前写了个程序包,实现快捷获取系统级和会话级统计指标信息。
早先程序包的命名太长了(MY_STATS),里面函数和过程名也都比较长(F_GET_SYSSTATS/P_SHOW_SYSSTATS),实际使用时敲得烦躁。
为了省事,后来索性把名字都做了缩写,包名改成MS,函数和过程名也都缩写,用起来能省点力气。

先贴上MS包源码:
MS_202504171346.txt

MS包内释出2个函数和2个过程,分别用于查询方式返回和直接PRINT输出两种方式

  1. 函数方式用于查询,返回集合对象,适用于在manager等图形化客户端工具中查看
  2. PRINT输出方式,将部分常用统计信息格式化输出,适用于在disql中使用

声明内容如下

--获取前后两次或指定时长范围统计数据差异情况 --DELAY NUMBER 间隔时间长度,单位:秒,可以用小数,如果不传入该参数或该参数为NULL则为取前后两次查询方式 -- SELECT * FROM TABLE(MS.FSYS(0.3)) -- 0.3 为300毫秒 -- SELECT * FROM TABLE(MS.FSYS) --第一次执行取初始值,第二次开始计算与上一次间差值 FUNCTION FSYS(DELAY_SECS NUMBER DEFAULT null) RETURN TT_STATINFO; --获取指定会话在前后两次或时长范围统计数据差异情况 --SID BIGINT 传入待检测的会话SID值 --DELAY NUMBER 间隔时间长度,单位:秒,可以用小数,如果不传入该参数或该参数为NULL则为取前后两次查询方式 -- SELECT * FROM TABLE(MS.FSES(SESSID,0.3)) -- 0.3 为300毫秒 -- SELECT * FROM TABLE(MS.FSES(SESSID)) --第一次执行取初始值,第二次开始计算与上一次间差值 FUNCTION FSES(SID BIGINT, DELAY_SECS NUMBER DEFAULT null) RETURN TT_STATINFO; --格式化文本方式输出前后两次或指定时长范围统计数据差异情况 --格式化输出方式只返回常用的部分指标,如果需要扩展可以自行修改程序 --DELAY NUMBER 间隔时间长度,单位:秒,可以用小数,如果不传入该参数或该参数为NULL则为取前后两次查询方式 -- MS.PSYS; -- 0.3 为300毫秒 -- MS.PSYS; --第一次执行取初始值,第二次开始计算与上一次间差值 PROCEDURE PSYS(DELAY_SECS NUMBER DEFAULT null); --格式化文本方式输出指定会话在前后两次或时长范围统计数据差异情况 --格式化输出方式只返回常用的部分指标,如果需要扩展可以自行修改程序 --SID BIGINT 传入待检测的会话SID值 --DELAY NUMBER 间隔时间长度,单位:秒,可以用小数,如果不传入该参数或该参数为NULL则为取前后两次查询方式 -- MS.PSES(SESSID,0.3); -- 0.3 为300毫秒 -- MS.PSYS(SESSID); --第一次执行取初始值,第二次开始计算与上一次间差值 PROCEDURE PSES(SID BIGINT, DELAY_SECS NUMBER DEFAULT null);

下面是使用示例:
首先是结果集方式,第一次执行查询,返回初始数据,其中列出的指标值是当前实时数据。
image.png

第二次执行查询,返回两次查询间的差值,并计算平均值
image.png

由于是查询方式,所以可以根据实际需要,控制返回结果集的列范围和记录范围,比如:
image.png

查看会话级统计信息,以及指定时间间隔方式的用法类似,这里不赘述,在下面PRINT输出方式的介绍中会做示例。


对于PRINT输出方式,是将查询模式的结果,以格式化文本形式输出到控制台。

因为系统级指标量较大(250117版本中已经有243个指标项),而实际关注的指标范围可能只有很少部分,所以PRINT输出方式仅显示部分我自以为常用的指标项。

用法示例:
第一次执行ms.psys过程,返回的是当前值。

SQL> set serveroutput on SQL> SQL> ms.psys; +-----------+--------------------------------+-------+------------+-----+----------+---------+ |Class Name |Statistic Name |Beg Val|End Val |Total|Per Second|Per Trans| +-----------+--------------------------------+-------+------------+-----+----------+---------+ | |stat time(ms) | |13:01:59.419| | | | |SQL |parse count | | 1051| | | | |SQL |hard parse count | | 1021| | | | |SQL |parser errors | | 11| | | | |SQL |sql executed count | | 4800| | | | |SQL |select statements | | 425| | | | |SQL |insert statements | | 0| | | | |SQL |delete statements | | 0| | | | |SQL |update statements | | 0| | | | |SQL |ddl statements | | 240| | | | |SQL |select statements in pl/sql | | 678| | | | |SQL |insert statements in pl/sql | | 4222| | | | |SQL |delete statements in pl/sql | | 1151| | | | |SQL |update statements in pl/sql | | 389| | | | |SQL |DDL in pl/sql count | | 0| | | | |SQL |dynamic exec in pl/sql | | 13| | | | |SQL |table scan count | | 1006| | | | |SQL |hash join count | | 632| | | | |SQL |logons current | | 5| | | | |SQL |User calls count | | 1497| | | | |Transaction|transaction total count | | 938| | | | |Transaction|transaction total time in sec | | 35741| | | | |Transaction|transaction commit count | | 903| | | | |Transaction|transaction rollback count | | 37| | | | |Transaction|transaction deadlock count | | 0| | | | |RLOG |redo log size in bytes | | 4151591| | | | |IO |physical read count | | 237| | | | |IO |physical write count | | 2849| | | | |B Tree |btree split count | | 0| | | | |Network |total bytes received from client| | 1829895| | | | |Network |total bytes sent to client | | 1567115| | | | |CPU |DB time(ms) | | 47345| | | | |CPU |parse time(ms) | | 2286| | | | |CPU |hard parse time(ms) | | 0| | | | |CPU |io wait time(ms) | | 310| | | | |CPU |trx lock wait time(ms) | | 0| | | | |CPU |CPU time(ms) | | 5787| | | | |OS |os total cpu rate | | 0| | | | |OS |os system cpu rate | | 0| | | | |OS |os DM database cpu rate | | 0| | | | +-----------+--------------------------------+-------+------------+-----+----------+---------+ DMSQL 过程已成功完成

第二次执行,返回前后两次间差值和平均值

SQL> ms.psys; +-----------+--------------------------------+------------+------------+-----+----------+---------+ |Class Name |Statistic Name |Beg Val |End Val |Total|Per Second|Per Trans| +-----------+--------------------------------+------------+------------+-----+----------+---------+ | |stat time(ms) |13:02:43.573|13:03:07.686|24113| | | |SQL |parse count | 1057| 1072| 15| 0.62| 1.50| |SQL |hard parse count | 1032| 1043| 11| 0.46| 1.10| |SQL |parser errors | 11| 11| 0| 0| 0| |SQL |sql executed count | 4800| 4800| 0| 0| 0| |SQL |select statements | 426| 430| 4| 0.17| 0.40| |SQL |insert statements | 0| 0| 0| 0| 0| |SQL |delete statements | 0| 0| 0| 0| 0| |SQL |update statements | 0| 0| 0| 0| 0| |SQL |ddl statements | 243| 243| 0| 0| 0| |SQL |select statements in pl/sql | 688| 691| 3| 0.12| 0.30| |SQL |insert statements in pl/sql | 4289| 4289| 0| 0| 0| |SQL |delete statements in pl/sql | 1165| 1165| 0| 0| 0| |SQL |update statements in pl/sql | 392| 392| 0| 0| 0| |SQL |DDL in pl/sql count | 0| 0| 0| 0| 0| |SQL |dynamic exec in pl/sql | 13| 13| 0| 0| 0| |SQL |table scan count | 1014| 1020| 6| 0.25| 0.60| |SQL |hash join count | 637| 640| 3| 0.12| 0.30| |SQL |logons current | 5| 5| 0| 0| 0| |SQL |User calls count | 1507| 1522| 15| 0.62| 1.50| |Transaction|transaction total count | 945| 955| 10| 0.41| 1.00| |Transaction|transaction total time in sec | 35741| 40208| 4467| 185.25| 446.70| |Transaction|transaction commit count | 910| 920| 10| 0.41| 1.00| |Transaction|transaction rollback count | 37| 37| 0| 0| 0| |Transaction|transaction deadlock count | 0| 0| 0| 0| 0| |RLOG |redo log size in bytes | 4198118| 4198615| 497| 20.61| 49.70| |IO |physical read count | 237| 237| 0| 0| 0| |IO |physical write count | 2863| 2863| 0| 0| 0| |B Tree |btree split count | 0| 0| 0| 0| 0| |Network |total bytes received from client| 1849634| 1850873| 1239| 51.38| 123.90| |Network |total bytes sent to client | 1577028| 1605615|28587| 1185.54| 2858.70| |CPU |DB time(ms) | 47789| 48010| 221| 9.17| 22.10| |CPU |parse time(ms) | 2311| 2311| 0| 0| 0| |CPU |hard parse time(ms) | 0| 0| 0| 0| 0| |CPU |io wait time(ms) | 310| 310| 0| 0| 0| |CPU |trx lock wait time(ms) | 0| 0| 0| 0| 0| |CPU |CPU time(ms) | 5827| 5843| 16| 0.66| 1.60| |OS |os total cpu rate | 0| 0| 0| 0| 0| |OS |os system cpu rate | 0| 0| 0| 0| 0| |OS |os DM database cpu rate | 0| 0| 0| 0| 0| +-----------+--------------------------------+------------+------------+-----+----------+---------+ DMSQL 过程已成功完成

也可以传入等待时间,程序自动计算指定时间间隔前后各指标项的变化情况。
注:因ms的过程也会消耗时间,所以显示出的时间比指定间隔值大一些。
下面是取5秒间隔内各统计指标项的变化情况。

SQL> ms.psys(5); +-----------+--------------------------------+------------+------------+-----+----------+---------+ |Class Name |Statistic Name |Beg Val |End Val |Total|Per Second|Per Trans| +-----------+--------------------------------+------------+------------+-----+----------+---------+ | |stat time(ms) |13:33:32.852|13:33:37.949| 5097| | | |SQL |parse count | 1175| 1195| 20| 3.92| 1.00| |SQL |hard parse count | 1116| 1136| 20| 3.92| 1.00| |SQL |parser errors | 11| 11| 0| 0| 0| |SQL |sql executed count | 5400| 5400| 0| 0| 0| |SQL |select statements | 465| 465| 0| 0| 0| |SQL |insert statements | 0| 0| 0| 0| 0| |SQL |delete statements | 0| 0| 0| 0| 0| |SQL |update statements | 0| 0| 0| 0| 0| |SQL |ddl statements | 243| 243| 0| 0| 0| |SQL |select statements in pl/sql | 709| 710| 1| 0.20| 0.05| |SQL |insert statements in pl/sql | 4289| 4289| 0| 0| 0| |SQL |delete statements in pl/sql | 1168| 1168| 0| 0| 0| |SQL |update statements in pl/sql | 395| 395| 0| 0| 0| |SQL |DDL in pl/sql count | 0| 0| 0| 0| 0| |SQL |dynamic exec in pl/sql | 13| 13| 0| 0| 0| |SQL |table scan count | 1061| 1061| 0| 0| 0| |SQL |hash join count | 663| 663| 0| 0| 0| |SQL |logons current | 7| 7| 0| 0| 0| |SQL |User calls count | 1630| 1652| 22| 4.32| 1.10| |Transaction|transaction total count | 1033| 1053| 20| 3.92| 1.00| |Transaction|transaction total time in sec | 40268| 40268| 0| 0| 0| |Transaction|transaction commit count | 997| 1017| 20| 3.92| 1.00| |Transaction|transaction rollback count | 37| 37| 0| 0| 0| |Transaction|transaction deadlock count | 0| 0| 0| 0| 0| |RLOG |redo log size in bytes | 4214880| 4215402| 522| 102.41| 26.10| |IO |physical read count | 237| 237| 0| 0| 0| |IO |physical write count | 2930| 2930| 0| 0| 0| |B Tree |btree split count | 0| 0| 0| 0| 0| |Network |total bytes received from client| 1861716| 1863136| 1420| 278.60| 71.00| |Network |total bytes sent to client | 1721041| 1722321| 1280| 251.13| 64.00| |CPU |DB time(ms) | 55409| 55412| 3| 0.59| 0.15| |CPU |parse time(ms) | 2313| 2313| 0| 0| 0| |CPU |hard parse time(ms) | 0| 0| 0| 0| 0| |CPU |io wait time(ms) | 310| 310| 0| 0| 0| |CPU |trx lock wait time(ms) | 0| 0| 0| 0| 0| |CPU |CPU time(ms) | 5905| 5905| 0| 0| 0| |OS |os total cpu rate | 0| 0| 0| 0| 0| |OS |os system cpu rate | 0| 0| 0| 0| 0| |OS |os DM database cpu rate | 0| 0| 0| 0| 0| +-----------+--------------------------------+------------+------------+-----+----------+---------+ DMSQL 过程已成功完成

对于查看指定会话的统计信息,返回内容与系统级类似。
因为会话级指标数据中缺少事务执行数,所以无法计算平均每事务的指标值,在输出时隐去Per Trans列。

SQL> MS.PSES(140443899182848); +----------+--------------------------------+-------+------------+-----+----------+ |Class Name|Statistic Name |Beg Val|End Val |Total|Per Second| +----------+--------------------------------+-------+------------+-----+----------+ | |stat time(ms) | |13:03:50.203| | | |SQL |parse count | | 132| | | |SQL |hard parse count | | 88| | | |SQL |select statements | | 69| | | |SQL |insert statements | | 0| | | |SQL |delete statements | | 0| | | |SQL |update statements | | 0| | | |SQL |ddl statements | | 0| | | |SQL |select statements in pl/sql | | 0| | | |SQL |insert statements in pl/sql | | 0| | | |SQL |delete statements in pl/sql | | 0| | | |SQL |update statements in pl/sql | | 0| | | |SQL |dynamic exec in pl/sql | | 0| | | |SQL |table scan count | | 142| | | |SQL |hash join count | | 74| | | |IO |physical read count | | 6| | | |B Tree |btree split count | | 0| | | |Network |total bytes received from client| | 23805| | | |Network |total bytes sent to client | | 190771| | | |CPU |parse time(ms) | | 13| | | |CPU |hard parse time(ms) | | 0| | | |CPU |io wait time(ms) | | 0| | | +----------+--------------------------------+-------+------------+-----+----------+ DMSQL 过程已成功完成

第二次返回差值和平均值

SQL> MS.PSES(140443899182848); +----------+--------------------------------+------------+------------+-----+----------+ |Class Name|Statistic Name |Beg Val |End Val |Total|Per Second| +----------+--------------------------------+------------+------------+-----+----------+ | |stat time(ms) |13:03:50.203|13:04:05.166|14964| | |SQL |parse count | 132| 138| 6| 0.40| |SQL |hard parse count | 88| 88| 0| 0| |SQL |select statements | 69| 75| 6| 0.40| |SQL |insert statements | 0| 0| 0| 0| |SQL |delete statements | 0| 0| 0| 0| |SQL |update statements | 0| 0| 0| 0| |SQL |ddl statements | 0| 0| 0| 0| |SQL |select statements in pl/sql | 0| 0| 0| 0| |SQL |insert statements in pl/sql | 0| 0| 0| 0| |SQL |delete statements in pl/sql | 0| 0| 0| 0| |SQL |update statements in pl/sql | 0| 0| 0| 0| |SQL |dynamic exec in pl/sql | 0| 0| 0| 0| |SQL |table scan count | 142| 150| 8| 0.53| |SQL |hash join count | 74| 77| 3| 0.20| |IO |physical read count | 6| 6| 0| 0| |B Tree |btree split count | 0| 0| 0| 0| |Network |total bytes received from client| 23805| 24407| 602| 40.23| |Network |total bytes sent to client | 190771| 214993|24222| 1618.68| |CPU |parse time(ms) | 13| 13| 0| 0| |CPU |hard parse time(ms) | 0| 0| 0| 0| |CPU |io wait time(ms) | 0| 0| 0| 0| +----------+--------------------------------+------------+------------+-----+----------+ DMSQL 过程已成功完成 已用时间: 3.731(毫秒). 执行号:3409. SQL>

下面是取3秒时间间隔内某会话的各项统计指标变化情况:

SQL> ms.pses(140443604685584,3); +----------+--------------------------------+------------+------------+-----+----------+ |Class Name|Statistic Name |Beg Val |End Val |Total|Per Second| +----------+--------------------------------+------------+------------+-----+----------+ | |stat time(ms) |13:36:56.483|13:36:59.627| 3143| | |SQL |parse count | 101| 103| 2| 0.64| |SQL |hard parse count | 85| 87| 2| 0.64| |SQL |select statements | 15| 17| 2| 0.64| |SQL |insert statements | 0| 0| 0| 0| |SQL |delete statements | 0| 0| 0| 0| |SQL |update statements | 0| 0| 0| 0| |SQL |ddl statements | 0| 0| 0| 0| |SQL |select statements in pl/sql | 0| 0| 0| 0| |SQL |insert statements in pl/sql | 0| 0| 0| 0| |SQL |delete statements in pl/sql | 0| 0| 0| 0| |SQL |update statements in pl/sql | 0| 0| 0| 0| |SQL |dynamic exec in pl/sql | 0| 0| 0| 0| |SQL |table scan count | 6| 34| 28| 8.91| |SQL |hash join count | 3| 29| 26| 8.27| |IO |physical read count | 0| 0| 0| 0| |B Tree |btree split count | 0| 0| 0| 0| |Network |total bytes received from client| 8707| 9017| 310| 98.63| |Network |total bytes sent to client | 9376| 37726|28350| 9020.04| |CPU |parse time(ms) | 0| 8| 8| 2.55| |CPU |hard parse time(ms) | 0| 0| 0| 0| |CPU |io wait time(ms) | 0| 0| 0| 0| +----------+--------------------------------+------------+------------+-----+----------+ DMSQL 过程已成功完成

以上内容是对MS包的简要介绍。
如果有兴趣,可以测试试用,并根据个人需要修改取值范围等。

由于个人技术水平有限,且手头测试环境很少,程序中不可避免存在各种缺陷和不足。希望大家能多多提出宝贵意见。

谢谢。

评论
后发表回复

作者

文章

阅读量

获赞

扫一扫
联系客服