之前简单的写了 10046事件介绍的文章 ,当然收集10046 trace不是最终的目的,能够读懂并且通过分析10046 trace进而分析相应sql的性能问题才是10046 trace真正发挥作用的地方。但是10046事件所产生的原始trace文件习惯称之为裸trace文件(raw trace),Oracle记录在裸trace文件中的内容一眼看上去并不是那么观,也不是那么容易看懂。为了祼trace文件能够以一种更直观、更容易懂的方式展现出来,Oracle提供了tkprof命令,这个命令是Oracle自带的,可以用它来翻译祼trace文件。
1、tkprof的语法:
1 2 3 |
tkprof filename1 filename2 [waits= yes |no] [ sort =option] [print=n]
[aggregate= yes |no] [insert=filename3] [sys= yes |no] [table=schema.table]
[explain=user /password ] [record=filename4] [width=n]
|
最简单的使用方法是tkprof trace_filename output_filename
具体使用方法及参数含义参考官方文档
tkprof生成的输出文件中会包含一些Oracle内部调用的sql的trace记录,这些SQL是由用户的SQL触发,一般来说是查一些数据字典基表例如 obj$、tab$等,常规情况下这些SQL运行消耗的资源和时间都非常少。我们不需要关心,我们关心的就是我们运行的sql所对应的各种信息。
2、一个简单sql对应的trace经过tkprof后的示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
SQL ID: 484dcpmb3vazu Plan Hash: 2949544139
select *
from
scott.emp where empno=:x
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 2 0 1
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 4 0.00 0.00 0 2 0 1
Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id : SYS
Number of plan statistics captured: 1
Rows (1st) Rows (avg) Rows (max) Row Source Operation
---------- ---------- ---------- ---------------------------------------------------
1 1 1 TABLE ACCESS BY INDEX ROWID EMP (cr=2 pr =0 pw=0 time =38 us cost=1 size=38 card=1)
1 1 1 INDEX UNIQUE SCAN PK_EMP (cr=1 pr =0 pw=0 time =19 us cost=0 size=0 card=1)(object id 87109)
Elapsed times include waiting on following events:
Event waited on Times Max. Wait Total Waited
---------------------------------------- Waited ---------- ------------
SQL*Net message to client 2 0.00 0.00
SQL*Net message from client 2 0.01 0.01
|
3、各部分详细分析
1)第一部分是SQL ID、Plan Hash和sql文本
1 2 3 4 5 |
SQL ID: 484dcpmb3vazu Plan Hash: 2949544139
select *
from
scott.emp where empno=:x
|
2)第二部分是此次SQL运行时各项指标的统计信息的汇总
1 2 3 4 5 6 7 |
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 2 0 1
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 4 0.00 0.00 0 2 0 1
|
行名解释
列名解释
3)第三部分Library Cache information
列出了语句在解析和执行阶段时library cache miss的次数,如果语句没有library cache miss,则tkprof不显示。还列出了优化器模式等信息
1 2 3 4 |
Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id : SYS
Number of plan statistics captured: 1
|
4)第四部分为Row source plan
1 2 3 4 |
Rows (1st) Rows (avg) Rows (max) Row Source Operation
---------- ---------- ---------- ---------------------------------------------------
1 1 1 TABLE ACCESS BY INDEX ROWID EMP (cr=2 pr =0 pw=0 time =38 us cost=1 size=38 card=1)
1 1 1 INDEX UNIQUE SCAN PK_EMP (cr=1 pr =0 pw=0 time =19 us cost=0 size=0 card=1)(object id 87109)
|
第五部分:语句执行的等待事件信息
包括各个等待事件的等待次数和等待时间。
1 2 3 4 5 |
Elapsed times include waiting on following events:
Event waited on Times Max. Wait Total Waited
---------------------------------------- Waited ---------- ------------
SQL*Net message to client 2 0.00 0.00
SQL*Net message from client 2 0.01 0.01
|
官方文档:
参考文档:
MOS文档:TKProf Interpretation (9i and above) (文档 ID 760786.1)
Interpreting Raw SQL_TRACE output (文档 ID 39817.1)
来源: http://www.linuxidc.com/Linux/2017-02/140808.htm