热门标签 | HotTags
当前位置:  开发笔记 > 数据库 > 正文

oracle工具:tkprof简析

oracle工具:tkprof简析1定义用于分析oracle跟踪文件并且可按需产生一个更加清晰合理的输出结果的可执行工具2首选项[sql]F:\>tkprofwww.2cto.comUsage:tkproftracefileoutputfil...SyntaxHighlig

oracle工具:tkprof简析
 
  1 定义
          用于分析oracle跟踪文件并且可按需产生一个更加清晰合理的输出结果的可执行工具
    2 首选项
[sql] 
F:\>tkprof    www.2cto.com  
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    www.2cto.com  
    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  
 
              注释:
              ① fchela这是个比较有用的sort选项,在生成的.prf文件将把最消耗时间的sql放在最前面显示
              ② sys=no 以sys身份执行的sql不会被显示,增加可读性
 
    3 tkprof输出文件中各列的含义
[sql] 
call     count       cpu    elapsed       disk      query    current        rows  
------- ------  -------- ---------- ---------- ---------- ----------  ----------  
          注释:  www.2cto.com  
          ㈠ call:
             每次sql的处理都分成3个部分
             ① parse:将sql语句转成执行计划,包括检查权限、表及其关联对象是否存在等
             ② execute:由sql引擎执行,因事务类型不同而异
             ③ fetch:这步只有select语句被执行,buffer cache是主战场
          ㈡ count:
             这条sql被parse、execute和fetch的次数
          ㈢ cpu:该sql语句所消耗的cpu的时间(单位:s)
          ㈣ elapsed:该sql语句在各个阶段的执行时间
          ㈤ disk:
             从磁盘上的数据文件中物理读取的块的数量
          ㈥ query:
             在一致性读的模式下,各阶段所获得的buffer的数量。一致性模式下,buffer是用于给一个长查询提供一致性
             读的快照
          ㈦ current:
             在当前读的模式下,各阶段所获得的buffer的数量。
          ㈧ rows:
             所有的sql返回的记录数目,但不包括子查询返回的记录。
             对于select语句,返回是在fetch这步
             对于DML语句,返回是在execute这步
   www.2cto.com  
    4 tkprof的使用步骤
        ⑴ 在实例级别上设置timed_statistic=true
        SQL> show parameter timed_statistic
        
        NAME_COL_PLUS_SHOW_P TYPE                   VALUE_COL_PLUS_SHOW_
        -------------------- ---------------------- --------------------
        timed_statistics     boolean                TRUE
        ⑵ 使用各种工具得到想要查看session的trc文件
        ⑶ udump路径找到生成的trc文件
        ⑷ 对trc文件进行tkprof分析
           tkprof tracefile outfile [options]
           注意:这条语句后面没有分号结尾!!
 
    5案例
[sql] 
18:32:55 hr@ORCL (^ω^) alter session set events '10046 trace name context forever,level 12';  
  
会话已更改。  
  
18:34:00 hr@ORCL (^ω^) select count(*) from t;  
  
  COUNT(*)  
----------  
         2  
18:34:00 hr@ORCL (^ω^) update t1 set tname='think high' where id=1;  
    www.2cto.com  
已更新 1 行。  
18:34:30 hr@ORCL (^ω^) alter session set events '10046 trace name context off';  
  
会话已更改。  
  
18:34:55 hr@ORCL (^ω^) exit  
  
D:\oracle\product\10.2.0\admin\orcl\udump>tkprof orcl_ora_6004.trc C:\Users\Administrator\tkprof_starting.txt sys=no waits=yes sort=fche  
  
TKPROF: Release 10.2.0.1.0 - Production on 星期日 9月 23 19:19:13 2012  
  
Copyright (c) 1982, 2005, Oracle.  All rights reserved.  
 
    这个tkprof文件最后的一部分如下:
[sql] 
OVERALL TOTALS FOR ALL RECURSIVE STATEMENTS  
    www.2cto.com  
