签到成功

知道了

CNDBA社区CNDBA社区

使用 Tkprof 分析 ORACLE 跟踪文件

2016-11-25 16:02 1461 0 原创 Oracle 性能优化
作者: dave

Tkprof是一个用于分析Oracle跟踪文件并且产生一个更加清晰合理的输出结果的可执行工具。如果一个系统的执行效率比较低,一个比较好的方法是通过跟踪用户的会话并且使用Tkprof工具使用排序功能格式化输出,从而找出有问题的SQL语句。


TKPROF 命令语法:
TKPROF  filename1, filename2 [ SORT  = [ opion][,option] ]
  [  PRINT = integer ]
  [ AGGREGATE  = [ YES | NO ] ]
  [ INSERT = filename3 ]
  [ SYS = [ YES | NO ]  ]
  [  [ TABLE = schema.table ] | [ EXPLAIN = user/password ]  ]
  [  RECORD = filename ]

http://www.cndba.cn/Dave/article/1587

相关说明:
filename1  指定的输入文件,可以是多个文件联起来。
Filename2  格式化输出文件。
SORT     在输出到输出文件前,先进程排序。如果省去,则按照实际使用的顺序输出到文件中。排序选项有以下多种:
  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


PRINT        只列出输出文件的第一个integer SQL语句。默认为所有的SQL语句。
AGGREGATE    如果= NO ,则不对多个相同的SQL进行汇总。
INSERT       SQL 语句的一种,用于将跟踪文件的统计信息存储到数据库中。在TKPROF创建脚本后,在将结果输入到数据库中。
SYS         禁止或启用 将SYS用户所发布的SQL语句列表到输出文件中。
TABLE       在输出到输出文件前,用于存放临时表的用户名和表名。
EXPLAIN     对每条SQL 语句确定其执行规划。并将执行规划写到输出文件中。

其中比较有用的一个排序选项是fchela,即按照elapsed time fetching来对分析的结果排序(记住要设置初始化参数timed_statistics=true),生成的文件将把最消耗时间的sql放在最前面显示。另外一个有用的参数就是sys,这个参数设置为no可以阻止所有以sys用户执行的sql被显示出来,这样可以减少分析出来的文件的复杂度,便于查看。


Tkprof命令输出的解释:


首先解释输出文件中列的含义:
CALL:每次SQL语句的处理都分成三个部分
Parse:这步将SQL语句转换成执行计划,包括检查是否有正确的授权和所需要用到的表、列以及其他引用到的对象是否存在。
Execute这步是真正的由Oracle来执行语句。对于insertupdatedelete操作,这步会修改数据,对于select操作,这步就只是确定选择的记录。
Fetch返回查询语句中所获得的记录,这步只有select语句会被执行。
COUNT:这个语句被parseexecutefetch的次数。
CPU这个语句对于所有的parseexecutefetch所消耗的cpu的时间,以秒为单位。
ELAPSED这个语句所有消耗在parseexecutefetch总的时间。
DISK从磁盘上的数据文件中物理读取的块的数量。一般来说更想知道的是正在从缓存中读取的数据而不是从磁盘上读取的数据。
QUERY在一致性读模式下,所有parseexecutefetch所获得的buffer的数量。一致性模式的buffer是用于给一个长时间运行的事务提供一个一致性读的快照,缓存实际上在头部存储了状态。
CURRENTcurrent模式下所获得的buffer的数量。一般在current模式下执行insertupdatedelete操作都会获取buffer。在current模式下如果在高速缓存区发现有新的缓存足够给当前的事务使用,则这些buffer都会被读入了缓存区中。
ROWS: 所有SQL语句返回的记录数目,但是不包括子查询中返回的记录数目。对于select语句,返回记录是在fetch这步,对于insertupdatedelete操作,返回记录则是在execute这步。


