(Oracle)DBMS_SYSTEM工具-01[20180510]
分析描述:
使用DBMS_SYSTEM分析SQL语句执行,并且获取SQL会话中的绑定变量、等待事件、消耗资源和执行计划等等。
环境介绍:
Oracle 11.2.0.4.0
Linux 5.8平台
工具:
alter system set events 'sql_trace [sql:&&SQL_ID] bind=true,wait=true'; 开启绑定变量的捕获
DBMS_SYSTEM.set_sql_trace_in_session(sid,serial#,ture|false);记录trace文件
执行带变量的SQL语句:
alter system flush shared_pool;--首先清空共享池
首先不开启绑定变量的捕获 ,查看trace文件。
variable B1 varchar2(30);variable B2 varchar2(30);variable B3 varchar2(20);variable B4 varchar2(20);exec :B1:='CHKKP_NEW'exec :B2:='CHKKP_NEW'exec :B3:='CHKKP_NEW'exec :B4:='CHKKP_NEW'set serveroutput ondeclarev_count number;beginfor i in 1..100 loopSELECT count(*) into v_count FROM CHKRULE_D WHERE RULETYPE=:B4 AND NVL(ITEM, :B3 ) = :B3 AND NVL(MODEL, :B2 ) = :B2 AND NVL(CUSTITEM, :B1 ) = :B1 ORDER BY RULESEQ DESC;dbms_output.put_line('Total ROws: ' || v_count);end loop;end;/select sid,serial# from v$session where sid in (select sys_context('USERENV','SID') from dual);
SYS会话监控
17:26:26 SQL> exec dbms_system.set_sql_trace_in_session(314,43729,true);
已順利完成 PL/SQL 程序.
操作系统查看trace文件
[oracle@xxxxx trace]$ ls -ltr|grep trc|grep _ora_
-rw-r----- 1 oracle dba 103524 May 10 17:31 xxxxx0_ora_5156.trc
SYS监控会话
--关闭捕获到trace
17:38:45 SQL> exec dbms_system.set_sql_trace_in_session(314,43729,false);已順利完成 PL/SQL 程序.
查询出执行SQL语句的SQL_ID
col sql_id format a30
col sql_text format a40
select sql_id,sql_text from v$sqlarea where sql_text like '%SELECT count(*) into v_count %' and sql_text not like '%v$sqlarea %';
开启绑定变量的捕获
alter system set events 'sql_trace [sql:3jw2927kh51wb] bind=true,wait=true';alter system set events 'sql_trace [sql:0zsvnxk40mbft] bind=true,wait=true';
[oracle@xxxxxx trace]$ ls -ltr|grep trc|grep _ora_-rw-r----- 1 oracle dba 154002 May 10 17:48 xxxxxx_ora_7977.trc[oracle@xxxxxxx trace]$ less xxxxx_ora_7977.trc
关闭捕获到trace
exec dbms_system.set_sql_trace_in_session(314,43729,false);
exec dbms_system.set_sql_trace_in_session(197,46871,false);
alter system set events 'sql_trace [sql:3jw2927kh51wb] bind=false,wait=true';
alter system set events 'sql_trace [sql:0zsvnxk40mbft] bind=false,wait=true