DB2中查看存储过程里SQL语句的执行情况

数据库
本文为您介绍如何在DB2数据库中,查看存储过程里SQL语句的执行情况。其中动态SQL可以用snapshot查看,存储过程里SQL语句的执行情况用event monitor查看。

本文为您介绍如何在DB2数据库中,查看存储过程里SQL语句的执行情况。其中可以用snapshot查看动态SQL,用event monitor查看存储过程里SQL语句的执行情况。如果您对此有兴趣,不妨一看,相信对您会有所帮助。

动态SQL可以用snapshot查看,存储过程里SQL语句的执行情况用event monitor查看。
但是event monitor没有记录SQL语句,而是记录了package id和Section id。
根据package id和Section id再查询系统表就可以得到原始的SQL语句。

例子如下:
(1)创建存储过程
create procedure sales_status
(in quota integer)
dynamic result sets 2
language sql
begin
declare SQLSTATE char(5);

declare rs cursor with return for
select sales_person, sum(sales) as total_sales
from sales
group by sales_person
having sum(sales) > quota;

open rs;

insert into tt1 values (1),(2),(3),(4),(5);

end#p#
@

(2)创建event monitor,并捕获statement信息
db2 create event monitor ev2 for statements write to file 'D: mp'
db2 set event monitor ev2 state 1
db2 "call sales_status(10)"
db2 FLUSH EVENT MONITOR ev2
db2evmon -db sample -evm ev2 > 1.out

(3)查看输出文件
在call sales_status(10)语句后面,可以找到
8) Statement Event ...
Appl Handle: 7
Appl Id: *LOCAL.DB2.070809034142
Appl Seq number: 00053

Record is the result of a flush: FALSE
-------------------------------------------
Type : Static
Operation: Execute
Section : 2
Creator : DB2ADMIN
Package : P2323139
Consistency Token : oAfgMJIX
Package Version ID : #p#
Cursor :
Cursor was blocking: FALSE
-------------------------------------------
Start Time: 2007-08-09 12:33:22.394140
Stop Time: 2007-08-09 12:33:22.394599
Exec Time: 0.000459 seconds
Number of Agents created: 1
User CPU: 0.000000 seconds
System CPU: 0.000000 seconds
Fetch Count: 0
Sorts: 0
Total sort time: 0
Sort overflows: 0
Rows read: 1
Rows written: 5
...

10) Statement Event ...
Appl Handle: 7
Appl Id: *LOCAL.DB2.070809034142
Appl Seq number: 00053

Record is the result of a flush: FALSE
-------------------------------------------
Type : Static
Operation: Close
Section : 1#p#
Creator : DB2ADMIN
Package : P2323139
Consistency Token : oAfgMJIX
Package Version ID :
Cursor : RS
Cursor was blocking: TRUE
-------------------------------------------
Start Time: 2007-08-09 12:33:22.390159
Stop Time: 2007-08-09 12:33:22.398984
Exec Time: 0.008825 seconds
Number of Agents created: 1
User CPU: 0.000000 seconds
System CPU: 0.000000 seconds
Fetch Count: 3
Sorts: 1
Total sort time: 0
Sort overflows: 0
Rows read: 45
Rows written: 0
...

看到Package=P2323139, Section=1的SQL有 Rows read: 45
看到Package=P2323139, Section=2的SQL有 Rows read: 1, Rows written: 5

(4)查询系统表,就可以看到原始的SQL语句了
select s.STMTNO, s.SECTNO, s.TEXT
from SYSCAT.STATEMENTS s
where s.PKGNAME='P2323139' ;

STMTNO SECTNO TEXT ----------- ------ -------
8 1 DECLARE RS cursor with return for select SALES_PERSON, SUM(SALES) as TOTAL_SALES from SALES group by SALES_PERSON having SUM(SALES) > :HV00008 :HI00008
17 2 insert into TT1 values (1),(2),(3),(4),(5)
 

责任编辑:段燃 来源: 互联网
相关推荐

2010-11-04 12:00:59

db2存储过程

2010-08-31 15:39:25

DB2存储过程

2010-11-04 13:55:26

DB2 SQL脚本批量

2010-09-06 15:13:05

DB2

2010-11-04 15:39:40

DB2 SQL语句

2010-08-05 14:50:23

DB2动态SQL

2010-08-05 10:20:29

DB2数据库动态

2010-08-18 13:59:04

DB2纯SQL存储过程

2010-09-06 10:54:52

DB2 sql 存储过

2010-09-06 10:43:15

DB2 SQL存储过程

2010-11-03 10:35:45

DB2存储过程

2010-11-02 11:43:11

DB2动态SQL

2010-08-18 13:29:11

DB2存储过程

2010-09-01 11:46:01

DB2临时表SQL

2010-11-03 10:26:22

DB2存储过程

2010-11-03 10:46:49

DB2存储过程

2010-08-05 10:42:33

DB2 拉链表存储过程

2010-08-27 14:54:21

DB2批处理脚本

2010-11-04 13:14:16

DB2执行SQL语句

2010-11-11 11:25:45

SQL UPDATE语
点赞
收藏

51CTO技术栈公众号