SQL最佳化好助手:Runstats

Steven1981發表於2008-05-23

在SQL最佳化的過程中,我們有時候需要比較兩個SQL或者過程的效能.這裡推薦一個工具:RUNSTATS包

[@more@]

這個包在使用前需要自己建立,指令碼如下:
conn / as sysdba;
grant select on V_$STATNAME to &&USERNAME;
grant select on V_$MYSTAT to &USERNAME;
grant select on V_$TIMER to &USERNAME;
grant select on V_$LATCH to &USERNAME;

drop table run_stats;
create global temporary table run_stats
( runid varchar2(15),
name varchar2(80),
value int )
on commit preserve rows;

--create VIEW

create or replace view stats
as select 'STAT...' || a.name name, b.value
from v$statname a, v$mystat b
where a.statistic# = b.statistic#
union all
select 'LATCH.' || name, gets
from v$latch
union all
select 'STAT...Elapsed Time', hsecs from v$timer;

--CREATE PACKAGE
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', 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 (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', 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/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;
/

grant execute on runStats_pkg to public;

/*
exec sys.runStats_pkg.rs_start;
exec sys.runStats_pkg.rs_middle;
exec sys.runStats_pkg.rs_stop;
*/

建立成功後,我們就可以用來測試了:注意在SQLPLUS中測試前需要先set serveroutput on ,不然沒有結果輸出.

begin
sys.runstats_pkg.rs_start;
--需要測試的第一個SQL或過程
for i in 1..1000
loop
insert into t2 values (i);
end loop;
commit;
sys.runstats_pkg.rs_middle;
--需要測試的第一個SQL或過程
for i in 1 .. 1000
loop
execute immediate 'insert into t2 values (:1)' using i;
commit;
end loop;
sys.runstats_pkg.rs_stop(500);
end;
/

執行後,我們會看到如下對於兩個SQL的比較結果.
Run1 ran in 3 hsecs
Run2 ran in 19 hsecs
run 1 ran in 15.79% of the time

Name Run1 Run2 Diff
STAT...deferred (CURRENT) bloc 1 503 502
STAT...messages sent 4 855 851
LATCH.Consistent RBA 2 855 853
LATCH.lgwr LWN SCN 2 856 854
LATCH.mostly latch-free SCN 2 862 860
STAT...calls to kcmgas 4 1,000 996
STAT...commit cleanouts 6 1,004 998
STAT...commit cleanouts succes 2 1,000 998
LATCH.session idle bit 1 1,000 999
STAT...user commits 1 1,000 999
STAT...parse count (total) 2 1,001 999
STAT...execute count 1,002 2,001 999
STAT...opened cursors cumulati 2 1,001 999
STAT...session cursor cache hi 0 1,000 1,000
LATCH.enqueues 1 1,002 1,001
STAT...consistent gets 5 1,007 1,002
STAT...redo entries 1,493 2,495 1,002
LATCH.post/wait queue 4 1,710 1,706
LATCH.redo writing 4 1,710 1,706
STAT...recursive calls 1,005 3,002 1,997
LATCH.dml lock allocation 4 2,002 1,998
LATCH.session allocation 2 2,000 1,998
STAT...enqueue releases 3 2,001 1,998
STAT...enqueue requests 2 2,001 1,999
STAT...db block changes 2,997 4,998 2,001
LATCH.redo allocation 1,497 4,205 2,708

Run1 latches total versus runs -- difference and pct
Run1 Run2 Diff Pct
15,049 30,630 15,581 49.13%

PL/SQL 過程已成功完成。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/703656/viewspace-1004461/,如需轉載,請註明出處,否則將追究法律責任。

相關文章