效能比較工具runStats_pkg
runStats_pkg是Tom大師寫的一個指令碼,
用於比較兩個語句的時間,統計資料和栓鎖使用情況。
指令碼下載連結:
https://asktom.oracle.com/pls/asktom/ASKTOM.download_file?p_file=6551378329289980701
下面以edmond帳號為例,使用runStats比較繫結變數的差異
首先,給edmond帳號系統表的授權。
conn / as sysdba
create user edmond identified by edmond;
grant connect,resource to edmond;
grant create view to edmond;
grant select on sys.v_$statname to edmond;
grant select on sys.v_$mystat to edmond;
grant select on sys.v_$latch to edmond;
grant select on sys.v_$timer to edmond;
然後,執行runStats指令碼
接下來建立測試表和過程
create table t(x int);
insert into t select rownum from dual connect by level<=10000;
沒有繫結變數的測試過程
create or replace procedure p1
as
l_cnt number;
begin
for i in 1 .. 10000
loop
execute immediate 'select count(*) from t where x=' || i into l_cnt;
end loop;
end;
/
繫結變數的測試過程
create or replace procedure p2
as
l_cnt number;
begin
for i in 1 .. 10000
loop
select count(*) into l_cnt from t where x=i;
end loop;
end;
/
開始測試:
set serveroutput on;
alter system flush shared_pool;
exec runStats_pkg.rs_start;
exec p1;
exec runStats_pkg.rs_middle;
exec p2;
exec runStats_pkg.rs_stop(1000);
測試結果
可以看到第一個過程解析了近1w次,而第二個過程僅僅解析了7次
非繫結變數的方式導致了大量的栓鎖使用.

用於比較兩個語句的時間,統計資料和栓鎖使用情況。
指令碼下載連結:
https://asktom.oracle.com/pls/asktom/ASKTOM.download_file?p_file=6551378329289980701
下面以edmond帳號為例,使用runStats比較繫結變數的差異
首先,給edmond帳號系統表的授權。
conn / as sysdba
create user edmond identified by edmond;
grant connect,resource to edmond;
grant create view to edmond;
grant select on sys.v_$statname to edmond;
grant select on sys.v_$mystat to edmond;
grant select on sys.v_$latch to edmond;
grant select on sys.v_$timer to edmond;
然後,執行runStats指令碼
-
create global temporary table run_stats
-
( runid varchar2(15),
-
name varchar2(80),
-
value int )
-
on commit preserve rows;
-
-
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 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;
-
-
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 (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/decode( run2, 0, to_number(null), 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;
- /
create table t(x int);
insert into t select rownum from dual connect by level<=10000;
沒有繫結變數的測試過程
create or replace procedure p1
as
l_cnt number;
begin
for i in 1 .. 10000
loop
execute immediate 'select count(*) from t where x=' || i into l_cnt;
end loop;
end;
/
繫結變數的測試過程
create or replace procedure p2
as
l_cnt number;
begin
for i in 1 .. 10000
loop
select count(*) into l_cnt from t where x=i;
end loop;
end;
/
開始測試:
set serveroutput on;
alter system flush shared_pool;
exec runStats_pkg.rs_start;
exec p1;
exec runStats_pkg.rs_middle;
exec p2;
exec runStats_pkg.rs_stop(1000);
測試結果
可以看到第一個過程解析了近1w次,而第二個過程僅僅解析了7次
非繫結變數的方式導致了大量的栓鎖使用.

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29254281/viewspace-1273911/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 效能比較
- Java實體對映工具MapStruct 與BeanUtils效能比較JavaStructBean
- python 批量resize效能比較Python
- 請比較下for、forEach、for of的效能的效能
- Java中List集合效能比較Java
- 排序演算法效能比較排序演算法
- IocPerformance 常見IOC 功能、效能比較ORM
- 堆排序和快速排序效能比較排序
- Java Bean Copy元件的效能比較JavaBean元件
- Java JIT與AOT效能比較 - foojayJava
- VisualDiffer for mac (檔案比較工具)Mac
- Jenkins vs Kubernetes:比較 DevOps 工具Jenkinsdev
- Beyond Compare for Mac(檔案比較對比工具)Mac
- 雲主機的硬碟IO效能比較硬碟
- Java幾種常用JSON庫效能比較JavaJSON
- WCF與ASP.NET Core效能比較ASP.NET
- PostgreSQL、Redis與Memcached的效能比較 - CYBERTECSQLRedis
- 77種資料建模工具比較
- UltraCompare for Mac「Macos檔案比較工具」Mac
- Kaleidoscope for Mac,檔案影像比較工具Mac
- Vue.js構建工具比較Vue.js
- ETL介紹與ETL工具比較
- 常用的Java開發工具比較Java
- 檔案和影像比較工具Kaleidoscope
- UltraCompare 21 for Mac 檔案比較工具Mac
- Apache與Nginx的優缺點、效能比較,到底選擇哪個比較好?ApacheNginx
- PHP file_get_contents 與 curl 效能比較PHP
- Python、JavaScript和Rust的Web效能比較 - AlexPythonJavaScriptRustWeb
- Spring Boot Native vs Go:效能比較 – Ignacio SuaySpring BootGo
- 使用 BenchmarkDotNet 比較指定容量的 List 的效能
- Caddy 與 Nginx的基準效能比較 - tjllNginx
- MySQL 中的 distinct 和 group by 的效能比較MySql
- 淺談前端MOCK資料工具比較前端Mock
- 原型設計工具比較及實踐原型
- 幾個比較火的BI分析工具
- POWER BI - 與其他BI工具的比較
- 2022年JavaScript開發工具比較 - bullshJavaScript
- Kaleidoscope for Mac(檔案和影像比較工具)Mac
- Mac檔案和影像比較工具:KaleidoscopeMac