oracle编程艺术--runstst工具

runstats工具是《 oracle database 9i/10g/11g编程艺术 深入数据库体系结构》作者写的一个统计性能工具,能对做同一件事的两个方法进行比较,得到孰优孰劣的结果。

(看到runstats想到了db2 里有runstats命令收集统计信息)

runststs工具主要测量三个要素

  • 墙上时钟(wall clock) 或耗用时间(elapsed time)
  • 系统统计结果,会并排地显示每个方法做某件事(如执行一个解析调用)的次数,并展示出二者之差
  • 闩定(latch)这个是报告的关键输出

要使用该工具,需要能访问V$视图,并创建一个表来存储统计结果,还需要创建runstats包,下面是在scott用户下创建该工具,以下试验在ORACLE 11.2.0.1.0上进行

使用SYS用户登录,执行以下语句

--默认scott无创建视图权限,创建视图时会报ORA-01031: insufficient privileges
grant create view to scott;
--将以下4个动态性能视图原表SELECT权限赋给scott
grant SELECT on v_$statname to scott ;
grant SELECT on v_$mystat to scott ;
grant SELECT on v_$latch to scott ;
grant SELECT on v_$timer to scott ;

scott用户下登录,执行以下语句

--创建统计结果表
create or replace view stats
as select ‘STAT...‘ || a.name name, b.value
      from sys.v_$statname a, sys.v_$mystat b
     where a.statistic# = b.statistic#
    union all
    select ‘LATCH.‘ || name,  gets
      from sys.v_$latch
    union all
    select ‘STAT...Elapsed Time‘, hsecs from sys.v_$timer; 

--创建临时表收集统计结果
create global temporary table run_stats
(runid varchar2(15),
 name varchar2(80),
 value int)
 on commit preserve rows;  

--创建runstats包
-- runstats包含3个API,runstats测试开始时调用rs_start,rs_middle会在测试中调用,完成时调用rs_stop,打印报告
-- rs_stop的p_difference_threshold参数,用来控制最后打印的数据量,输入这个参数可只查看差值大于参数的统计结果和闩信息,默认为0全部显示

create or replace package runstats_pkg
as
       procedure rs_start;
       procedure rs_middle;
       procedure rs_stop(p_difference_threshold in number default 0);
end ;
/

create or replace package body runstats_pkg
    as
    g_start number;
    g_run1  number;
    g_run2  number; 

    --清空统计结果表,插入上一次统计结果,获取当前定时器值
    procedure rs_start
    is
    begin
       delete from run_stats; 

       insert into run_stats
       select ‘before‘, stats.* from stats;
         g_start := dbms_utility.get_cpu_time;
   end; 

   procedure rs_middle
   is
   begin
       g_run1 := (dbms_utility.get_cpu_time-g_start); 

       insert into run_stats
       select ‘after 1‘, stats.* from stats;
       g_start := dbms_utility.get_cpu_time; 

   end; 

   --打印每次运行累计CPU时间,分别打印两次运行的统计结果和闩值(只打印超过p_difference_threshold的结果)
   procedure rs_stop(p_difference_threshold in number default 0)
   is
   begin
       g_run2 := (dbms_utility.get_cpu_time-g_start); 

       dbms_output.put_line
       ( ‘Run1 ran in ‘ || g_run1 || ‘ cpu hsecs‘ );
       dbms_output.put_line
       ( ‘Run2 ran in ‘ || g_run2 || ‘ cpu hsecs‘ );
           if ( g_run2 <> 0 )
           then
       dbms_output.put_line
       ( ‘run 1 ran in ‘ || round(g_run1/g_run2*100,2) ||
         ‘% of the time‘ );
           end if;
       dbms_output.put_line( chr(9) ); 

       insert into run_stats
       select ‘after 2‘, stats.* from stats; 

       dbms_output.put_line
       ( rpad( ‘Name‘, 30 ) || lpad( ‘Run1‘, 12 ) ||
         lpad( ‘Run2‘, 12 ) || lpad( ‘Diff‘, 12 ) ); 

       for x in
       ( select rpad( a.name, 30 ) ||
                to_char( b.value-a.value, ‘999,999,999‘ ) ||
                to_char( c.value-b.value, ‘999,999,999‘ ) ||
                 to_char( ( (c.value-b.value)-(b.value-a.value)),
                                    ‘999,999,999‘ ) data
           from run_stats a, run_stats b, run_stats c
          where a.name = b.name
            and b.name = c.name
            and a.runid = ‘before‘
            and b.runid = ‘after 1‘
            and c.runid = ‘after 2‘ 

            and abs( (c.value-b.value) - (b.value-a.value) )
                  > p_difference_threshold
          order by abs( (c.value-b.value)-(b.value-a.value))
       ) loop
           dbms_output.put_line( x.data );
       end loop; 

       dbms_output.put_line( chr(9) );
       dbms_output.put_line
       ( ‘Run1 latches total versus runs -- difference and pct‘ );
       dbms_output.put_line
       ( lpad( ‘Run1‘, 12 ) || lpad( ‘Run2‘, 12 ) ||
         lpad( ‘Diff‘, 12 ) || lpad( ‘Pct‘, 10 ) ); 

       for x in
       ( select to_char( run1, ‘999,999,999‘ ) ||
                to_char( run2, ‘999,999,999‘ ) ||
                to_char( diff, ‘999,999,999‘ ) ||
                to_char( round( run1/decode( run2, 0,
                             to_number(0), run2) *100,2 ), ‘99,999.99‘ ) || ‘%‘ data
           from ( select sum(b.value-a.value) run1, sum(c.value-b.value) run2,
                         sum( (c.value-b.value)-(b.value-a.value)) diff
                    from run_stats a, run_stats b, run_stats c
                   where a.name = b.name
                    and b.name = c.name
                     and a.runid = ‘before‘
                     and b.runid = ‘after 1‘
                     and c.runid = ‘after 2‘
                     and a.name like ‘LATCH%‘
                   )
       ) loop
           dbms_output.put_line( x.data );
       end loop;
   end; 

  end;
  /

