在生产环境中,当数据库运行异常缓慢的时候,DBA同学们都会想冲进数据库内部看看sql到底如何运行,为何语句执行的如此缓慢?在我的生产环境中,经常有多表关联查询语句运行缓慢,多数是I/O等待的问题,因而我第一步会去看sql的执行计划是否出现了问题,其次就会用到sql trace工具来跟踪下sql的实际运行情况!
一:使用sql_trace
1:产生select语句的trace文件,一般会使用tracefile_identifier给trace文件起一个标识性的名称,便于查找
- [Oracle@dg53 ~]$ sqlplus /nolog
- SQL*Plus: Release 10.2.0.1.0 - Production on Fri Jun 8 11:53:36 2012
- Copyright (c) 1982, 2005, Oracle. All rights reserved.
- SQL> conn hr/hr
- Connected.
- SQL> alter session set tracefile_identifier='hr_trace01';
- Session altered.
- SQL> alter session set sql_trace=true;
- Session altered.
- SQL> select salary,last_name from employees where employee_id=100;
- SALARY LAST_NAME
- ---------- -------------------------
- 2000 King
- SQL> alter session set sql_trace=false;
- Session altered.
- [oracle@dg53 ~]$ cd $ORACLE_BASE/admin/orcl10g/udump/
- [oracle@dg53 udump]$ ll *hr*
- -rw-r----- 1 oracle oinstall 89149 Jun 8 11:58 dg53_ora_10498_hr_trace01.trc
2:使用tkprof工具对产生的trace文件进行过滤,抽取有用的信息,默认的trace文件输出太多信息!
sys=no代表不输出trace文件中所有sys用户的操作,包含用户sql语句引起的递归sql,使输出变的简洁;
aggragate=yes代表相同的sql语句在输入文件中做合并,使输出变的简洁;
|
[oracle@dg53 udump]$ tkprof dg53_ora_10498_hr_trace01.trc /home/oracle/trace01.log aggregate=yes sys=no explain=hr/hr [oracle@dg53 udump]$ wc -l dg53_ora_10498_hr_trace01.trc [oracle@dg53 ~]$ cat trace01.log Trace file: dg53_ora_10498_hr_trace01.trc ******************************************************************************** call count cpu elapsed disk query current rows Misses in library cache during parse: 0 call count cpu elapsed disk query current rows Misses in library cache during parse: 1 (表示该sql语句执行了硬解析,未在库缓存中命中) Rows Row Source Operation 51859) Rows Execution Plan (因为在使用tkprof分析trace文件的时候使用了explain参数,所以有执行计划输出) ******************************************************************************** call count cpu elapsed disk query current rows Misses in library cache during parse: 1 ******************************************************************************** call count cpu elapsed disk query current rows Misses in library cache during parse: 2 OVERALL TOTALS FOR ALL RECURSIVE STATEMENTS call count cpu elapsed disk query current rows Misses in library cache during parse: 15 3 user SQL statements in session. 1 session in tracefile. |
