runstats是《Oracle Database 9i/10g/11g编程艺术:深入数据库体系结构》作者编写的一个工具,能对做同一件事情的两个不同方法进行比较,得出孰优孰劣的结果。我们只需要提供两个不同的方法,余下的事情都由runstats负责。runstats只负责测量3个要素:
- 墙上时钟或耗时时间:知道墙上时钟或耗时时间很有用,不过这不是最重要的信息。
- 系统统计结果:会并排地i显示每个方法做某件事(如执行一个解析调用)的次数,并展示出两者之差
- 闩定(latching):这是这个报告的关键输出。
要使用runstats,需要能访问几个V$视图,并创建一个表来存储统计结果,还要创建runstats包。为此,需要访问4个V$表(就是那些神奇的动态性能表):V$STATNAME、V$MYSTAT和V$LATCH和V$TIMER。这四个表其实是别名,真正对象的名称应为V_$STATNAME、V_$MYSTAT、 V_$LATCH、 V_$TIMER,并且都是在sys账户下。如果其他账户要访问这四张表, 需要进行授权。
下面是安装方法:
1.设置权限
在你需要执行runstats包的用户赋予相关权限
grant SELECT on SYS.v_$statname to test ;grant SELECT on SYS.v_$mystat to test ;grant SELECT on SYS.v_$latch to test ;grant SELECT on SYS.v_$timer to test ;
2.创建视图
--在test账户下创建视图
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;
3.创建收集信息的表
--创建信息收集表create global temporary table run_stats( runid varchar2(15),name varchar2(80),value int )on commit preserve rows;
4.创建runstats包
create or replace package runstats_pkgasprocedure rs_start;procedure rs_middle;procedure rs_stop( p_difference_threshold in number default 0 );end;/
4.创建runstats包体
--创建包体create or replace package body runstats_pkgasg_start number;g_run1 number;g_run2 number;procedure rs_startisbegindelete from run_stats;insert into run_statsselect 'before', stats.* from stats;g_start := dbms_utility.get_cpu_time;end;procedure rs_middleisbeging_run1 := (dbms_utility.get_cpu_time-g_start);insert into run_statsselect 'after 1', stats.* from stats;g_start := dbms_utility.get_cpu_time;end;procedure rs_stop(p_difference_threshold in number default 0)isbeging_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 )thendbms_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_statsselect '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' ) datafrom run_stats a, run_stats b, run_stats cwhere a.name = b.nameand b.name = c.nameand 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_thresholdorder by abs( (c.value-b.value)-(b.value-a.value))) loopdbms_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' ) || '%' datafrom ( select sum(b.value-a.value) run1, sum(c.value-b.value) run2,sum( (c.value-b.value)-(b.value-a.value)) difffrom run_stats a, run_stats b, run_stats cwhere a.name = b.nameand b.name = c.nameand a.runid = 'before'and b.runid = 'after 1'and c.runid = 'after 2'and a.name like 'LATCH%')) loopdbms_output.put_line( x.data );end loop;end;end;/
5.测试runstats脚本
runstats脚本分为三步
第一.执行runstats_pkg.rs_start开始调用,然后执行你第一段sql
第二.执行完成第一段sql后面执行runstats_pkg.rs_middle,然后执行你第二段的sql
第三.执行rs_stop,完成之后就会打印出你第一段sql和第二段sql的资源使用统计信息.
下面开始测试:
第一步:
SQL> set serveroutput onSQL> exec runstats_pkg.rs_startPL/SQL procedure successfully completed.SQL> select count(*) from tobj;COUNT(*)----------14921
第二步:
SQL> exec runstats_pkg.rs_middlePL/SQL procedure successfully completed.SQL> select /*+ full(tobj) */ count(*) from tobj;COUNT(*)----------14921
第三步:
SQL> exec runstats_pkg.rs_stopRun1 ran in 1 cpu hsecsRun2 ran in 2 cpu hsecsrun 1 ran in 50% of the timeName Run1 Run2 DiffSTAT...Requests to/from client 6 7 1LATCH.threshold alerts latch 0 1 1STAT...physical write total IO 0 1 1STAT...db block gets direct 0 1 1STAT...physical writes 0 1 1STAT...physical writes direct 0 1 1STAT...physical write IO reque 0 1 1STAT...physical writes non che 0 1 1STAT...physical writes direct 0 1 1STAT...index scans kdiixs1 5 6 1STAT...sql area purged 0 1 1STAT...sql area evicted 0 1 1STAT...cursor authentications 2 1 -1STAT...parse count (failures) 0 1 1LATCH.session switching 2 1 -1LATCH.ksuosstats global area 3 2 -1LATCH.object stats modificatio 0 1 1LATCH.enqueues 1 2 1LATCH.ksv allocation latch 1 0 -1LATCH.queued dump request 0 1 1LATCH.KMG MMAN ready and start 7 8 1LATCH.cache buffers lru chain 1 2 1LATCH.active checkpoint queue 7 8 1LATCH.begin backup scn array 0 1 1LATCH.dml lock allocation 1 0 -1LATCH.KTF sga latch 0 1 1LATCH.Change Notification Hash 7 8 1LATCH.sort extent pool 1 0 -1LATCH.deferred cleanup latch 1 0 -1LATCH.shared pool sim alloc 2 1 -1LATCH.session timer 7 8 1LATCH.cp sga latch 1 0 -1LATCH.ncodef allocation latch 1 0 -1LATCH.ASM network state latch 1 0 -1STAT...DB time 3 4 1STAT...recursive cpu usage 3 1 -2STAT...free buffer requested 10 12 2STAT...calls to kcmgas 1 3 2STAT...active txn count during 0 2 2STAT...cleanout - number of kt 0 2 2STAT...lob writes unaligned 0 2 2STAT...session cursor cache co 2 0 -2STAT...parse time elapsed 1 3 2STAT...SQL*Net roundtrips to/f 5 7 2LATCH.mostly latch-free SCN 20 22 2LATCH.lgwr LWN SCN 20 22 2LATCH.Consistent RBA 20 22 2LATCH.object queue header heap 15 17 2LATCH.LGWR NS Write 2 0 -2LATCH.managed standby latch 2 0 -2LATCH.alert log latch 2 0 -2LATCH.shared pool simulator 8 6 -2LATCH.PL/SQL warning settings 3 5 2STAT...lob writes 0 3 3STAT...user calls 7 10 3STAT...index fetch by key 10 7 -3STAT...HSC Heap Segment Block 16 19 3LATCH.session allocation 4 1 -3LATCH.session idle bit 18 21 3LATCH.active service list 10 13 3LATCH.simulator hash latch 22 25 3STAT...enqueue requests 11 14 3STAT...enqueue releases 11 14 3LATCH.FAL Queue 3 0 -3LATCH.archive control 3 0 -3LATCH.loader state object free 0 4 4LATCH.row cache objects 243 247 4STAT...consistent changes 64 60 -4STAT...table scans (short tabl 0 4 4LATCH.tablespace key chain 1 6 5STAT...non-idle wait count 5 10 5LATCH.undo global data 8 13 5STAT...calls to get snapshot s 23 29 6LATCH.shared pool 263 269 6LATCH.ASM db client latch 42 48 6LATCH.space background task la 30 36 6LATCH.redo allocation 61 67 6LATCH.archive process latch 6 0 -6STAT...session cursor cache hi 11 18 7STAT...deferred (CURRENT) bloc 1 10 9STAT...rows fetched via callba 9 0 -9STAT...execute count 16 25 9STAT...calls to kcmgcs 25 35 10STAT...parse count (total) 16 26 10LATCH.DML lock allocation 3 13 10LATCH.OS process allocation 40 50 10STAT...opened cursors cumulati 13 23 10STAT...commit cleanouts 1 12 11LATCH.redo writing 97 108 11STAT...commit cleanouts succes 1 12 11STAT...consistent gets - exami 34 22 -12LATCH.object queue header oper 22 34 12STAT...recursive calls 89 106 17STAT...table fetch by rowid 31 10 -21STAT...db block gets from cach 1 22 21STAT...redo entries 18 43 25STAT...db block changes 84 111 27LATCH.enqueue hash chains 80 111 31LATCH.channel operations paren 241 277 36STAT...db block gets from cach 65 105 40STAT...db block gets 65 106 41STAT...buffer is not pinned co 73 31 -42LATCH.messages 316 362 46LATCH.checkpoint queue latch 729 833 104STAT...consistent gets from ca 104 257 153STAT...consistent gets 104 257 153STAT...no work - consistent re 61 215 154STAT...consistent gets from ca 70 235 165STAT...session logical reads 169 363 194STAT...table scan blocks gotte 0 199 199LATCH.SQL memory manager worka 1,410 1,611 201STAT...Elapsed Time 2,053 2,497 444LATCH.cache buffers chains 453 971 518STAT...bytes sent via SQL*Net 1,285 1,845 560STAT...bytes received via SQL* 1,560 2,351 791STAT...undo change vector size 4,148 5,708 1,560STAT...physical write bytes 0 8,192 8,192STAT...cell physical IO interc 0 8,192 8,192STAT...physical write total by 0 8,192 8,192STAT...redo size for direct wr 0 8,228 8,228STAT...table scan rows gotten 0 14,929 14,929STAT...redo size 5,768 21,416 15,648STAT...session pga memory max 0 917,504 917,504STAT...session pga memory 0 1,114,112 1,114,112STAT...logical read bytes from 1,384,448 2,965,504 1,581,056Run1 latches total versus runs -- difference and pct Run1 Run2 Diff Pct 4,418 5,436 1,018 81.27% PL/SQL procedure successfully completed.
RunStats是一个用于比较两种不同方法执行同一任务效率的工具。通过记录墙上时钟时间、系统统计结果及闩定次数等关键指标,帮助用户评估两种方法的性能差异。本文详细介绍了RunStats的安装步骤和使用方法。

539

被折叠的 条评论
为什么被折叠?