Tkprof的使用步骤基本上遵循以下几步:
1、设置TIMED_STATISTICSTrue,可以在会话级别,也可以在实例级别。
会话级:
SQL> alter session set timed_statistics=True;
实例级:
SQL> alter system set timed_statistics=True scope=both;
2、 设置SQL_TRACE,可以在会话级,也可以在数据库级。
会话级:
SQL> alter session set sql_trace=true
或者:
SQL>EXEC DBMS_SYSTEM.SET_SQL_TRACE_IN_SESSION(SID,SERIAL#,TRUE);
实例级:
SQL> alter system set sql_trace=true scope=both;


四.举例说明:

--启用SQL_TRACE
SQL> alter session set sql_trace=true;
会话已更改。
SQL> select count(*) from bigtab;

  COUNT(*)

----------

   1922423

--启用timed_statistics
SQL> alter session set timed_statistics=true;

会话已更改。

SQL>  select count(*) from bigtab;

  COUNT(*)

----------

   1922423

SQL>  alter session set sql_trace =false;

会话已更改。


--查询此会话产生的TRACE文件
SQL> select username,sid,serial# from v$session where username='SYS';

USERNAME                              SID    SERIAL#

------------------------------ ---------- ----------

SYS                                    19       2518

http://www.cndba.cn/Dave/article/1587

SQL> select 'orcl_ora_'||spid||'.trc' from v$process where addr = (select paddr from v$session where sid=19);

'DSS_ORA_'||SPID||'.TRC'

------------------------------------

orcl_ora_7240.trc

http://www.cndba.cn/Dave/article/1587

也可以通过下面的函数来获取当前的trace文件:

create or replace function gettracename  return varchar2 is
  v_result varchar2(200);
begin
  SELECT    d.VALUE
      || '/'
      || LOWER (RTRIM (i.INSTANCE, CHR (0)))
      || '_ora_'
      || p.spid
      || '.trc' into v_result
  FROM (SELECT p.spid
          FROM v$mystat m, v$session s, v$process p
        WHERE m.statistic# = 1 AND s.SID = m.SID AND p.addr = s.paddr) p,
      (SELECT t.INSTANCE
          FROM v$thread t, v$parameter v
        WHERE v.NAME = 'thread'
          AND (v.VALUE = 0 OR t.thread# = TO_NUMBER (v.VALUE))) i,
      (SELECT VALUE
          FROM v$parameter
        WHERE NAME = 'user_dump_dest') d; 
  return v_result;
end gettracename;

运行SQL> select gettracename() from dual;即可
SQL> select gettracename() from dual;

GETTRACENAME()

--------------------------------------------------------------------------

d:/app/administrator/diag/rdbms/orcl/orcl/trace/orcl_ora_7240.trc


--使用tkprof分析trace文件
C:/Users/Administrator.DavidDai>tkprof d:/app/administrator/diag/rdbms/orcl/orcl

/trace/orcl_ora_7240.trc D:/orcl_ora_7240.txt aggregate=yes sys=no waits=yes sor

t=fchela

TKPROF: Release 11.2.0.1.0 - Development on 星期五 5月 28 16:48:49 2010http://www.cndba.cn/Dave/article/1587

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.

http://www.cndba.cn/Dave/article/1587


--tkprocf输出了以下文件:D:/orcl_ora_7240.txt 

TKPROF: Release 11.2.0.1.0 - Development on 星期五 5月 28 16:48:49 2010

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.

Trace file: d:/app/administrator/diag/rdbms/orcl/orcl/trace/orcl_ora_7240.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

********************************************************************************

OVERALL TOTALS FOR ALL NON-RECURSIVE STATEMENTShttp://www.cndba.cn/Dave/article/1587

call     count       cpu    elapsed       disk      query    current        rows

------- ------  -------- ---------- ---------- ---------- ----------  ----------

Parse        4      0.00       0.00          0          0          0           0

Execute      5      0.00       0.00          0          0          0           0

Fetch        4      0.79       7.45      57075      57082          0           2

------- ------  -------- ---------- ---------- ---------- ----------  ----------

total       13      0.79       7.45      57075      57082          0           2

Misses in library cache during parse: 3

Misses in library cache during execute: 1

OVERALL TOTALS FOR ALL RECURSIVE STATEMENTS

http://www.cndba.cn/Dave/article/1587

call     count       cpu    elapsed       disk      query    current        rows

------- ------  -------- ---------- ---------- ---------- ----------  ----------

Parse        0      0.00       0.00          0          0          0           0

Execute      0      0.00       0.00          0          0          0           0

Fetch        0      0.00       0.00          0          0          0           0

http://www.cndba.cn/Dave/article/1587

------- ------  -------- ---------- ---------- ---------- ----------  ----------

total        0      0.00       0.00          0          0          0           0

Misses in library cache during parse: 0

    5  user  SQL statements in session.

    0  internal SQL statements in session.

    5  SQL statements in session.

********************************************************************************

Trace file: d:/app/administrator/diag/rdbms/orcl/orcl/trace/orcl_ora_7240.trc

Trace file compatibility: 11.1.0.7

Sort options: fchela  

http://www.cndba.cn/Dave/article/1587

       1  session in tracefile.

       5  user  SQL statements in trace file.

       0  internal SQL statements in trace file.

       5  SQL statements in trace file.

       5  unique SQL statements in trace file.

http://www.cndba.cn/Dave/article/1587

      73  lines in trace file.

      75  elapsed seconds in trace file.


.  分析会话的示例
先从os上利用top命令找到当前占用cpu资源最高的一个进程的PID号:14483
然后在数据库中根据PID号找到相应的sid号和serial#
SQL> select s.sid,s.serial# from v$session s,v$process p where s.paddr=p.addr and p.spid='14483';
       SID    SERIAL#
---------- ----------
       101      25695


使用dbms_system.set_sql_trace_in_session包来对这个session进行trace
SQL> exec DBMS_SYSTEM.SET_SQL_TRACE_IN_SESSION(101,25695,true);
PL/SQL procedure successfully completed.


user_dump_dest定义的路径下查找刚刚最近生成的trace文件,可以根据时间来排序,找最近的trace文件,也可以根据SID_ORA_SPID.TRC的规则,即ORCL_ORA_14483.TRC找到TRACE文件。

接着使用tkprof工具对此trace文件进行格式化分析,生成分析后的trace文件。
$tkprof orcl_ora_14483.trc allan.txt explain=system/manager aggregate=yes sys=no waits=yes sort=fchela


TKPROF: Release 11.2.0.1.0 - Development on 星期五 5月 28 16:48:49 2010

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.


这里生成的allan.txt文件就是我们最终得到的格式化后的trace文件了,然后打开这个文件进行分析。
最后总的统计:
OVERALL TOTALS FOR ALL RECURSIVE STATEMENTS
call    count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------   --------
Parse     20      0.01       0.02          0         58         0           0
Execute 13197     0.81       0.90         17       7436     6316          1484
Fetch   12944    22.86      22.10         20    2205941        0          8972
------- ------  -------- ---------- ---------- ---------- ---------- --------
total    26161     23.68     23.02         37    2213435     6316       10456

版权声明:本文为博主原创文章,未经博主允许不得转载。

用户评论
* 以下用户言论只代表其个人观点,不代表CNDBA社区的观点或立场
dave

dave

关注

人的一生应该是这样度过的:当他回首往事的时候,他不会因为虚度年华而悔恨,也不会因为碌碌无为而羞耻;这样,在临死的时候,他就能够说:“我的整个生命和全部精力,都已经献给世界上最壮丽的事业....."

  • 2237
    原创
  • 2
    翻译
  • 535
    转载
  • 185
    评论
  • 访问:6530009次
  • 积分:4229
  • 等级:核心会员
  • 排名:第1名
精华文章
    最新问题
    查看更多+
    热门文章
      热门用户
      推荐用户
        Copyright © 2016 All Rights Reserved. Powered by CNDBA · 皖ICP备2022006297号-1·

        QQ交流群

        注册联系QQ