tkprof查看trace文件

对特定的操作,开启trace文件跟踪
开启会话级 (针对特定操作,尽量用此级别)
SQL> alter session set sql_trace=true;
SQL> alter session set events ‘10046 trace name context forever,level 12’;
SQL> exec sys.dbms_system.set_sql_trace_in_session(10 , 39196 , true);
开启实例级(对实例的跟踪)
SQL> alter system set sql_trace=true;
关闭trace跟踪
会话级
SQL> alter session set sql_trace=false;
SQL> alter session set events ‘10046 trace name context off’;
SQL> exec sys.dbms_system.set_sql_trace_in_session(10 , 39196 , false);
实例级
SQL> alter system set sql_trace=false;


TKPROF的参数:
不输入任何参数,直接输入tkprof,回车,可以获得一个完整的参数列表.
C:>tkprof
Usage: tkprof tracefile outputfile [explain= ] [table= ]
[print= ] [insert= ] [sys= ] [sort= ]
table=schema.tablename Use ‘schema.tablename’ with ‘explain=’ option.
explain=user/password Connect to ORACLE and issue EXPLAIN PLAN.
print=integer List only the first ‘integer’ SQL statements.
aggregate=yes|no
insert=filename List SQL statements and data inside INSERT statements.
sys=no TKPROF does not list SQL statements run as user SYS.
record=filename Record non-recursive statements found in the trace file.
waits=yes|no Record summary for any wait events found in the trace file.
sort=option Set of zero or more of the following sort options:
prscnt number of times parse was called
prscpu cpu time parsing
prsela elapsed time parsing
prsdsk number of disk reads during parse
prsqry number of buffers for consistent read during parse
prscu number of buffers for current read during parse
prsmis number of misses in library cache during parse
execnt number of execute was called
execpu cpu time spent executing
exeela elapsed time executing
exedsk number of disk reads during execute
exeqry number of buffers for consistent read during execute
execu number of buffers for current read during execute
exerow number of rows processed during execute
exemis number of library cache misses during execute
fchcnt number of times fetch was called
fchcpu cpu time spent fetching
fchela elapsed time fetching
fchdsk number of disk reads during fetch
fchqry number of buffers for consistent read during fetch
fchcu number of buffers for current read during fetch
fchrow number of rows fetched
userid userid of user that parsed the cursor

2)几个重要参数的用法讲解
sys参数,如果不指定默认值为yes.这个参数的含义是,输出文件中是否包含以SYS用户运行的sql语句。这个参数还是蛮有用的,我们执行sql语句的时候,后台经常会执行很多递归的语句,比如你输入了SELECT * FROM TEST;如果这个语句是硬解析的话,那么会产生很多递归的SQL,递归的去查询表的统计信息,列的统计信息,索引的统计信息等,当然递归的不止是这些。这些递归的sql都是以SYS用户运行的,如果你不希望看到这些递归SQL,那么就加上这个参数sys=no.
record参数,它指定的是一个路径下的文件,这个文件用来生成在跟踪文件中找到的所有的非递归SQL。比如你在SQLPLUS里执行了三条语句,select * from a;select * from b;select * from c;,那么如果你指定了这个参数如:record=c:\test.log,那么你用tkprof格式化跟踪文件后,这个test.log里就会记录这三个SQL。这个特性在有些时候还是满有用的,因为跟踪文件往往都会比较大,找起来会比较费劲,我们可以通过指定这个参数先大体了解下,跟踪文件里都有哪些非递归SQL。而且这个功能还有助于我们重演SQL语句(绑定变量的不可以)。
aggregate参数,它指定tkprof是否将同样文本内容的sql聚合处理,比如,你执行了十次select * from a,如果你指定这个参数为no(默认情况),那么产生的输出文件会有十个这样语句的执行信息,如果你指定的是yes,那么tkprof会把这十次的执行信息汇总显示。这个参数怎么指定就看你的需要了,个人觉得还是满有用的一个参数。
sort参数,这个参数是经常使用到的一个参数,它用来指定tkprof输出文件里sql语句按照什么排序,默认是按照执行的先后顺序排序的,我们可以指定它按照其他方式排序,比如磁盘读取数,CPU时间等。这个参数最经常用的方式是:sort=prsela,exeela,fchela,其实这三个值加起来就是响应时间,即按照响应时间排序。这里别产生误解,tkprof会根据prsela,exeela,fchela三个值的和进行排序,而不是像SQL语句似的一个个的排序。
print参数,它经常搭配sort参数一起使用,用来指定tkprof输出sql语句的数量。这两个参数搭配使用起来就比较妙,比如你想知道一个跟踪文件里响应时间排前十的SQL,那么你就可以sort=prsela,exeela,fchela print=10来搭配使用。
explain参数,这个参数的含义是为每一个SQL提供一个执行计划。使用的方法是explain=用户名/密码,其实原理很简单,就是通过你指定的用户名,密码登陆数据库,然后为每一个sql执行以下explain plan for sql,输出到plan_table里,最后添加到输出文件里。注意,由于explain plan for 命令要求执行操作的用户要对sql语句里包含的对象都有执行权限,如果包含视图,也要对视图基于的基础表有执行权限,否则产生不了执行计划。注意增加了这个参数后,执行tkprof会比较慢。
wait参数,指定输出文件中包含不包含等待事件,默认是包含的。一般都取默认值。
基本最常用到的就这些,其他的我就不介绍了,平时这些也就基本够用了。

