目錄
動态執行sql
變量指派方式:
拼接sql方式:
動态遊标
變量指派方式:
拼接sql方式:
動态執行sql
分别運作一萬次,執行三遍
變量指派方式執行三遍耗時在80毫秒左右
拼接sql方式執行三遍耗時在200毫秒左右
由此可見,變量指派方式更快。
變量指派方式:
declare
a number;
begin
for i in 1..10000
loop
execute immediate 'select :b from dual' into a using i;
end loop;
DBMS_OUTPUT.PUT_LINE(a);
end;

拼接sql方式:
declare
a number;
begin
for i in 1..10000
loop
execute immediate 'select ' || i || ' from dual' into a;
end loop;
DBMS_OUTPUT.PUT_LINE(a);
end;
動态遊标
分别運作一萬次,執行三遍
變量指派方式執行三遍耗時在190毫秒左右
拼接sql方式執行三遍耗時在250毫秒左右
由此可見,變量指派方式更快。
變量指派方式:
declare
type cur is ref cursor;
cr cur;
a number;
b number;
begin
for i in 1..10000
loop
open cr for 'select :c From Dual' using i;
loop
fetch cr into a;
exit when cr%notfound;
b := a;
end loop;
close cr;
end loop;
DBMS_OUTPUT.PUT_LINE(b);
end;
拼接sql方式:
declare
type cur is ref cursor;
cr cur;
a number;
b number;
begin
for i in 1..10000
loop
open cr for 'select '|| i || ' From Dual';
loop
fetch cr into a;
exit when cr%notfound;
b := a;
end loop;
close cr;
end loop;
DBMS_OUTPUT.PUT_LINE(b);
end;