call     count       cpu    elapsed       disk      query    current        rows  
------- ------  -------- ---------- ---------- ---------- ----------  ----------  
Parse       11      0.03       0.16          0          0          0           0  
Execute     41      0.07       0.22          0          0          0           0  
Fetch       48      0.03       0.12          3        118          0          50  
------- ------  -------- ---------- ---------- ---------- ----------  ----------  
total      100      0.14       0.51          3        118          0          50  
  
Misses in library cache during parse: 11  
Misses in library cache during execute: 11  
  
Elapsed times include waiting on following events:  
  Event waited on                             Times   Max. Wait  Total Waited  
  ----------------------------------------   Waited  ----------  ------------  
  db file sequential read                         3        0.02          0.05  
  
    3  user  SQL statements in session.  
   41  internal SQL statements in session.  
   44  SQL statements in session.  
 
********************************************************************************  
Trace file: orcl_ora_6004.trc  
Trace file compatibility: 10.01.00  
Sort options: fchela    
       1  session in tracefile.  
       3  user  SQL statements in trace file.  
      41  internal SQL statements in trace file.  
      44  SQL statements in trace file.  
      14  unique SQL statements in trace file.  
     902  lines in trace file.  
      55  elapsed seconds in trace file.  
  www.2cto.com  
          注释:
          ① 这个session共有44条sql语句在里面,对于递归的语句一共分析了41次
          ② 计算hit ratio
             logical reads=consistent gets+ db block gets
             consistent gets=query列的execute+query列的fetch
             db block gets=current列的execute+current列的fetch
             因此,logical reads=118+0+0+0=118
             physical reads=disk列,即:3
             所以,hit ratio=L/(L+P)=97.5%
 
          ③ 还有一对参数非常重要,fetch的次数以及取得的记录的行数,一般来说,以尽可能少的fetch来取得尽可能多的记录数
             ,也就是,fetch的值要远远小于取得的记录数才好。从上面这个例子可知,fetch了48,rows 50,这表明
             有些记录是通过好几次的fetch才得到的。这个session的有些sql是需要优化的。
          ④ 从query列的parse可以看出从row cache里读取的数据
 
    这个tkprof文件中两个sql语句摘入如下:
[sql] 
********************************************************************************  
  
select count(*)   
from  
 t    www.2cto.com  
  
call     count       cpu    elapsed       disk      query    current        rows  
------- ------  -------- ---------- ---------- ---------- ----------  ----------  
Parse        1      0.01       0.06          0          0          0           0  
Execute      1      0.00       0.00          0          0          0           0  
Fetch        2      0.00       0.01          6          7          0           1  
------- ------  -------- ---------- ---------- ---------- ----------  ----------  
total        4      0.01       0.08          6          7          0           1  
  
Misses in library cache during parse: 1  
Optimizer mode: ALL_ROWS  
Parsing user id: 55    
  
Rows     Row Source Operation  
-------  ---------------------------------------------------  
      1  SORT AGGREGATE (cr=7 pr=6 pw=0 time=15493 us)  
      2   TABLE ACCESS FULL T (cr=7 pr=6 pw=0 time=15449 us)  
    www.2cto.com  
Elapsed times include waiting on following events:  
  Event waited on                             Times   Max. Wait  Total Waited  
  ----------------------------------------   Waited  ----------  ------------  
  SQL*Net message to client                       2        0.00          0.00  
  db file sequential read                         1        0.01          0.01  
  db file scattered read                          1        0.00          0.00  
  SQL*Net message from client                     2        0.00          0.00  
 
            注释:
            从上面的输出可以看到,这条语句访问了7个块来找到需要select的记录,返回1条记录。
            这个查询parse了1次,而且是hard parse 1次。
            select语句主要看fetch这一行
[sql] 
********************************************************************************  
  
update t1 set tname='think high'   
where    www.2cto.com  
 id=1  
  
call     count       cpu    elapsed       disk      query    current        rows  
------- ------  -------- ---------- ---------- ---------- ----------  ----------  
Parse        1      0.00       0.00          0          0          0           0  
Execute      1      0.00       0.03          0          5          2           1  
Fetch        0      0.00       0.00          0          0          0           0  
------- ------  -------- ---------- ---------- ---------- ----------  ----------  
total        2      0.00       0.03          0          1          1           1  
  