工具创建好了之后,可以拿个例子来测试一下,把下面语句写入test.sql,做成一个SQL文件,sqlplus中执行

drop table testStat;
create table testStat(id varchar2(10));
exec runstats_pkg.rs_start;
exec dbms_output.put_line(‘rs_start....‘);
insert into testStat select level from dual connect by level <=500000;
commit;
exec dbms_output.put_line(‘insert completed....‘);
exec runstats_pkg.rs_middle;
exec dbms_output.put_line(‘rs_middle....‘);
begin
  for i in 1 .. 500000
    loop
    insert into testStat values (i);
    end loop;
    commit;
   end;
   /
exec dbms_output.put_line(‘loop insert....‘);
exec runstats_pkg.rs_stop(0);

结果如下:

[[email protected] test]$ sqlplus scott/oracle@orcl @t.sql

SQL*Plus: Release 11.2.0.1.0 Production on Sat Jan 30 16:54:09 2016

Copyright (c) 1982, 2009, Oracle.  All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

Table dropped.

Table created.

PL/SQL procedure successfully completed.

rs_start....

PL/SQL procedure successfully completed.

500000 rows created.

Commit complete.

insert completed....

PL/SQL procedure successfully completed.

PL/SQL procedure successfully completed.

rs_middle....

PL/SQL procedure successfully completed.

PL/SQL procedure successfully completed.

loop insert....

PL/SQL procedure successfully completed.

Run1 ran in 66 cpu hsecs
Run2 ran in 2217 cpu hsecs
run 1 ran in 2.98% of the time

