10046事件跟蹤會話sql

張衝andy發表於2016-12-20

背景知識:

10046 事件按照收集資訊內容,可以分成4個級別:

Level 1: 等同於SQL_TRACE 的功能
Level 4: 在Level 1的基礎上增加收集繫結變數的資訊
Level 8: 在Level 1 的基礎上增加等待事件的資訊
Level 12:等同於Level 4+Level 8, 即同時收集繫結變數資訊和等待事件資訊。

一: 跟蹤當前會話sql

1. sys使用者給執行跟蹤dblink使用者授權
SQL> grant alter session to dblink;
Grant succeeded.

2. 返回dblink使用者操作 
SQL> show user;
USER is "DBLINK"

3. 查詢sid,serial#
SQL> select sid,serial# from v$session where username='DBLINK';
SID SERIAL#
---------- ----------
45 14

4. 查詢當前使用者的trace檔案
SQL> select * from v$diag_info where name like 'Default%';

INST_ID NAME
---------- ----------------------------------------------------------------
VALUE
--------------------------------------------------------------------------------
1 Default Trace File
/home/oracle/app/diag/rdbms/orcl/orcl/trace/orcl_ora_2823.trc

5. 啟動10046事件
SQL> alter session set events '10046 trace name context forever, level 12';

Session altered.

6. 執行測試sql(即將被跟蹤的sql)
SQL> variable a number; #含有繃定變數的sql
SQL> exec :a:=2;
PL/SQL procedure successfully completed.

SQL> select count(*) from dba_objects where object_id=:a;

COUNT(*)
----------

7. 關閉10046事件 
SQL> alter session set events '10046 trace name context off';
Session altered.

8.1 檢視原始10046後的trace檔案 注意:10046事件的trace檔案內容是sql按時間順序執行的結果

[oracle@11g ~]$ vi /home/oracle/app/diag/rdbms/orcl/orcl/trace/orcl_ora_2823.trc

Trace file /home/oracle/app/diag/rdbms/orcl/orcl/trace/orcl_ora_2823.trc
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
ORACLE_HOME = /home/oracle/app/oracle/product/11.2.0/dbhome_1
System name: Linux
Node name: 11g
Release: 2.6.32-573.el6.x86_64
Version: #1 SMP Thu Jul 23 15:44:03 UTC 2015
Machine: x86_64
Instance name: orcl
Redo thread mounted by this instance: 1
Oracle process number: 32
Unix process pid: 2823, image: oracle@11g (TNS V1-V3)

*** 2014-11-19 04:42:30.941
*** SESSION ID:(45.14) 2014-11-19 04:42:30.941
*** CLIENT ID:() 2014-11-19 04:42:30.941
*** SERVICE NAME:(SYS$USERS) 2014-11-19 04:42:30.941
*** MODULE NAME:(SQL*Plus) 2014-11-19 04:42:30.941
*** ACTION NAME:() 2014-11-19 04:42:30.941

WAIT #2: nam='SQL*Net message to client' ela= 2 driver id=1650815232 #bytes=1 p3=0 obj#=-1 tim=1416390150940558

*** 2014-11-19 04:44:47.004
WAIT #2: nam='SQL*Net message from client' ela= 136063164 driver id=1650815232 #bytes=1 p3=0 obj#=-1 tim=1416390287004294
CLOSE #2:c=0,e=3,dep=0,type=3,tim=1416390287004441
=====================
PARSING IN CURSOR #5 len=19 dep=0 uid=90 oct=47 lid=90 tim=1416390287005001 hv=3805855218 ad='87972f88' sqlid='1w9223jdjggk'
BEGIN :a:=2; END;
END OF STMT
PARSE #5:c=0,e=467,p=0,cr=0,cu=0,mis=1,r=0,dep=0,og=1,plh=0,tim=1416390287005001
BINDS #5:
Bind#0
oacdty=02 mxl=22(22) mxlc=00 mal=00 scl=00 pre=00
oacflg=03 fl2=1000000 frm=00 csi=00 siz=24 off=0
kxsbbbfp=7fd108695a28 bln=22 avl=00 flg=05
WAIT #5: nam='SQL*Net message to client' ela= 2 driver id=1650815232 #bytes=1 p3=0 obj#=-1 tim=1416390287006240
EXEC #5:c=1999,e=1191,p=0,cr=0,cu=0,mis=1,r=1,dep=0,og=1,plh=0,tim=1416390287006261


