SQL_TRACE命令能使SQL的执行过程写入到一个TRACE文件,这是性能诊断和调优过程中一种非常重要的工具也是比掌握的技能之一,下面通过一个小实验学习下该工具。
SQL> alter session set tracefile_identifier=’test’;设置trace文件的标识,便于trace文件的定位
Session altered.
SQL> alter session set sql_trace=true;启动当前session的sql_trace
Session altered.
SQL> select * from t where object_id=100; 该命令的执行过程已经写入到trace文件
ID OWNER OBJECT_NAME
―――- ―――――――――― ――――――――――
SUBOBJECT_NAME OBJECT_ID DATA_OBJECT_ID OBJECT_TYPE
―――――――――― ―――- ――――�C ――――――-
CREATED LAST_DDL_ TIMESTAMP STATUS T G S
――― ――― ――――――- ――- �C - -
1 SYS I_TYPED_VIEW1
100 100 INDEX
30-JUN-05 30-JUN-05 2005-06-30:19:10:18 VALID N N N
SQL> alter session set sql_trace=false; 关闭sql_trace
Session altered.
生成的trace文件默认放在 $ORACLE_BASE/admin/SID/udump 目录
进入udump目录,找到与test相关关键词的trace文件,利用tkprof工具转化成易于阅读的文本格式。
[oracle@prod udump]$ tkprof prod1_ora_4554_test.trc prod1_ora_4554_test.txt 转换格式
TKPROF: Release 10.2.0.1.0 �C Production on Fri Jul 13 12:49:04 2012
Copyright (c) 1982, 2005, Oracle. All rights reserved.
[oracle@prod udump]$ cat prod1_ora_4554_test.txt 格式转换成功,下面看到的就是上面执行的select语句的执行过程,格式很清爽,看着非常的舒服。
TKPROF: Release 10.2.0.1.0 �C Production on Fri Jul 13 12:49:04 2012
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Trace file: prod1_ora_4554_test.trc
Sort options: default
********************************************************************************
count = number of times OCI procedure was executed
cpu = cpu time in seconds executing
elapsed = elapsed time in seconds executing
disk = number of physical reads of buffers from disk
query = number of buffers gotten for consistent read
current = number of buffers gotten in current mode (usually for update)
rows = number of rows processed by the fetch or execute call
********************************************************************************
alter session set sql_trace=true
call count cpu elapsed disk query current rows
――- ―― ――�C ―――- ―――- ―――- ―――- ―――-
Parse 0 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 0 0.00 0.00 0 0 0 0
――- ―― ――�C ―――- ―――- ―――- ―――- ―――-
total 1 0.00 0.00 0 0 0 0
Misses in library cache during parse: 0
Optimizer mode: ALL_ROWS
Parsing user id: 61
********************************************************************************
select *
from
t where object_id=100
call count cpu elapsed disk query current rows
――- ―― ――�C ―――- ―――- ―――- ―――- ―――-
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 2 0.01 0.01 0 712 0 1
――- ―― ――�C ―――- ―――- ―――- ―――- ―――-
total 4 0.01 0.01 0 712 0 1
Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: 61
Rows Row Source Operation
――- ―――――――――――――――――
1 TABLE ACCESS FULL T (cr=712 pr=0 pw=0 time=293 us)
********************************************************************************
alter session set sql_trace=false
call count cpu elapsed disk query current rows
――- ―― ――�C ―――- ―――- ―――- ―――- ―――-
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 0 0.00 0.00 0 0 0 0
――- ―― ――�C ―――- ―――- ―――- ―――- ―――-
total 2 0.00 0.00 0 0 0 0
Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: 61
********************************************************************************
OVERALL TOTALS FOR ALL NON-RECURSIVE STATEMENTS
call count cpu elapsed disk query current rows
――- ―― ――�C ―――- ―――- ―――- ―――- ―――-
Parse 2 0.00 0.00 0 0 0 0
Execute 3 0.00 0.00 0 0 0 0
Fetch 2 0.01 0.01 0 712 0 1
――- ―― ――�C ―――- ―――- ―――- ―――- ―――-
total 7 0.01 0.02 0 712 0 1
Misses in library cache during parse: 2
OVERALL TOTALS FOR ALL RECURSIVE STATEMENTS
call count cpu elapsed disk query current rows
――- ―― ――�C ―――- ―――- ―――- ―――- ―――-
Parse 0 0.00 0.00 0 0 0 0
Execute 0 0.00 0.00 0 0 0 0
Fetch 0 0.00 0.00 0 0 0 0
――- ―― ――�C ―――- ―――- ―――- ―――- ―――-
total 0 0.00 0.00 0 0 0 0
Misses in library cache during parse: 0
3 user SQL statements in session.
0 internal SQL statements in session.
3 SQL statements in session.
********************************************************************************
Trace file: prod1_ora_4554_test.trc
Trace file compatibility: 10.01.00
Sort options: default
1 session in tracefile.
3 user SQL statements in trace file.
0 internal SQL statements in trace file.
3 SQL statements in trace file.
3 unique SQL statements in trace file.
41 lines in trace file.
54 elapsed seconds in trace file.