Name                                  Run1        Run2        Diff
STAT...opened cursors current           -1           0           1
STAT...redo synch writes                 2           1          -1
STAT...commit txn count during           2           3           1
STAT...IMU Flushes                       2           1          -1
STAT...rows fetched via callba           5           4          -1
STAT...cursor authentications            0           1           1
STAT...buffer is pinned count            1           2           1
STAT...parse time elapsed                1           0          -1
LATCH.channel handle pool latc           2           1          -1
LATCH.queued dump request                0           1           1
LATCH.MinActiveScn Latch                 0           1           1
LATCH.Shared B-Tree                      1           2           1
LATCH.hash table modification            1           0          -1
LATCH.SQL memory manager latch           0           1           1
LATCH.kwqbsn:qsga                        0           1           1
LATCH.threshold alerts latch             0           1           1
STAT...IMU pool not allocated            0           2           2
STAT...IMU- failed to get a pr           0           2           2
STAT...SQL*Net roundtrips to/f          10           8          -2
LATCH.ksuosstats global area             0           2           2
LATCH.dml lock allocation                2           4           2
STAT...user calls                       15          12          -3
STAT...sorts (memory)                   11           8          -3
STAT...sorts (rows)                      5           2          -3
LATCH.object stats modificatio           7           4          -3
LATCH.kcbtsemkid latch                   0           3           3
LATCH.managed standby latch              0           3           3
LATCH.parameter list                     0           3           3
LATCH.session state list latch           3           0          -3
LATCH.session switching                  1           5           4
LATCH.ksv allocation latch               0           4           4
LATCH.sort extent pool                   0           4           4
LATCH.deferred cleanup latch             0           4           4
LATCH.cp sga latch                       0           4           4
LATCH.parallel query alloc buf           1           5           4
LATCH.ncodef allocation latch            0           4           4
LATCH.qmn task queue latch               0           4           4
LATCH.ASM network state latch            0           4           4
STAT...write clones created in           0           5           5
STAT...immediate (CURRENT) blo          14           9          -5
LATCH.resmgr:active threads              0           5           5
LATCH.resmgr:schema config               0           5           5
LATCH.job_queue_processes para           0           5           5
STAT...table scans (short tabl           6          12           6
STAT...table scan blocks gotte           4          10           6
LATCH.FAL Queue                          0           6           6
LATCH.alert log latch                    0           6           6
LATCH.reservation so alloc lat           0           6           6
LATCH.transaction allocation            15           8          -7
LATCH.OS process allocation              0           9           9
LATCH.KMG MMAN ready and start           0           9           9
LATCH.Change Notification Hash           0           9           9
LATCH.Real-time plan statistic           0           9           9
STAT...redo buffer allocation            0          10          10
STAT...physical read total IO            2          13          11
STAT...physical reads                    2          13          11
STAT...physical reads cache              2          13          11
STAT...physical read IO reques           2          13          11
LATCH.cache buffer handles           1,064       1,076          12
LATCH.archive control                    0          12          12
LATCH.Reserved Space Latch               0          12          12
LATCH.session timer                      0          12          12
LATCH.kks stats                          1          15          14
LATCH.shared pool simulator             10          24          14
STAT...Heap Segment Array Upda          23           8         -15
STAT...switch current to new b          31          14         -17
STAT...calls to get snapshot s         188         171         -17
STAT...cluster key scans                51          34         -17
STAT...cluster key scan block           51          34         -17
STAT...index scans kdiixs1              88         105          17
STAT...deferred (CURRENT) bloc          32          14         -18
LATCH.FIB s.o chain latch                0          18          18
STAT...consistent changes               63          44         -19
STAT...table fetch by rowid             31          50          19
STAT...shared hash latch upgra          50          70          20
LATCH.archive process latch              0          21          21
LATCH.space background task la           0          21          21
STAT...consistent gets - exami         888         912          24
STAT...index fetch by key               52          28         -24
LATCH.FOB s.o list latch                 2          26          24
STAT...commit cleanouts                867         842         -25
STAT...commit cleanouts succes         861         836         -25
STAT...no work - consistent re         146         171          25
STAT...workarea memory allocat         -46         -21          25
LATCH.session idle bit                  35          63          28
STAT...hot buffers moved to he           0          29          29
LATCH.In memory undo latch              15          45          30
STAT...buffer is not pinned co         277         312          35
STAT...redo log space requests           0          40          40
LATCH.SGA IO buffer pool latch           2          45          43
LATCH.DML lock allocation              139          93         -46
LATCH.post/wait queue                    3          51          48
LATCH.active service list                0          51          51
LATCH.file cache latch                  46         108          62
STAT...cleanout - number of kt         744         817          73
STAT...active txn count during         743         816          73
LATCH.call allocation                    8          82          74
LATCH.active checkpoint queue            9          84          75
LATCH.session allocation                12         115         103
LATCH.ASM db client latch                2         106         104
LATCH.object queue header heap          22         132         110
LATCH.Consistent RBA                    15         129         114
LATCH.lgwr LWN SCN                      15         129         114
LATCH.mostly latch-free SCN             15         131         116
STAT...table scan rows gotten           50         176         126
LATCH.message pool operations            4         130         126
STAT...enqueue releases                280         409         129
STAT...enqueue requests                280         409         129
STAT...enqueue conversions               3         147         144
LATCH.JS queue state obj latch           0         180         180
STAT...messages sent                    14         202         188
STAT...file io wait time                66         276         210
STAT...non-idle wait count              21         251         230
STAT...redo log space wait tim           0         355         355
STAT...IMU undo allocation siz         712       1,080         368
STAT...change write time                 3         403         400
LATCH.redo writing                      49         486         437
STAT...bytes sent via SQL*Net        2,379       1,894        -485
LATCH.row cache objects              1,062       1,577         515
STAT...non-idle wait time                2         534         532
LATCH.SQL memory manager worka           7         610         603
LATCH.channel operations paren          15         694         679
LATCH.redo allocation                   52         751         699
STAT...bytes received via SQL*       3,942       3,228        -714
STAT...calls to kcmgcs               1,795       1,027        -768
STAT...consistent gets from ca       2,060       1,290        -770
STAT...consistent gets               2,060       1,290        -770
STAT...consistent gets from ca       1,087         287        -800
STAT...db block gets from cach       2,252       1,216      -1,036
LATCH.messages                          63       1,136       1,073
LATCH.enqueues                         348       1,422       1,074
LATCH.enqueue hash chains              566       1,722       1,156
STAT...recursive cpu usage               5       1,912       1,907
STAT...CPU used when call star          66       2,217       2,151
STAT...CPU used by this sessio          66       2,217       2,151
STAT...Elapsed Time                     68       2,751       2,683
STAT...DB time                          67       2,751       2,684
STAT...Heap Segment Array Inse       2,779          14      -2,765
STAT...free buffer requested         1,063       5,148       4,085
STAT...redo subscn max counts        1,021       5,124       4,103
STAT...calls to kcmgas                 300       4,409       4,109
STAT...redo ordering marks             233       4,369       4,136
LATCH.simulator lru latch              114       4,520       4,406
LATCH.undo global data               1,065       5,517       4,452
STAT...free buffer inspected             0       4,479       4,479
LATCH.cache buffers lru chain          187       5,401       5,214
LATCH.checkpoint queue latch           221       6,512       6,291
STAT...IMU Redo allocation siz         540      11,212      10,672
LATCH.object queue header oper       2,563      25,027      22,464
LATCH.simulator hash latch             949      36,561      35,612
STAT...session cursor cache hi          78      41,324      41,246
STAT...physical read bytes          16,384     106,496      90,112
STAT...cell physical IO interc      16,384     106,496      90,112
STAT...physical read total byt      16,384     106,496      90,112
STAT...HSC Heap Segment Block        2,813     500,029     497,216
STAT...redo entries                  6,853     504,649     497,796
STAT...recursive calls               1,443     501,196     499,753
STAT...execute count                   172     500,170     499,998
STAT...opened cursors cumulati         159     500,162     500,003
LATCH.shared pool                      219     500,325     500,106
STAT...session logical reads        12,488     516,953     504,465
STAT...db block gets                10,428     515,663     505,235
STAT...db block gets from cach      10,428     515,663     505,235
STAT...session pga memory          524,288    -196,608    -720,896
STAT...session uga memory          785,856    -196,464    -982,320
STAT...db block changes             10,145   1,009,164     999,019
LATCH.cache buffers chains          41,525   2,576,396   2,534,871
STAT...session pga memory max   24,772,608           0 -24,772,608
STAT...session uga memory max   24,843,448           0 -24,843,448
STAT...undo change vector size   1,210,048  34,006,444  32,796,396
STAT...redo size                 8,857,832 124,592,660 115,734,828

Run1 latches total versus runs -- difference and pct
Run1        Run2        Diff       Pct
50,679   3,171,932   3,121,253      1.60%

PL/SQL procedure successfully completed.

scott@ORCL>
时间: 2024-10-08 10:08:54

oracle编程艺术--runstst工具的相关文章

C#编程的最佳工具

C#是企业中广泛使用的编程语言,特别是那些依赖微软的程序语言.如果您使用C#构建应用程序,则最有可能使用Visual Studio,并且已经寻找了一些扩展来对您的开发进行管理.但是,这个工具列表可能会改变您编写C#代码的方式. C#编程的最佳工具有以下几类: IDE VS扩展 编译器.编辑器和序列化 反编译和代码转换工具 构建自动化和合并工具 版本控制 测试工具和VS扩展 性能分析 APM 部署自动化 容器 使用上面的链接直接跳转到特定工具,或继续阅读以浏览完整列表. IDE 1. Visual

JS DOM编程艺术的学习—— JS学习笔记2015-7-5(第76天)

由于看高程3卡住了,所以现在在学习JS DOM编程艺术来巩固基础: 一.在看JS DOM 编程艺术的时候,有个作者提到的方法很实用, 就是在JS中变量和函数的命名方法:  变量的命名方法为使用下划线,比如: var temp_color = ['red','blue']; 函数就使用驼峰法,比如: function current_date(){}; 这样的习惯可以帮助理解,提高可读性,在理解变量和函数名的时候就很方便了: 二.还有个收获就是关于“对象”的理解又加深了 之前对于什么对象的属性,对

Unix编程艺术读书笔记(一)

 Unix编程艺术读书笔记(一)这本书是一位面试官告诉我的,很感谢他的指点,关于面试的详细过程,我想以后再写,当然我也意识到自己的不足,一方面是面试的技巧,还有一方面就是学习的方法.阅读这本书很享受,加上之前的积累,一些疑惑,还有一些基础的不足,在这本书里找到了归宿. 下面,一起来享受这本书的精华吧 Unix诞生与1969年,那时候我们还在闹文革吧,可一个可以改变21世纪甚至更远的未来的操作系统问世,至少现在的Linux是基于Unix的设计哲学.Unix是唯一一个在不同种类的计算机,众多厂商

Javascript Dom编程艺术(第2版)读书笔记

Javascript Dom编程艺术这本书我看的是第2版的,真心觉得这本书不错,它将DOM编程的一些基本原则,及如何让Javascript代码在浏览器不支持的情况下,或浏览器支持了Javascript但用户禁用它的情况下做到平衡退化,让最基本的操作仍能顺利完成,以及如何让结构与样式分享做到真正的渐近增强叙述得很清楚,也很容易理解接受.我也是看了这本书之后,才对代码优化,结构,行为,样式有了进一步的加深理解. Javascript Dom编程艺术(第2版)读书笔记,布布扣,bubuko.com

【DOM编程艺术】综合示例

index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>Jay Skript And The Domsters</title> <script src="scripts/modernizr-1.6.min.js"></script> <l

《DOM编程艺术》读书笔记&lt;概述&gt;

作为一名前端开发工程师,学习的过程中总少不了各种各样的书籍,作为新手如何在众多书籍中选到适合自己的呢,我们今天先来谈谈<DOM编程艺术>这本书. 其实呢大部分书都是好书,就像LOL中大部分英雄都很厉害,只有不强悍的召唤师.先了解一本书,然后认真的读下去你一定会有所收获的.在网上搜索书单时,你大概率会看到标题中这本书,但是又会看到褒贬不一的评论,那么这本书到底适不适合入门阅读呢,且听我慢慢道来. 读完这本书的第一感觉就是这本书里面好多内容已经过时了,比如里面大量的 js 对浏览器兼容性的检查,会

R语言编程艺术(1)快速入门

这本书与手上其他的R语言参考书不同,主要从编程角度阐释R语言,而不是从统计角度.因为之前并没有深刻考虑这些,因此写出的代码往往是一条条命令的集合,并不像是"程序",因此,希望通过学习这本书,能提高编程效率,以及让自己更像是一个"程序员". 本文对应<R语言编程艺术>第1章:快速入门. 生成向量的函数c(),这里c表示"连接"(concatenate) 打印列表方式: print(listname) str(listname) 第一种将

程序员面试、算法研究、编程艺术、红黑树、数据挖掘5大系列集锦

程序员面试.算法研究.编程艺术.红黑树.数据挖掘5大系列集锦

JavaScript DOM编程艺术第一章:JavaScript简史

本系列的博客是由本人在阅读<JavaScript DOM编程艺术>一书过程中做的总结.前面的偏理论部分都是书中原话,觉得有必要记录下来,方便自己翻阅,也希望能为读到本博客的人提供一些帮助,所以 如果您也看过这本书,希望不要喷小的"抄袭",其实我也懒得敲这些文字也想简单粗暴地上代码,但是只有实践没有理论不容易自己理解,为避免误导,该"抄"的地方还是要"抄"的,哈哈~~ 一.JavaScript的起源 JavaScript是Netscap