-- 创建 runstats 包。其中包括 3 个简单 API 调用: 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_time; end; procedure rs_middle is begin g_run1 := (dbms_utility.get_time - g_start); insert into run_stats select 'after 1',stats.* from stats; g_start := dbms_utility.get_time; end; procedure rs_stop(p_difference_threshold in number default 0) is begin g_run2 := (dbms_utility.get_time - g_start); dbms_output.put_line('Run1 ran in'||g_run1||'hsecs'); dbms_output.put_line('Run2 ran in'||g_run2||'hsecs'); dbms_output.put_line('run 1 ran in '||round(g_run1/g_run2*100,2)||'%of the time'); 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',10)|| lpad('Run2',10)|| lpad('Diff',10)); for x in ( select rpad(a.name,30)|| to_char(b.value - a.value,'9,999,999')|| to_char(c.value - b.value,'9,999,999')|| to_char(((c.value - b.value)-(b.value - a.value)),'9,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 (c.value - a.value) > 0 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',10) || lpad('Run2',10) || lpad('Diff',10) || lpad('Pct',8)); for x in ( select to_char(run1, '9,999,999')|| to_char(run2, '9,999,999')|| to_char(diff, '9,999,999')|| to_char(round(run1/run2*100,2), '999.99')||'%' data from ( select sum(b.value - a.value) run1, sum(c.value - b.value) run2, sum((c.value - b.value) - (b.value - c.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; /
create or replace view stats as select 'STAT...'||a.NAME AS name, b.VALUE as value from v$statname a, v$mystat b where a.STATISTIC# = b.STATISTIC# union all select 'latch'||name as name, gets as value from v$latch;
创建一张存储这些统计信息的小表: create global temporary table run_stats ( runid varchar2(15), name varchar2(80), value int ) on commit preserve rows;
使用测试:
set serveroutput on size 1000000; exec runstats_pkg.rs_start; exec pkg_mv_test.PR_MV_ENTRANCE; exec runstats_pkg.rs_middle; exec pkg_mv_test.PR_MV_ENTRANCE; exec runstats_pkg.rs_stop(100000);
========================================== Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 Connected as zhangxsh SQL> set serveroutput on size 1000000; SQL> exec runstats_pkg.rs_start; PL/SQL procedure successfully completed SQL> exec pkg_mv_test.PR_MV_ENTRANCE; PL/SQL procedure successfully completed SQL> exec runstats_pkg.rs_middle; PL/SQL procedure successfully completed SQL> exec pkg_mv_test.PR_MV_ENTRANCE; SQL> exec pkg_mv_test.PR_MV_ENTRANCE; PL/SQL procedure successfully completed SQL> exec runstats_pkg.rs_stop; Run1 ran in5848hsecs Run2 ran in3687hsecs run 1 ran in 158.61%of the time Name Run1 Run2 Diff STAT...cleanouts only - consis 175 176 1 STAT...transaction rollbacks 3 2 -1 STAT...immediate (CR) block cl 175 176 1 STAT...commit txn count during 368 369 1 STAT...commit batch/immediate 3 2 -1 STAT...commit immediate reques 3 2 -1 STAT...commit batch/immediate 3 2 -1 STAT...commit immediate perfor 3 2 -1 STAT...parse time cpu 6 5 -1 latchksuosstats global area 4 3 -1 latchobject stats modification 4 5 1 latchsort extent pool 7 8 1 latchktm global data 11 12 1 latchparameter list 0 1 1 latchJS mem alloc latch 1 0 -1 latchJS queue access latch 1 0 -1 STAT...redo synch writes 55,975 55,973 -2 STAT...session cursor cache co 2 0 -2 STAT...bytes sent via SQL*Net 2,120 2,118 -2 STAT...bytes received via SQL* 2,452 2,450 -2 latchobject queue header heap 7 9 2 latchcommit callback allocatio 0 2 2 latchjob_queue_processes param 3 1 -2 latchthreshold alerts latch 2 0 -2 STAT...immediate (CURRENT) blo 56,183 56,180 -3 STAT...parse time elapsed 8 5 -3 latchparameter table allocatio 5 2 -3 latchbuffer pool 3 0 -3 latchtransaction allocation 13 10 -3 latchhash table modification l 3 0 -3 STAT...user commits 111,948 111,944 -4 STAT...redo buffer allocation 15 11 -4 latchFOB s.o list latch 9 5 -4 latchFIB s.o chain latch 4 0 -4 latchparallel query alloc buff 8 4 -4 STAT...redo log space requests 16 11 -5 STAT...workarea executions - o 9 14 5 STAT...parse count (hard) 38 33 -5 latchmessage pool operations p 10 15 5 latchJS slv state obj latch 5 0 -5 STAT...user I/O wait time 9 3 -6 STAT...sorts (memory) 3 9 6 latchOS process 0 6 6 latchsession timer 22 16 -6 STAT...data blocks consistent 1,045 1,038 -7 STAT...rollbacks only - consis 1,045 1,038 -7 STAT...rows fetched via callba 40 32 -8 STAT...cursor authentications 33 25 -8 latchKMG MMAN ready and startu 20 12 -8 STAT...physical read total IO 27 18 -9 STAT...physical reads 27 18 -9 STAT...physical reads cache 27 18 -9 STAT...physical read IO reques 27 18 -9 STAT...write clones created in 82 72 -10 STAT...consistent changes 1,081 1,070 -11 STAT...application wait time 1 13 12 STAT...table fetch continued r 23 11 -12 latchresmgr group change latch 8 22 14 STAT...sorts (rows) 3,538 3,553 15 latchOS process allocation 20 35 15 latchpost/wait queue 57 40 -17 STAT...cluster key scans 182 163 -19 latchdummy allocation 4 23 19 latchresmgr:actses change grou 2 21 19 latchresmgr:actses active list 4 23 19 latchresmgr:free threads list 4 23 19 latchMemory Management Latch 20 0 -20 latchevent group latch 0 21 21 latchglobal KZLD latch for mem 0 21 21 STAT...opened cursors current 20 -2 -22 STAT...CR blocks created 1,126 1,104 -22 latchprocess allocation 0 23 23 latchprocess group creation 0 23 23 latchOS process: request alloc 0 23 23 latchloader state object freel 48 24 -24 STAT...table scans (short tabl 104 79 -25 latchchannel handle pool latch 3 28 25 latchactive checkpoint queue l 500 527 27 STAT...shared hash latch upgra 261 233 -28 STAT...index scans kdiixs1 85 57 -28 STAT...workarea memory allocat -3 26 29 latchactive service list 148 177 29 STAT...redo synch time 2 33 31 STAT...index fetch by key 172,087 172,056 -31 latchsession state list latch 8 41 33 latchfile cache latch 134 169 35 latchlibrary cache pin allocat 50 89 39 STAT...cluster key scan block 383 343 -40 latchcompile environment latch 41 83 42 latchuser lock 8 54 46 latchlibrary cache lock alloca 61 107 46 latchPL/SQL warning settings 25 77 52 latchkks stats 50 107 57 latchlibrary cache load lock 64 122 58 latchsequence cache 3 68 65 latchclient/application info 34 110 76 STAT...opened cursors cumulati 371 280 -91 STAT...session cursor cache hi 353 244 -109 STAT...parse count (total) 415 296 -119 latchchannel operations parent 534 656 122 STAT...redo log space wait tim 475 346 -129 STAT...change write time 277 146 -131 STAT...table fetch by rowid 4,168 4,018 -150 latchlist of block allocation 245 80 -165 latchJS queue state obj latch 486 252 -234 latchundo global data 546,976 547,262 286 STAT...calls to get snapshot s 281,447 281,127 -320 latchenqueues 2,432 2,063 -369 STAT...buffer is not pinned co 12,813 12,427 -386 STAT...IMU- failed to get a pr 8,907 8,493 -414 STAT...IMU pool not allocated 8,907 8,493 -414 STAT...cleanout - number of kt 1,566 1,133 -433 STAT...active txn count during 1,389 956 -433 latchSQL memory manager workar 1,303 868 -435 STAT...consistent gets - exami 342,647 342,116 -531 STAT...recursive cpu usage 1,704 1,003 -701 STAT...CPU used when call star 1,928 1,203 -725 STAT...CPU used by this sessio 1,928 1,203 -725 STAT...DB time 2,585 1,703 -882 STAT...hot buffers moved to he 1,208 0 -1,208 STAT...switch current to new b 1,264 0 -1,264 latchsession idle bit 263 1,700 1,437 STAT...calls to kcmgcs 2,339 803 -1,536 latchcache buffer handles 3,034 1,255 -1,779 latchsession allocation 7,506 9,570 2,064 latchlibrary cache lock 2,477 4,939 2,462 STAT...redo ordering marks 4,352 1,783 -2,569 STAT...rollback changes - undo 2,700 4 -2,696 latchcheckpoint queue latch 13,708 10,933 -2,775 STAT...calls to kcmgas 119,054 115,175 -3,879 STAT...free buffer requested 13,965 7,869 -6,096 STAT...table scan blocks gotte 118,393 112,276 -6,117 STAT...no work - consistent re 237,863 231,521 -6,342 STAT...consistent gets 582,789 575,657 -7,132 STAT...consistent gets from ca 582,789 575,657 -7,132 latchConsistent RBA 74,764 66,472 -8,292 latchmostly latch-free SCN 74,813 66,486 -8,327 latchlgwr LWN SCN 74,769 66,436 -8,333 STAT...messages sent 74,770 66,436 -8,334 latchcache buffers lru chain 41,576 30,955 -10,621 STAT...free buffer inspected 13,321 0 -13,321 latchobject queue header opera 56,580 36,948 -19,632 latchmessages 221,645 198,144 -23,501 latchredo writing 225,013 199,935 -25,078 latchredo allocation 441,395 415,945 -25,450 latchsimulator lru latch 108,398 78,460 -29,938 latchsimulator hash latch 110,063 79,545 -30,518 STAT...deferred (CURRENT) bloc 113,306 56,136 -57,170 STAT...physical read bytes 221,184 147,456 -73,728 STAT...physical read total byt 221,184 147,456 -73,728 STAT...IMU Flushes 103,035 3,033 -100,002 STAT...IMU commits 0 100,415 100,415 STAT...enqueue releases 336,854 224,702 -112,152 STAT...enqueue requests 336,854 224,702 -112,152 STAT...commit cleanouts succes 282,507 169,224 -113,283 STAT...commit cleanouts 282,514 169,231 -113,283 STAT...execute count 392,218 224,189 -168,029 STAT...recursive calls 956,477 787,573 -168,904 latchIn memory undo latch 669,714 466,685 -203,029 latchdml lock allocation 448,392 224,432 -223,960 latchenqueue hash chains 675,343 451,005 -224,338 STAT...buffer is pinned count 277,659 1,769 -275,890 STAT...redo entries 607,160 307,006 -300,154 STAT...db block gets from cach 1,041,178 735,660 -305,518 STAT...db block gets 1,041,178 735,660 -305,518 STAT...session logical reads 1,623,967 1,311,317 -312,650 STAT...session uga memory 392,880 65,464 -327,416 latchlibrary cache 792,505 462,068 -330,437 latchlibrary cache pin 788,056 455,048 -333,008 STAT...session uga memory max 516,332 138,512 -377,820 latchshared pool 705,607 233,465 -472,142 STAT...db block changes 1,448,639 852,288 -596,351 STAT...session pga memory 589,824 -131,072 -720,896 STAT...table scan rows gotten 9,698,389 8,915,734 -782,655 latchrow cache objects 1,707,517 30,186-1,677,331 latchcache buffers chains 6,709,233 4,968,781-1,740,452 STAT...session pga memory max 2,752,512 0-2,752,512 STAT...undo change vector size############################## STAT...IMU undo allocation siz############################## STAT...redo size ############################## STAT...IMU Redo allocation siz########## 1,031,880########## ==========================================