Oracle 11g R2性能优化 tkprof

对SCOTT开启跟踪

执行一条SQL语句

对SCOTT关闭跟踪

tkprof分析trace文件

总结

参考

正文

另一篇博文总结了关于SQL TRACE工具的使用方式,但是产生的trace文件格式阅读起来并不是十分友好,为了更好的分析trace文件,Oracle也自带了一个格式化工具tkproftkprof工具用于处理原始的trace文件,合并汇总trace文件当中一些项目值,然后进行格式化,这样就使得trace文件的可读性更好。

关于tkprof的详细说明可以参考官方文档:

关于trace文件的生成可以参考另一篇博文:Oracle 11g R2性能优化 SQL TRACE

主要说明

tkprof命令位于 $ORACLE_HOME/bin/路径下,通过命令tkprof直接获取相关选项参数:

$ tkprof
Usage: tkprof tracefile outputfile [explain= ] [table= ]
[print= ] [insert= ] [sys= ] [sort= ]

关键选项

tracefile
需要格式化的trace文件

outputfile
格式化完成后的输出文件

explain=
通过给定方式连接数据库,并为每条语句生成执行计划

sort=
指定排序键来展示SQL语句

其他选项如下:

table=schema.tablename Use 'schema.tablename' with 'explain=' option. -- 指定PLAN_TABLE生成执行计划,默认为系统的PLAN_TABLE explain=user/password Connect to ORACLE and issue EXPLAIN PLAN. print=integer List only the first 'integer' SQL statements. -- 显示指定数目的SQL语句 aggregate=yes|no -- 指定是否对重复SQL语句信息合并,默认yes合并,如果指定为no,对重复SQL语句信息会独立输出一个条目 insert=filename List SQL statements and data inside INSERT statements. -- 创建一张表将每个跟踪的SQL语句的一行统计信息插入到表中 sys=no TKPROF does not list SQL statements run as user SYS. -- 指定不记录由SYS用户执行的语句 record=filename Record non-recursive statements found in the trace file. -- 指定生成不包含递归语句的SQL语句文件 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: -- 1、解析调用期间执行排序 prscnt number of times parse was called -- 解析次数 prscpu cpu time parsing -- 解析消耗CPU时间 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 -- 解析时库缓存区未命中次数 -- 2、执行调用期间执行排序 execnt number of execute was called -- 执行次数 execpu cpu time spent executing -- 执行时消耗CPU时间 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 -- 执行时库缓冲区未命中次数 -- 3、提取调用期间执行排序 fchcnt number of times fetch was called -- 提取数据次数 fchcpu cpu time spent fetching -- 提取时消耗CPU时间 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 -- 按游标解析时的userid排序

关于tkprof工具更详细的用法可以参考Oracle MOS文档:TKProf Interpretation (9i and above) (文档 ID 760786.1)

用法示例 对SCOTT开启跟踪 $ sqlplus scott/tiger SCOTT@dbabd> alter session set tracefile_identifier = 'SCOTT'; Session altered. SCOTT@dbabd> exec dbms_session.session_trace_enable(true, true, 'all_executions'); PL/SQL procedure successfully completed. 执行一条SQL语句 SCOTT@dbabd> select * from emp; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------------- ---------- ------------------- ---------- ---------- ---------- 7369 SMITH CLERK 7902 1980-12-17 00:00:00 800 20 7499 ALLEN SALESMAN 7698 1981-02-20 00:00:00 1600 300 30 7521 WARD SALESMAN 7698 1981-02-22 00:00:00 1250 500 30 7566 JONES MANAGER 7839 1981-04-02 00:00:00 2975 20 7654 MARTIN SALESMAN 7698 1981-09-28 00:00:00 1250 1400 30 7698 BLAKE MANAGER 7839 1981-05-01 00:00:00 2850 30 7782 CLARK MANAGER 7839 1981-06-09 00:00:00 2450 10 7788 SCOTT ANALYST 7566 1987-04-19 00:00:00 3000 20 7839 KING PRESIDENT 1981-11-17 00:00:00 5000 10 7844 TURNER SALESMAN 7698 1981-09-08 00:00:00 1500 0 30 7876 ADAMS CLERK 7788 1987-05-23 00:00:00 1100 20 7900 JAMES CLERK 7698 1981-12-03 00:00:00 950 30 7902 FORD ANALYST 7566 1981-12-03 00:00:00 3000 20 7934 MILLER CLERK 7782 1982-01-23 00:00:00 1300 10 14 rows selected. 对SCOTT关闭跟踪 SCOTT@dbabd> exec dbms_session.session_trace_disable(); PL/SQL procedure successfully completed. tkprof分析trace文件

执行如下命令生成格式化文件:

$ tkprof /data/app/oracle/diag/rdbms/dbabd/dbabd/trace/dbabd_ora_18629_SCOTT.trc /data/app/scott_trace.log explain=scott/tiger aggregate=yes sys=no waits=yes sort=fchela

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/074ccedba30050d0f78d455fe89360a6.html