作用:格式化跟踪文件,例如格式化sql trace,10046产生的输出文件。
1)TKPROF的参数:
不输入任何参数,直接输入tkprof,回车,可以获得一个完整的参数列表.
C:\>tkprof
Usage: tkprof tracefile outputfile [explain= ] [table= ]
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_FILE_NAME
--------------------------------------------------------------------------------
f:\app\administrator\diag\rdbms\orcl\orcl\trace/orcl_ora_3040.trc
--使用tkprof分析trace文件
C:\Documents and Settings\Administrator>tkprof
--tkprocf输出了以下文件:f:\4020.trc.txt
TKPROF: Release 11.1.0.6.0 - Production on 星期一 3月 29 13:50:42 2010
Copyright (c) 1982, 2007, Oracle.
Trace file: f:\4020.trc
Sort options: fchela
********************************************************************************
count
cpu
elapsed
disk
query
current
rows
********************************************************************************
OVERALL TOTALS FOR ALL NON-RECURSIVE STATEMENTS
call
------- ------
Parse
Execute
Fetch
------- ------
total
Misses in library cache during parse: 2
Misses in library cache during execute: 1
OVERALL TOTALS FOR ALL RECURSIVE STATEMENTS
call
------- ------
Parse
Execute
Fetch
------- ------
total
Misses in library cache during parse: 2
Misses in library cache during execute: 1
********************************************************************************
Trace file: f:\4020.trc
Trace file compatibility: 11.01.00
Sort options: fchela