綁定變量在PL/SQL塊很好的工作,因爲他們在SQL語句做。
您可以通過在循環中運行簡單語句來測試此操作,然後查看v$sesstat
中的分析計數。
創建一個簡單的表用於插入和刪除。獲取初始分析計數。
create table test1(a number);
--Flush the pool, or else this test won't be repeatable.
alter system flush shared_pool;
select value, name
from v$sesstat natural join v$statname
where sid = sys_context('userenv', 'sid')
and name in ('parse count (total)', 'parse count (hard)');
47 parse count (total)
5 parse count (hard)
這就是硬解析的樣子:
begin
for i in 1 .. 10000 loop
execute immediate 'insert into test1 values('||i||')';
end loop;
commit;
end;
/
select value, name
from v$sesstat natural join v$statname
where sid = sys_context('userenv', 'sid')
and name in ('parse count (total)', 'parse count (hard)');
10072 parse count (total)
10007 parse count (hard)
與綁定變量並不總是硬解析PL/SQL塊。請注意,分析計數是累積的,並且在這裏僅略有增加。
begin
for i in 1 .. 10000 loop
execute immediate
'begin
delete from test1 where a = :i;
end;'
using i;
end loop;
commit;
end;
/
select value, name
from v$sesstat natural join v$statname
where sid = sys_context('userenv', 'sid')
and name in ('parse count (total)', 'parse count (hard)');
10106 parse count (total)
10019 parse count (hard)