当你在执行一条SQL语句非常慢的时候,你是不是想问Oracle怎么执行这条语句的呢?
Oracle提供的SQL_TRACE工具可以让你知道你执行的SQL究竟做了什么.执行的过程会被输出到trace文件中。
下面用例子来跟踪一个SQL语句的执行情况:
SQL> create table t as select rownum as id,object_name from dba_objects;
Table created.
SQL> create index t_ind on t(id);
Index created.
SQL> alter session set tracefile_identifier='mysession';
Session altered.
生成的trace文件的路径是$ORACLE_BASE/admin/SID/udump目录.
上面的语句是让生产trace文件名包括mysession,如本例中在
D:oracleproduct10.2.0adminorcludump生成了orcl_ora_5732_mysession.trc
SQL> alter session set sql_trace=true;
Session altered.
SQL> select * from t where id=123;
ID OBJECT_NAME
123 I_ACCESS1
SQL> alter session set sql_trace=false;
Session altered.
一般来讲生成的trace文件比较难读,可以用tkprof来生成一个更可读的文件.
注意tkprof是Oracle带的一个命令行工具,不是SQLPLUS命令.
在另外一个命令行中进入D:oracleproduct10.2.0adminorcludump目录
D:oracleproduct10.2.0adminorcludump>tkprof orcl_ora_5732_mysession.trc orcl_ora_5732_mysession.txt
TKPROF: Release 10.2.0.1.0 - Production on Fri Sep 14 16:59:12 2012
Copyright (c) 1982, 2005, Oracle. All rights reserved.
打开 orcl_ora_5732_mysession.txt文件,可以看到执行SQL的信息:
select *
from
t where id=123
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.04 0.30 0 2 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 2 0.00 0.00 0 4 0 1
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 4 0.04 0.30 0 6 0 1
Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: 61
Rows Row Source Operation
------- ---------------------------------------------------
1 TABLE ACCESS BY INDEX ROWID T (cr=4 pr=0 pw=0 time=86 us)
1 INDEX RANGE SCAN T_IND (cr=3 pr=0 pw=0 time=67 us)(object id 57205)