oracle session(會話) 跟蹤

abin1703發表於2017-05-22
開啟跟蹤事件


開啟10046事件並設定級別為4:alter session set events '10046 trace name context forever, level 4';


關閉10046事件:alter session set events '10046 trace name context off';


檢視尋10046生成的trace檔案
 select value from v$diag_info where name='Default Trace File';
 
 
用下面的方法對其他會話進行跟蹤:


SQL_TRACE:
獲取想要跟蹤session的sid:select distinct sid from v$mystat;
獲得需要trace會話的SID和serial#的值:select sid, serial# from v$session where sid=[SID];
啟動SQL_TRACE: execute dbms_system.set_sql_trace_in_session([SID],[seial#],true); 需要sysdba許可權
停止SQL_TRACE: execute dbms_system.set_sql_trace_in_session([SID],[seial#],false); 需要sysdba許可權
10046事件:


獲得需要trace會話的SID和serial#的值:select sid, serial# from v$session where sid=[SID];
啟動SQL_TRACE: execute dbms_monitor.session_trace_enable([SID],[seial#],waits=>true,binds=>true);
停止SQL_TRACE: exec dbms_monitor.session.trace_disable([SID],[seial#]);




其中, 10046 按照收集資訊的內容分為以下等級:
 Level 0 停用SQL跟蹤,相當於SQL_TRACE=FALSE
 Level 1 標準SQL跟蹤,相當於SQL_TRACE=TRUE
 Level 4 在level 1的基礎上增加繫結變數的資訊
 Level 8 在level 1的基礎上增加等待事件的資訊
 Level 12 在level 1的基礎上增加繫結變數和等待事件的資訊
 
 PARSING IN CURSOR #140397889619520 len=70 dep=1 uid=0 oct=3 lid=0 tim=1495445064661679 hv=3849548163 ad='887f8898' sqlid='53saa2zkr6wc3'
 cursor cursor number
 len sql 語句長度
 dep sql 語句遞迴深度
 uid user id
 oct oracle command type
 lid privilege user id
 tim timestamp,時間戳
 hv hash id
 ad sql address 地址, 用在 v$sqltext
 sqlid sql id
 
EXEC #140397889619520:c=0,e=110,p=0,cr=0,cu=0,mis=0,r=0,dep=1,og=4,plh=1514015273,tim=1495445064661771
WAIT #140397889619520: nam='db file sequential read' ela= 652 file#=1 block#=79197 blocks=1 obj#=58 tim=1495445064662479
FETCH #140397889619520:c=4998,e=783,p=1,cr=3,cu=0,mis=0,r=1,dep=1,og=4,plh=1514015273,tim=1495445064662578


 c CPU 消耗的時間
 e Elapsed time 
 p number of physical reads 物理讀的次數
 cr number of buffers retrieved for CR reads   邏輯讀的資料塊
 cu number of buffers retrieved in current mode (current 模式讀取的資料塊)
 mis cursor missed in the cache 庫快取中丟失的遊標, 硬解析次數
 r number of rows processed 處理的行數
 dep 遞迴深度
 og optimizer mode 【1:all_rows, 2:first_rows, 3:rule, 4:choose】
 plh plan hash value
 tim timestamp 時間戳




WAIT #140397889619520: nam='db file sequential read' ela= 652 file#=1 block#=79197 blocks=1 obj#=58 tim=1495445064662479




 nam an event that we waited for 等待事件
 ela 此操作消耗的時間
 p3 block 塊號
 trm timestamp 時間戳


STAT #140397883926128 id=1 cnt=1 pid=0 pos=1 obj=22 op='TABLE ACCESS BY INDEX ROWID USER$ (cr=2 pr=0 pw=0 time=29 us cost=1 size=104 card=1)'


 cnt 當前行源返回的行數
 pid parent id of this row source 當前行源的父結點 id
 pos position in explain plan 執行計劃的位置
 obj object id of row source (if this is a base object)
 op the row source access operation


例如, 執行步驟 TABLE ACCESS BY INDEX ROWID 消耗的邏輯讀為 2, 物理讀為 0, 耗費的時間為 29 us, 成本 cost 1,返回 1 條記錄


使用 tkprof 命令翻譯 trace 檔案


我們也可以使用 tkprof 命令對 trace 檔案進行翻譯,得到一個容易理解的 trace 彙總報表檔案


tkprof 1.txt 1.out


注意:Misses in library cache during parse: 1 意思是解析的時候庫快取丟失遊標,  也就是說發生了一次硬解析

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

相關文章