Chinaunix首页 | 论坛 | 博客
  • 博客访问: 394893
  • 博文数量: 58
  • 博客积分: 1136
  • 博客等级: 少尉
  • 技术积分: 945
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 10:07
个人简介

彪悍人生无需过多解释!

文章分类

全部博文(58)

文章存档

2013年(4)

2012年(16)

2011年(38)

分类:

2012-03-15 17:12:08

使用Tkprof分析ORACLE跟踪文件

    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 ]
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来执行语句。对于insert、update、delete操作,这步会修改数据,对于select操作,这步就只是确定选择的记录。
Fetch:返回查询语句中所获得的记录,这步只有select语句会被执行。
COUNT:这个语句被parse、execute、fetch的次数。
CPU:这个语句对于所有的parse、execute、fetch所消耗的cpu的时间,以秒为单位。
ELAPSED:这个语句所有消耗在parse、execute、fetch的总的时间。
DISK:从磁盘上的数据文件中物理读取的块的数量。一般来说更想知道的是正在从缓存中读取的数据而不是从磁盘上读取的数据。
QUERY:在一致性读模式下,所有parse、execute、fetch所获得的buffer的数量。一致性模式的buffer是用于给一个长时间运行的事务提供一个一致性读的快照,缓存实际上在头部存储了状态。
CURRENT:在current模式下所获得的buffer的数量。一般在current模式下执行insert、update、delete操作都会获取buffer。在current模式下如果在高速缓存区发现有新的缓存足够给当前的事务使用,则这些buffer都会被读入了缓存区中。
ROWS: 所有SQL语句返回的记录数目,但是不包括子查询中返回的记录数目。对于select语句,返回记录是在fetch这步,对于insert、update、delete操作,返回记录则是在execute这步。
   Tkprof的使用步骤基本上遵循以下几步:
1、设置TIMED_STATISTICS为True,可以在会话级别,也可以在实例级别。
会话级:
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;
下面两个例子说明一下具体的用法:
1)
SQL> conn
;
已连接。
--启用SQL_TRACE
SQL> alter session set sql_trace=true;
会话已更改。
SQL> select count(*) from jf2;
  COUNT(*)
----------
  20806440
SQL> select count(*) from jf22 where aab301 like '4401%';
  COUNT(*)
----------
   7584848
--启用timed_statistics
SQL> alter session set timed_statistics=true;
会话已更改。
SQL> select count(*) from jf2;
  COUNT(*)
----------
  20806440
--关闭SQL_TRACE
SQL> alter session set sql_trace =false;
会话已更改。
--查询此会话产生的TRACE文件
SQL> select username,sid,serial# from v$session where username='GDSBDW';
USERNAME                              SID    SERIAL#
------------------------------ ---------- ----------
GDSBDW                                 37      27544
SQL> select 'dss_ora_'||spid||'.trc' from v$process where addr = (select paddr f
rom v$session where sid=37);
'DSS_ORA_'||SPID||'.TRC'
------------------------
dss_ora_2432.trc
--使用tkprof分析trace文件
E:\Documents and Settings\Administrator>tkprof E:\oracle\admin\dss\udump\dss_ora
_2432.trc e:\dss_ora_2432.trc.txt aggregate=yes sys=no waits=yes sort=fchela
--tkprocf输出了以下文件:e:\dss_ora_2432.trc.txt 
*******************************************************************************
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
*******************************************************************************
select count(*) from jf2
call    count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse       2      0.00       0.00          0          0          0           0
Execute     2      0.01       1.57          0         12          0           0
Fetch       4      0.12     242.67          0          0          0           2
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total       8      0.14     244.24          0         12          0           2
Misses in library cache during parse: 0
Optimizer goal: CHOOSE
Parsing user id: 63 
Rows     Row Source Operation
-------  ---------------------------------------------------
      1  SORT AGGREGATE
      0   SORT AGGREGATE
      0    TABLE ACCESS FULL JF2
*******************************************************************************
select count(*) from jf22 where aab301 like '4401%'
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     24.42      62.50      19012      19012          0           1
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total       4     24.42      62.50      19012      19012          0           1
Misses in library cache during parse: 1
Optimizer goal: CHOOSE
Parsing user id: 63 
Rows     Row Source Operation
-------  ---------------------------------------------------
      1  SORT AGGREGATE
7584848   INDEX RANGE SCAN AAB301_INDX (object id 92712)
*******************************************************************************
OVERALL TOTALS FOR ALL NON-RECURSIVE STATEMENTS
call    count       cpu    elapsed       disk      query    current        rows
------- ------  -------- ---------- ---------- ---------- ----------  ----------
Parse       4      0.00       0.00          0          0          0           0
Execute     4      0.01       1.58          0         12          0           0
Fetch       4     24.54     305.17      19012      19012          0           3
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total      12     24.56     306.76      19012      19024          0           3
Misses in library cache during parse: 3
OVERALL TOTALS FOR ALL RECURSIVE STATEMENTS
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
------- ------  -------- ---------- ---------- ---------- ----------  ----------
total       0      0.00       0.00          0          0          0           0
Misses in library cache during parse: 0
    4  user  SQL statements in session.
    0  internal SQL statements in session.
    4  SQL statements in session.
*******************************************************************************
Trace file: E:\oracle\admin\dss\udump\dss_ora_2432.trc
Trace file compatibility: 9.02.00
Sort options: fchela 
       1  session in tracefile.
       4  user  SQL statements in trace file.
       0  internal SQL statements in trace file.
       4  SQL statements in trace file.
       4  unique SQL statements in trace file.
      83  lines in trace file.
--解释输出的文件
第2个语句:select count(*) from jf22 where aab301 like '4401%'
分析1次;执行1次;取数2行;总CPU是24.42秒;处理用去62.50秒;从磁盘读19012数据块;从SGA中得到19012(19012+0) ;返回1行。表使用了索引AAB301_INDX。
2)分析其它会话的例子(转载):
先从os上利用top命令找到当前占用cpu资源最高的一个进程的PID号:14483
然后在数据库中根据PID号找到相应的sid号和serial#:
SQL> select s.sid,s.serial# from v$session s,v$process p
  2  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 9.2.0.4.0 - Production on Sun Dec 5 22:27:28 2004
Copyright (c) 1982, 2002, Oracle Corporation.  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

阅读(762) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~