【最佳化】10046事件之生成跟蹤檔案

不一樣的天空w發表於2016-10-15

10046事件:

110046事件分類

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

Level 1:跟蹤sql語句,包括解析、執行、提取、提交和回滾等;等同於SQL_TRACE 的功能

Level 4:在Level 1的基礎上增加收集繫結變數的資訊

Level 8:在Level 1 的基礎上增加等待事件的資訊

Level 12:等同於Level 4+Level 8, 即同時收集繫結變數資訊和等待事件資訊。

 

2)使用10046前提條件

SYS@ORA11GR2>show parameter timed_statistics --是否收集與時間相關的統計資訊

 

NAME                                 TYPE        VALUE

------------------------------------ ----------- ------------------

timed_statistics                     boolean     TRUE(預設)


SYS@ORA11GR2>show parameter statistics_level--此引數不允許為basic

 

NAME                                 TYPE        VALUE

------------------------------------ ----------- ------------------

statistics_level                     string      TYPICAL(預設)

SYS@ORA11GR2>show parameter max_dump_file_size--注意此引數,如果trace檔案過於大的話,此引數要設定為unlimited(預設)

 

NAME                                 TYPE        VALUE

------------------------------------ ----------- ------------------

max_dump_file_size                   string      unlimited(預設)

SYS@ORA11GR2>

 

3)非繫結變數實驗:

——建立測試表

SYS@ORA11GR2>create table t(id number(5) primary key,create_date date default sysdate not null);

 

Table created.

 

——開啟10046事件(收集一條或多條sql執行計劃的執行狀況)

SYS@ORA11GR2>alter session set events '10046 trace name context forever,level 12';

 

Session altered.

 

——對t表進行操作:

       
  begin
  for i in 21..40 loop
  execute immediate'insert into t(id) values('||i||')';
  end loop;
  end;
  /

 

PL/SQL procedure successfully completed.

 

——關閉10046事件

SYS@ORA11GR2>alter session set events '10046 trace name context off';

 

Session altered.

 

——查詢跟蹤檔案位置:

SELECT c.value || '/' || d.instance_name || '_ora_' || a.spid || '.trc' trace
    FROM v$process a, v$session b, v$parameter c, v$instance d
   WHERE a.addr = b.paddr
        AND b.audsid = userenv('sessionid')
        AND c.name = 'user_dump_dest';

 

TRACE

-------------------------------------------------------------------

/u01/app/oracle/diag/rdbms/ora11gr2/ORA11GR2/trace/ORA11GR2_ora_18140.trc

/u01/app/oracle/diag/rdbms/ora11gr2/ORA11GR2/trace/ORA11GR2_ora_10479.trc

/u01/app/oracle/diag/rdbms/ora11gr2/ORA11GR2/trace/ORA11GR2_ora_10495.trc

/u01/app/oracle/diag/rdbms/ora11gr2/ORA11GR2/trace/ORA11GR2_ora_14272.trc

其他的trace檔案要麼不存在,要麼不是當前生成的trace檔案;

SYS@ORA11GR2>

 

——利用tkprof工具格式化生成的trace檔案

[oracle@wang trace]$ tkprof ORA11GR2_ora_18140.trc 10046.txt

 

TKPROF: Release 11.2.0.4.0 - Development on Wed Oct 12 22:00:30 2016

 

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

 

驗證:

[oracle@wang trace]$ ls 10046.txt

10046.txt

[oracle@wang trace]$ pwd

/u01/app/oracle/diag/rdbms/ora11gr2/ORA11GR2/trace

——檢視:

[oracle@wang trace]$ cat 10046.txt

……省略……

*******************************************************************

SQL ID: 2fmndg2ajj1n7 Plan Hash: 0

 

insert into t(id)

values

(24)

 

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          1          3           1

Fetch        0      0.00       0.00          0          0          0           0

------- ------  -------- ---------- ---------- ---------- ----------

total        2      0.00       0.00          0          1          3           1

 

Misses in library cache during parse: 1

Optimizer mode: ALL_ROWS

Parsing user id: SYS   (recursive depth: 1)

Number of plan statistics captured: 1

 

Rows (1st) Rows (avg) Rows (max)  Row Source Operation

---------- ---------- ----------  ----------------------------------

         0          0          0  LOAD TABLE CONVENTIONAL  (cr=1 pr=0 pw=0 time=52 us)

 

*******************************************************************

省略其他插入值的執行計劃…………………………………………………………..

 