trace文件样例
TKPROF: Release 10.2.0.1.0 - Production on Sun Apr 22 11:00:58 2012
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Trace file: orcl_ora_4299.trc
Sort options: fchela


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


The following statements encountered a error during parse:
BEGIN :a = 99; END;

Error encountered: ORA-06550

alter session set sql_trace=off
Error encountered: ORA-00922


error connecting to database using: system/manager
ORA-01017: invalid username/password; logon denied
EXPLAIN PLAN option disabled.


select count(*)
from
t where id = :a
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.03 0.04 99 106 0 1


total 4 0.04 0.05 99 106 0 1
Misses in library cache during parse: 1
Misses in library cache during execute: 1
Optimizer mode: ALL_ROWS
Parsing user id: 55
Rows Row Source Operation


  1  SORT AGGREGATE (cr=106 pr=99 pw=0 time=46182 us)

50422 INDEX FAST FULL SCAN T_IDX (cr=106 pr=99 pw=0 time=4489223 us)(object id 52998)


OVERALL TOTALS FOR ALL NON-RECURSIVE STATEMENTS
call count cpu elapsed disk query current rows


Parse 10 0.00 0.01 0 0 0 0
Execute 12 0.01 0.03 0 0 0 3
Fetch 10 0.07 0.08 99 316 0 5


total 32 0.09 0.13 99 316 0 8
Misses in library cache during parse: 8
Misses in library cache during execute: 8
OVERALL TOTALS FOR ALL RECURSIVE STATEMENTS
call count cpu elapsed disk query current rows


Parse 47 0.03 0.05 0 0 0 0
Execute 210 0.05 0.07 0 0 0 0
Fetch 328 0.06 0.04 0 852 0 976


total 585 0.15 0.16 0 852 0 976
Misses in library cache during parse: 22
Misses in library cache during execute: 22
12 user SQL statements in session.
210 internal SQL statements in session.
222 SQL statements in session.
0 statements EXPLAINed in this session.


Trace file: orcl_ora_4299.trc
Trace file compatibility: 10.01.00
Sort options: fchela
1 session in tracefile.
12 user SQL statements in trace file.
210 internal SQL statements in trace file.
222 SQL statements in trace file.
30 unique SQL statements in trace file.
2131 lines in trace file.
130 elapsed seconds in trace file.

猜你喜欢

转载自blog.csdn.net/louisjh/article/details/79466336