四、演示10046调试事件
- 1.创建演示环境
- CREATE TABLE sql_trace_test
- (
- id NUMBER
- , description VARCHAR2( 50 )
- );
- EXEC DBMS_STATS.gather_table_stats('SCOTT', 'SQL_TRACE_TEST');
- CREATE OR REPLACE PROCEDURE populate_sql_trace_test( p_loops IN NUMBER ) AS
- l_number NUMBER;
- BEGIN
- FOR i IN 1 .. p_loops
- LOOP
- INSERT INTO sql_trace_test( id, description )
- VALUES ( i
- , 'Description for '
- || i );
- END LOOP;
- SELECT COUNT( * ) INTO l_number FROM sql_trace_test;
- COMMIT;
- DBMS_OUTPUT.put_line( l_number
- || ' rows inserted.' );
- END;
- /
- SHOW ERRORS
- 2.使用10046事件开启8级跟踪
- scott@CNMMBO> ALTER SESSION SET EVENTS '10046 trace name context forever, level 8';
- Session altered.
- scott@CNMMBO> EXEC populate_sql_trace_test(p_loops => 10);
- 10 rows inserted.
- PL/SQL procedure successfully completed.
- scott@CNMMBO> ALTER SESSION SET EVENTS '10046 trace name context off';
- Session altered.
- scott@CNMMBO> ho ls -hltr /u02/database/CNMMBO/udump/*example*
- -rw-r----- 1 Oracle oinstall 9.1K 2012-02-24 10:24 /u02/database/CNMMBO/udump/cnmmbo_ora_6667_trace_sql_example.trc
- 3.使用tkprof工具格式化trace file
- scott@CNMMBO> host
- oracle@SZDB:~/robinson/scripts/dba_scripts/custom/sql> cd /u02/database/CNMMBO/udump/
- oracle@SZDB:/u02/database/CNMMBO/udump> tkprof cnmmbo_ora_6667_trace_sql_example.trc sql_example.txt \
- > explain=scott/tiger sys=no sort=prsela,exeela,fchela
- TKPROF: Release 10.2.0.3.0 - Production on Fri Feb 24 10:33:16 2012
- Copyright (c) 1982, 2005, Oracle. All rights reserved.
- oracle@SZDB:/u02/database/CNMMBO/udump> more sql_example.txt
- TKPROF: Release 10.2.0.3.0 - Production on Fri Feb 24 10:33:16 2012
- Copyright (c) 1982, 2005, Oracle. All rights reserved.
- Trace file: cnmmbo_ora_6667_trace_sql_example.trc
- Sort options: prsela exeela fchela
- ********************************************************************************
- count = number of times OCI procedure was executed
- cpu = cpu time in seconds executing
- elapsed = elapsed time in seconds executing
- disk = number of physical reads of buffers from disk
- query = number of buffers gotten for consistent read
- current = number of buffers gotten in current mode (usually for update)
- rows = number of rows processed by the fetch or execute call
- ********************************************************************************
- BEGIN populate_sql_trace_test(p_loops => 10); END;
- call count cpu elapsed disk query current rows
- ------- ------ -------- ---------- ---------- ---------- ---------- ----------
- Parse 1 0.00 0.00 0 0 0 0
- Execute 1 0.00 0.00 0 0 0 1
- Fetch 0 0.00 0.00 0 0 0 0
- ------- ------ -------- ---------- ---------- ---------- ---------- ----------
- total 2 0.01 0.00 0 0 0 1
- Misses in library cache during parse: 1
- Optimizer mode: ALL_ROWS
- Parsing user id: 66 (SCOTT)
- Elapsed times include waiting on following events:
- Event waited on Times Max. Wait Total Waited
- ---------------------------------------- Waited ---------- ------------
- log file sync 1 0.09 0.09
- SQL*Net message to client 1 0.00 0.00
- SQL*Net message from client 1 0.00 0.00
- ********************************************************************************
- INSERT INTO SQL_TRACE_TEST( ID, DESCRIPTION )
- VALUES
- ( :B1 , 'Description for ' || :B1 )
- call count cpu elapsed disk query current rows
- ------- ------ -------- ---------- ---------- ---------- ---------- ----------
- Parse 1 0.00 0.00 0 0 0 0
- Execute 10 0.00 0.00 0 1 31 10
- Fetch 0 0.00 0.00 0 0 0 0
- ------- ------ -------- ---------- ---------- ---------- ---------- ----------
- total 11 0.00 0.00 0 1 31 10
- Misses in library cache during parse: 1
- Misses in library cache during execute: 1
- Optimizer mode: ALL_ROWS
- Parsing user id: 66 (SCOTT) (recursive depth: 1)
- /**************************************************/
- /* Author: Robinson Cheng */
- /* Blog: http://blog.csdn.net/robinson_0612 */
- /* MSN: robinson_0612@hotmail.com */
- /* QQ: 645746311 */
- /**************************************************/
- 4.演示应用程序上的跟踪(使用client identifier)
- -->根据前面的描述创建角色并授予需要跟踪的用户以及添加client identifier的触发器,并启动应用程序
- oracle@2go-devDB01uv:/u02/database/SYBO2/udump> ls -hltr -->移出不必要的trace文件以清除判断其产生的跟踪文件
- total 8.5K
- drwxr-xr-x 2 oracle oinstall 8.5K 2012-02-24 11:52 old
- oex_admin@SYBO2> create role sql_trace; -->创建角色
- Role created.
- goex_admin@SYBO2> grant sql_trace to goex_webuser; -->将角色授予用户。注:触发器logon_set_identifier已经创建
- Grant succeeded.
- -->启动应用程序后,session的信息如下
- goex_admin@SYBO2> SELECT sid , serial#, username , client_identifier, client_info , module AS module_name
- 2 FROM v$session WHERE username is not null;
- SID SERIAL# USERNAME CLIENT_IDENTIFIER CLIENT_INFO MODULE_NAME
- ---------- ---------- -------------------- ------------------------------ ------------- -----------------
- 1068 6250 GOEX_WEBUSER GOEX_WEBUSER:Robinson.Cheng mBO.exe
- 1073 7201 GOEX_ADMIN SQL*Plus
- 1075 9115 GOEX_ADMIN SQL*Plus
- 1086 60120 GOEX_ADMIN TOAD 10.5.0.41
- -->开启基于client identifier的trace
- goex_admin@SYBO2> exec DBMS_MONITOR.CLIENT_ID_TRACE_ENABLE('GOEX_WEBUSER:Robinson.Cheng',waits=>TRUE, binds=>FALSE );
- PL/SQL procedure successfully completed.
- -->调用应用程序的report 模块并生成report,此时可以看到应用程序多产生了一个session,其sid,serial分别为1078,34788
- goex_admin@SYBO2> SELECT sid , serial#, username , client_identifier, client_info , module AS module_name
- 2 FROM v$session WHERE username is not null;
- SID SERIAL# USERNAME CLIENT_IDENTIFIER CLIENT_INFO MODULE_NAME
- ---------- ---------- -------------------- ------------------------------ ------------- -----------------
- 1068 6250 GOEX_WEBUSER GOEX_WEBUSER:Robinson.Cheng mBO.exe
- 1073 7201 GOEX_ADMIN SQL*Plus
- 1075 9115 GOEX_ADMIN SQL*Plus
- 1078 34788 GOEX_WEBUSER GOEX_WEBUSER:Robinson.Cheng mBO.exe
- 1086 60120 GOEX_ADMIN TOAD 10.5.0.41
- -->report产生后关闭基于client identifier的trace
- goex_admin@SYBO2> exec DBMS_MONITOR.CLIENT_ID_TRACE_DISABLE('GOEX_WEBUSER:Robinson.Cheng');
- PL/SQL procedure successfully completed.
- oracle@2go-devDB01uv:/u02/database/SYBO2/udump> ls -hltr -->此时的udump目录下产生了两个trace文件
- total 409K
- drwxr-xr-x 2 oracle oinstall 8.5K 2012-02-24 11:52 old
- -rw-r----- 1 oracle oinstall 4.9K 2012-02-24 11:56 sybo2_ora_6419.trc
- -rw-r----- 1 oracle oinstall 391K 2012-02-24 11:56 sybo2_ora_6422.trc
- oracle@2go-devDB01uv:/u02/database/SYBO2/udump> cat sybo2_ora_6419.trc | grep SESSION
- *** SESSION ID:(1068.6250) 2012-02-24 11:56:19.617
- oracle@2go-devDB01uv:/u02/database/SYBO2/udump> cat sybo2_ora_6422.trc | grep SESSION
- *** SESSION ID:(1078.34788) 2012-02-24 11:56:20.650
- IF ( DBMS_SESSION.is_role_enabled( 'SQL_TRACE' ) ) THEN
- DBMS_SESSION.set_identifier( UID );
- ALTER SESSION SET NLS_CALENDAR = 'GREGORIAN' NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS' NLS_NUMERIC_CHARACTERS = '.,'