Misses in library cache during parse: 1  
Optimizer mode: ALL_ROWS  
Parsing user id: 55    
  
Rows     Row Source Operation  
-------  ---------------------------------------------------  
      1  UPDATE  T1 (cr=1 pr=0 pw=0 time=31186 us)  
      1   INDEX UNIQUE SCAN INDEX_T1 (cr=1 pr=0 pw=0 time=23 us)(object id 54498)  
    www.2cto.com  
Elapsed times include waiting on following events:  
  Event waited on                             Times   Max. Wait  Total Waited  
  ----------------------------------------   Waited  ----------  ------------  
  SQL*Net message to client                       1        0.00          0.00  
  SQL*Net message from client                     1        0.00          0.00  
 
            注释:
            这条语句访问了5个block来找到需要的update的记录,在执行update的时候只访问了2个,一共更新了1条记录
            DML语句主要看execute这一行

推荐阅读
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 本文介绍了如何在MySQL中将零值替换为先前的非零值的方法,包括使用内联查询和更新查询。同时还提供了选择正确值的方法。 ... [详细]
  • 本文详细介绍了MysqlDump和mysqldump进行全库备份的相关知识,包括备份命令的使用方法、my.cnf配置文件的设置、binlog日志的位置指定、增量恢复的方式以及适用于innodb引擎和myisam引擎的备份方法。对于需要进行数据库备份的用户来说,本文提供了一些有价值的参考内容。 ... [详细]
  • 使用Ubuntu中的Python获取浏览器历史记录原文: ... [详细]
  • 本文由编程笔记小编整理,介绍了PHP中的MySQL函数库及其常用函数,包括mysql_connect、mysql_error、mysql_select_db、mysql_query、mysql_affected_row、mysql_close等。希望对读者有一定的参考价值。 ... [详细]
  • 本文介绍了Oracle数据库中tnsnames.ora文件的作用和配置方法。tnsnames.ora文件在数据库启动过程中会被读取,用于解析LOCAL_LISTENER,并且与侦听无关。文章还提供了配置LOCAL_LISTENER和1522端口的示例,并展示了listener.ora文件的内容。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • Java String与StringBuffer的区别及其应用场景
    本文主要介绍了Java中String和StringBuffer的区别,String是不可变的,而StringBuffer是可变的。StringBuffer在进行字符串处理时不生成新的对象,内存使用上要优于String类。因此,在需要频繁对字符串进行修改的情况下,使用StringBuffer更加适合。同时,文章还介绍了String和StringBuffer的应用场景。 ... [详细]
  • Oracle分析函数first_value()和last_value()的用法及原理
    本文介绍了Oracle分析函数first_value()和last_value()的用法和原理,以及在查询销售记录日期和部门中的应用。通过示例和解释,详细说明了first_value()和last_value()的功能和不同之处。同时,对于last_value()的结果出现不一样的情况进行了解释,并提供了理解last_value()默认统计范围的方法。该文对于使用Oracle分析函数的开发人员和数据库管理员具有参考价值。 ... [详细]
  • MyBatis错题分析解析及注意事项
    本文对MyBatis的错题进行了分析和解析,同时介绍了使用MyBatis时需要注意的一些事项,如resultMap的使用、SqlSession和SqlSessionFactory的获取方式、动态SQL中的else元素和when元素的使用、resource属性和url属性的配置方式、typeAliases的使用方法等。同时还指出了在属性名与查询字段名不一致时需要使用resultMap进行结果映射,而不能使用resultType。 ... [详细]
  • 本文详细介绍了在ASP.NET中获取插入记录的ID的几种方法,包括使用SCOPE_IDENTITY()和IDENT_CURRENT()函数,以及通过ExecuteReader方法执行SQL语句获取ID的步骤。同时,还提供了使用这些方法的示例代码和注意事项。对于需要获取表中最后一个插入操作所产生的ID或马上使用刚插入的新记录ID的开发者来说,本文提供了一些有用的技巧和建议。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • 本文介绍了通过mysql命令查看mysql的安装路径的方法,提供了相应的sql语句,并希望对读者有参考价值。 ... [详细]
author-avatar
lydia芸小窝的天使
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有