Welcome 微信登录

首页 / 数据库 / MySQL / 在Oracle中利用SQL_TRACE跟踪SQL的执行

当你在执行一条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)
  • 1
  • 2
  • 下一页
Oracle Database 中关于null值的存储VMWare8中Oracle Linux 5.8 64位下成功安装Oracle 10.2.0.1 64 位相关资讯      SQL语句 
  • 如何定位SQL语句在共享池里用到了  (03月17日)
  • Java 注解入门 自动生成SQL语句  (07/28/2015 16:08:34)
  • Oracle 通过sql profile为sql语句  (05/03/2015 19:43:07)
  • MySQL 存储过程动态执行sql语句  (10/13/2015 19:10:08)
  • 画图解释 SQL join 语句  (07/17/2015 15:16:27)
  • MySQL数据库sql语句调优  (03/21/2015 17:42:45)
本文评论 查看全部评论 (0)
表情: 姓名: 字数