使用 oracle 10046 event

kunlunzhiying發表於2017-09-01

描述

遇到sql語句查詢出錯的問題.Yong Huang版提示做10046事件.對這個事件以前一直是模糊概念.想理清楚,所以寫成這個文件.供以後使用.

環境

RHEL5.4 64 + 10.2.0.4

說明

10046 event 能幹什麼

目前知道這是一個系統效能分析事件,這個事件可以告訴oracle核心把相應session的詳細時間資訊輸出到trace檔案中.

10046 event level (不翻譯了,水平有限,翻了半天還是覺得原文好)

You can think of the event 10046 “level” attribute associated with an Oracle session as a 4-bit flag whose bits have the following meanings:

Level
Function
Decimal Binary
1 0001 Emit statistics for parse, execute, fetch, commit, and rollback database calls (standard sql_trace)
2 0010 Unknown
3 0100 Emit values for SQL bind variables (also called “placeholders”)
4 1000 Emit statistics for Oracle kernel internal function calls (also called “wait events”) listed in v$event_name

For example, a level-12 trace combines the effects of level-4  and level-8 tracing. Strangely, activating any non-zero tracing level also activates level-1 tracing. Therefore, tracing at levels 4, 8, and 12 are exactly equivalent to tracing at levels 5, 9, and 13, respectively: all these levels include the standard sql_trace output.

trace 檔案位置

11gR1 或 11gR1 以上版本

SQL> show parameter diagnostic_dest

11gR1以前版本

SQL> show parameter user_dump_dest

trace 檔案命名規則

tracefile 命名規則 :<ORACLE_SID>_ora_<pid>_<tracedid>.trc

其中pid為相應session所對應的OS PID,tracedid 跟session的TRACEFILE_IDENTIFIER引數相關,預設TRACEFILE_IDENTIFIER為null.

eg.

給當前session設定TRACEFILE_IDENTIFIER

ALTER SESSION SET TRACEFILE_IDENTIFIER='TOMS';
此處的"TOMS"即為trace file 命名規則中
<ORACLE_SID>_ora_<pid>_<tracedid>.trctracedid.
可以設定TRACEFILE_IDENTIFIER引數的session裡查詢V$PROCESS.TRACEID檢視tracefile_identifier的設定.

取消session標識將
tracefile_identifier置空即可.
alter session set tracefile_identifier='';

定位trace檔案(10g版本測試透過)