2)繫結變數實驗:

——開啟10046事件

SYS@ORA11GR2>alter session set events '10046 trace name context forever, level 12';

 

Session altered.

 

——進行操作生成trace檔案:

SYS@ORA11GR2>begin

  2    for i in 1..20 loop

  3      execute immediate 'insert into t(id,create_date) values(:1,:2)' using i,sysdate;

  4    end loop;

  5  end;

  6  /

 

PL/SQL procedure successfully completed.

 

——檢視trace檔案位置:

SYS@ORA11GR2>SELECT c.value || '/' || d.instance_name || '_ora_' || a.spid || '.trc' trace

  2     FROM v$process a, v$session b, v$parameter c, v$instance d

  3    WHERE a.addr = b.paddr

  4      AND b.audsid = userenv('sessionid')

  5      AND c.name = 'user_dump_dest';

 

TRACE

-------------------------------------------------------------------

/u01/app/oracle/diag/rdbms/ora11gr2/ORA11GR2/trace/ORA11GR2_ora_18140.trc

/u01/app/oracle/diag/rdbms/ora11gr2/ORA11GR2/trace/ORA11GR2_ora_10479.trc

/u01/app/oracle/diag/rdbms/ora11gr2/ORA11GR2/trace/ORA11GR2_ora_10495.trc

 

——驗證:

[oracle@wang trace]$ ls /u01/app/oracle/diag/rdbms/ora11gr2/ORA11GR2/trace/ORA11GR2_ora_18140.trc

/u01/app/oracle/diag/rdbms/ora11gr2/ORA11GR2/trace/ORA11GR2_ora_18140.trc

[oracle@wang trace]$ ls /u01/app/oracle/diag/rdbms/ora11gr2/ORA11GR2/trace/ORA11GR2_ora_10479.trc

ls: /u01/app/oracle/diag/rdbms/ora11gr2/ORA11GR2/trace/ORA11GR2_ora_10479.trc: No such file or directory

[oracle@wang trace]$ ls /u01/app/oracle/diag/rdbms/ora11gr2/ORA11GR2/trace/ORA11GR2_ora_10495.trc

ls: /u01/app/oracle/diag/rdbms/ora11gr2/ORA11GR2/trace/ORA11GR2_ora_10495.trc: No such file or directory

 

——利用tkprof工具格式化生成的trace檔案

[oracle@wang trace]$ tkprof ORA11GR2_ora_18140.trc 10046.tkf

 

TKPROF: Release 11.2.0.4.0 - Development on Wed Oct 12 22:18:17 2016

 

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

 

——檢視:

[oracle@wang trace]$ ls 10046.t

10046.tkf  10046.txt 

[oracle@wang trace]$

 

[oracle@wang trace]$ cat 10046.tkf

………………………………省略………………………………………………

*******************************************************************

begin

  for i in 1..20 loop

    execute immediate 'insert into t(id,create_date) values(:1,:2)' using i,sysdate;

  end loop;

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.00       0.00          0          0          0           1

 

Misses in library cache during parse: 1

Optimizer mode: ALL_ROWS

Parsing user id: SYS

 

Elapsed times include waiting on following events:

  Event waited on                             Times   Max. Wait  Total Waited

  ----------------------------------------   Waited  ----------  ---

  SQL*Net message to client                       1        0.00          0.00

  SQL*Net message from client                     1        0.00          0.00

*******************************************************************

SQL ID: bdsmcbhvqt7by Plan Hash: 0

 

insert into t(id,create_date)

values

(:1,:2)

 

 

call     count       cpu    elapsed       disk      query    current        rows

------- ------  -------- ---------- ---------- ---------- ----------

Parse        1      0.00       0.00          0          0          0           0

Execute     20      0.00       0.00          0          1         62          20

Fetch        0      0.00       0.00          0          0          0           0

------- ------  -------- ---------- ---------- ---------- ----------

total       21      0.00       0.00          0          1         62          20

 

Misses in library cache during parse: 1

Misses in library cache during execute: 1

Optimizer mode: ALL_ROWS

Parsing user id: SYS   (recursive depth: 1)

Number of plan statistics captured: 1

 

Rows (1st) Rows (avg) Rows (max)  Row Source Operation

---------- ---------- ----------  ----------------------------------

         0          0          0  LOAD TABLE CONVENTIONAL  (cr=1 pr=0 pw=0 time=162 us)

 

*******************************************************************

 


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

相關文章