*** 2014-11-19 04:56:00.212
WAIT #5: nam='SQL*Net message from client' ela= 673206425 driver id=1650815232 #bytes=1 p3=0 obj#=-1 tim=1416390960212735
CLOSE #5:c=0,e=49,dep=0,type=0,tim=1416390960212948
=====================
PARSING IN CURSOR #1 len=51 dep=0 uid=90 oct=3 lid=90 tim=1416390960213839 hv=3085049059 ad='87973410' sqlid='214vxnyvy4773'
select count(*) from dba_objects where object_id=:a
END OF STMT
PARSE #1:c=1000,e=844,p=0,cr=0,cu=0,mis=1,r=0,dep=0,og=1,plh=0,tim=1416390960213838
=====================
PARSING IN CURSOR #6 len=37 dep=1 uid=0 oct=3 lid=0 tim=1416390960214450 hv=1398610540 ad='9a8c2c00' sqlid='grwydz59pu6mc'
select text from view$ where rowid=:1
END OF STMT
PARSE #6:c=1000,e=408,p=0,cr=0,cu=0,mis=1,r=0,dep=1,og=4,plh=0,tim=1416390960214449
=====================
PARSING IN CURSOR #2 len=210 dep=2 uid=0 oct=3 lid=0 tim=1416390960215089 hv=864012087 ad='8a7b0300' sqlid='96g93hntrzjtr'
select /*+ rule */ bucket_cnt, row_cnt, cache_cnt, null_cnt, timestamp#, sample_size, minimum, maximum, distcnt, lowval, hival, density, col#, spare1, spare2, avgcln from hist_head$ where obj#=:1 and intcol#=:2
END OF STMT
PARSE #2:c=0,e=292,p=0,cr=0,cu=0,mis=1,r=0,dep=2,og=3,plh=0,tim=1416390960215089
BINDS #2:
Bind#0
oacdty=02 mxl=22(22) mxlc=00 mal=00 scl=00 pre=00
oacflg=08 fl2=0001 frm=00 csi=00 siz=24 off=0
kxsbbbfp=7fd108751d48 bln=22 avl=02 flg=05
value=69
Bind#1
oacdty=02 mxl=22(22) mxlc=00 mal=00 scl=00 pre=00
oacflg=08 fl2=0001 frm=00 csi=00 siz=24 off=0
kxsbbbfp=7fd108751d18 bln=24 avl=03 flg=05
value=1001
EXEC #2:c=1000,e=9315,p=0,cr=0,cu=0,mis=1,r=0,dep=2,og=3,plh=2239883476,tim=1416390960224458
FETCH #2:c=0,e=28,p=0,cr=2,cu=0,mis=0,r=0,dep=2,og=3,plh=2239883476,tim=1416390960224573
STAT #2 id=1 cnt=0 pid=0 pos=1 obj=424 op='TABLE ACCESS BY INDEX ROWID HIST_HEAD$ (cr=2 pr=0 pw=0 time=0 us)'
STAT #2 id=2 cnt=0 pid=1 pos=1 obj=426 op='INDEX RANGE SCAN I_HH_OBJ#_INTCOL# (cr=2 pr=0 pw=0 time=0 us)'
CLOSE #2:c=0,e=2,dep=2,type=3,tim=1416390960224765
=====================
PARSING IN CURSOR #5 len=210 dep=2 uid=0 oct=3 lid=0 tim=1416390960224900 hv=864012087 ad='8a7b0300' sqlid='96g93hntrzjtr'
select /*+ rule */ bucket_cnt, row_cnt, cache_cnt, null_cnt, timestamp#, sample_size, minimum, maximum, distcnt, lowval, hival, density, col#, spare1, spare2, avgcln from hist_head$ where obj#=:1 and intcol#=:2
END OF STMT
BINDS #5:
Bind#0
oacdty=02 mxl=22(22) mxlc=00 mal=00 scl=00 pre=00
oacflg=08 fl2=0001 frm=00 csi=00 siz=24 off=0
kxsbbbfp=7fd108751d48 bln=22 avl=02 flg=05
value=69
Bind#1
oacdty=02 mxl=22(22) mxlc=00 mal=00 scl=00 pre=00
oacflg=08 fl2=0001 frm=00 csi=00 siz=24 off=0
kxsbbbfp=7fd108751d18 bln=24 avl=02 flg=05
value=8
EXEC #5:c=0,e=132,p=0,cr=0,cu=0,mis=0,r=0,dep=2,og=3,plh=2239883476,tim=1416390960225016
FETCH #5:c=0,e=12,p=0,cr=2,cu=0,mis=0,r=0,dep=2,og=3,plh=2239883476,tim=1416390960225045
CLOSE #5:c=0,e=1,dep=2,type=3,tim=1416390960225068
BINDS #6:
Bind#0
oacdty=11 mxl=16(16) mxlc=00 mal=00 scl=00 pre=00
oacflg=18 fl2=0001 frm=00 csi=00 siz=16 off=0
kxsbbbfp=7fd1086aa078 bln=16 avl=16 flg=05