1.自己當前session的trace file (需要有查詢 v$mystat, v$session ,v$process 的許可權)

Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE</w:LidThemeComplexScript. MicrosoftInternetExplorer4 <style. /* Style. Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.5pt; mso-bidi-font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-font-kerning:1.0pt;} </style.

select d.value || '/' || lower(rtrim(i.instance, chr(0))) || '_ora_' ||
       p.spid || '.trc' trace_file_name
  from (select p.spid
          from v$mystat m, v$session s, v$process p
         where m.statistic# = 1
           and s.sid = m.sid
           and p.addr = s.paddr) p,
       (select t.instance
          from v$thread t, v$parameter v
         where v.name = 'thread'
           and (v.value = 0 or t.thread# = to_number(v.value))) i,
       (select value from v$parameter where name = 'user_dump_dest') d;

2.以sys使用者查詢其他session的trace file (需要知道其他session sid)

Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE</w:LidThemeComplexScript. MicrosoftInternetExplorer4 <style. /* Style. Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.5pt; mso-bidi-font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-font-kerning:1.0pt;} </style.

Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE</w:LidThemeComplexScript. MicrosoftInternetExplorer4 <style. /* Style. Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.5pt; mso-bidi-font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-font-kerning:1.0pt;} </style.

Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE</w:LidThemeComplexScript. MicrosoftInternetExplorer4 <style. /* Style. Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.5pt; mso-bidi-font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-font-kerning:1.0pt;} </style.

select d.value || '/' || lower(rtrim(i.instance, chr(0))) || '_ora_' ||
       p.spid || '.trc' trace_file_name
  from (select p.spid
          from v$session s, v$process p
         where s.sid = &sid
           and p.addr = s.paddr) p,
       (select t.instance
          from v$thread t, v$parameter v
         where v.name = 'thread'
           and (v.value = 0 or t.thread# = to_number(v.value))) i,
       (select value from v$parameter where name = 'user_dump_dest') d;

會提示輸入sid的數值.嘿嘿,就是把1給小小的改動了下.sid到v$session檢視中查詢.這裡面的trace file名字是拼出來的.所以僅僅符合trace命名規則的<ORACLE_SID>_ora_<pid>.trc部分


步驟

session級的trace

1.10046 trace 自己的 session

alter session set tracefile_identifier='10046';
alter session set timed_statistics = true;
alter session set statistics_level=all;
alter session set max_dump_file_size = unlimited;
--alter session set max_dump_file_size =
2147483647;
alter session set events '10046 trace name context forever,level 12';

-- Execute the queries or operations to be traced here --

select * from dual;
  alter session set events '10046 trace name context off';
exit;

2. dbms_system包 10046 trace 指定的session (Oracle release 8.1.6 and newer)

  conn / as sysdba
exec sys.dbms_system.set_bool_param_in_session(sid,serial#,'timed_statistics',true);
exec sys.dbms_system.set_int_param_in_session(sid,serial#,'max_dump_file_size',2147483647);
exec sys.dbms_system.set_ev(sid,serial#,10046,12,'');

-- Execute the queries or operations to be traced here --

select * from dual;

exec sys.dbms_system.set_ev(sid,serial#,10046,0,'');
exit;

3.oradebug

查詢出session的SID,SERIAL#,PID和SPID(OS PID)

  conn / as sysdba
select p.PID,p.SPID,s.SID,s.SERIAL#
from v$process p,v$session s
where s.paddr = p.addr
and s.sid = &SESSION_ID;

透過OS的PID生成10046 trace

  connect / as sysdba
oradebug setospid 9834
oradebug unlimit
oradebug event 10046 trace name context forever,level 12
oradebug event 10046 trace name context off
記得把9834換成自己查詢出來的OS PID

透過資料庫的PID生成10046 trace

  connect / as sysdba
oradebug setorapid 9834
oradebug unlimit
oradebug event 10046 trace name context forever,level 12
oradebug event 10046 trace name context off

記得把9834換成自己查詢出來的OS PID

instance級的10046 trace

Note: Please be cautious when setting system wide, as this will impact performance due to every session being traced.

修改引數檔案events引數實現instance級的10046
1.只對引數修改後新連線的session生效.
  alter system set events '10046 trace name context forever,level 12';
alter system set events '10046 trace name context off';
2.重啟資料庫後生效
  alter system set events '10046 trace name context forever,level 12' scope=spfile;
關閉也用 alter system set events '10046 trace name context off';

透過 Logon Trigger 做10046 trace

有些情況我們需要trace一個登入使用者.這時可以透過trigger完成.

  CREATE OR REPLACE TRIGGER SYS.set_trace
AFTER LOGON ON DATABASE
WHEN (USER like '&USERNAME')
DECLARE
lcommand varchar(200);
BEGIN
EXECUTE IMMEDIATE 'alter session set statistics_level=ALL';
EXECUTE IMMEDIATE 'alter session set max_dump_file_size=UNLIMITED';
EXECUTE IMMEDIATE 'alter session set events ''10046 trace name context forever, level 12''';
END set_trace;
  /

注意登入使用者必須擁有alter session許可權才能成功trace.

grant alter session to <USERNAME> ;
透過trace檔案命名規則,我們可以知道,oracle kernel只在process生命期內寫trace.所以,當session自然斷開後,trace的工作也自然停止,而不必顯式的敲入關閉trace命令.


個人總結

生成10046 trace僅僅是分析的第一步.後面還有好多分析要去學習.

疑問:

1.TRACEFILE_IDENTIFIER在某一個session中設定後,其他session怎麼才能知道設定的TRACEFILE_IDENTIFIER值? session設定TRACEFILE_IDENTIFIER後,會在trace目錄下有兩個trace檔案,一個含tracedid,一個不含.

2.dbms_system包的用法沒找到.待解決.


參考文件




如何快速獲取trace檔名

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

相關文章