。。。。。 省略大量輸出

"~/app/diag/rdbms/orcl/orcl/trace/orcl_ora_2823.trc" 148L, 8943C

8.2 使用tkprof工具檢視10046時間的trace檔案
[oracle@11g ~]$ tkprof /home/oracle/app/diag/rdbms/orcl/orcl/trace/orcl_ora_2823.trc andy.txt sys=no

TKPROF: Release 11.2.0.1.0 - Development on Wed Nov 19 05:00:35 2014

Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.

[oracle@11g ~]$ vi andy.txt

from
dba_objects where object_id=:a

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 0
Fetch 2 0.00 0.00 0 5 0 1
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 4 0.01 0.00 0 5 0 1

Misses in library cache during parse: 1
Misses in library cache during execute: 1
Optimizer mode: ALL_ROWS
Parsing user id: 90

Rows Row Source Operation # 執行計劃
------- ---------------------------------------------------
1 SORT AGGREGATE (cr=5 pr=0 pw=0 time=0 us)
1 VIEW DBA_OBJECTS (cr=5 pr=0 pw=0 time=0 us cost=5 size=26 card=2)
1 UNION-ALL (cr=5 pr=0 pw=0 time=0 us)
1 FILTER (cr=5 pr=0 pw=0 time=0 us)
1 NESTED LOOPS (cr=5 pr=0 pw=0 time=0 us cost=5 size=71 card=1)
1 NESTED LOOPS (cr=4 pr=0 pw=0 time=0 us cost=4 size=67 card=1)
1 TABLE ACCESS BY INDEX ROWID OBJ$ (cr=3 pr=0 pw=0 time=0 us cost=3 size=45 card=1)
1 INDEX RANGE SCAN I_OBJ1 (cr=2 pr=0 pw=0 time=0 us cost=2 size=0 card=1)(object id 36)
1 INDEX RANGE SCAN I_USER2 (cr=1 pr=0 pw=0 time=0 us cost=1 size=22 card=1)(object id 47)
1 INDEX RANGE SCAN I_USER2 (cr=1 pr=0 pw=0 time=0 us cost=1 size=4 card=1)(object id 47)
0 TABLE ACCESS BY INDEX ROWID IND$ (cr=0 pr=0 pw=0 time=0 us cost=2 size=8 card=1)
0 INDEX UNIQUE SCAN I_IND1 (cr=0 pr=0 pw=0 time=0 us cost=1 size=0 card=1)(object id 41)
0 NESTED LOOPS (cr=0 pr=0 pw=0 time=0 us cost=2 size=29 card=1)
0 INDEX FULL SCAN I_USER2 (cr=0 pr=0 pw=0 time=0 us cost=1 size=20 card=1)(object id 47)
0 INDEX RANGE SCAN I_OBJ4 (cr=0 pr=0 pw=0 time=0 us cost=1 size=9 card=1)(object id 39)

————————————————————————————————————————————————————

二:跟蹤指定會話 (具體步驟參上面部分,這裡簡寫)

使用10046 事件跟蹤啟動trace
SQL> exec dbms_monitor.session_trace_enable(45,14,waits=>true,binds=>true)
PL/SQL procedure successfully completed.

關閉trace
SQL> exec dbms_monitor.session_trace_disable(45,14);
PL/SQL procedure successfully completed.

OK,結束。 轉載請標明出處。

10046 事件按照收集資訊內容,可以分成4個級別:

Level 1: 等同於SQL_TRACE 的功能
Level 4: 在Level 1的基礎上增加收集繫結變數的資訊
Level 8: 在Level 1 的基礎上增加等待事件的資訊
Level 12:等同於Level 4+Level 8, 即同時收集繫結變數資訊和等待事件資訊。

 


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31383567/viewspace-2131014/,如需轉載,請註明出處,否則將追究法律責任。

相關文章