我想知道在PL-SQL缓存中,FOR LOOP中使用的数组的长度是否会对程序性能产生重大影响?
这个例子也是如此:
ln_count := lna_avcs.COUNT;
FOR i in 1..ln_count LOOP
--do something
END LOOP;
比这更有效:
FOR i in 1..lna_avcs.COUNT LOOP
--do something
END LOOP;
假设数组的维度在100到1000之间.
我知道在javascript中,缓存的例子可以有显着的改进.
谢谢
最佳答案 我对性能进行了一些检查,版本1和版本2之间没有任何显着差异.我重复了几次测试,结果几乎相同.有时版本1更好,有时版本2更好.
出于测试目的,我创建了一个数组:
type array_t is varray(1000) of varchar2(80);
array array_t ;
然后我用虚拟数据填充数组:
select dbms_random.string('X',25)
bulk collect into array
from dual connect by level < 1000;
测试1:
for j in 1..10000 loop
for i in 1..array.count loop
temp := array(i);
end loop;
end loop;
测试2:
for j in 1..10000 loop
vcount := array.COUNT;
for i in 1..vcount loop
temp := array(i);
end loop;
end loop;
我测量了执行Test1和Test2的时间
timestart := systimestamp;
--Test1 or Test2 is here
timeend := systimestamp;
diff := extract(second from timeEnd - timeStart);
我将diff插入到loop_test表中:
insert into loop_test values
(1, diff);
之后,我重复每次测试200次.查询表单loop_test为我们提供:
select id,
avg(exectime),
count(*),
max(exectime),
min(exectime)
from loop_test
group by id
ID AVG(EXECTIME) COUNT(*) MAX(EXECTIME) MIN(EXECTIME)
1 0.797545 200 1.046 0.78
2 0.79841 200 1.045 0.78
结果:
使用array.Count或使用首先分配给变量的计数与性能点视图没有显着差异.测试是在Oracle 11g上完成的.
完整的测试在这里:
declare
type array_t is varray(1000) of varchar2(80);
array array_t ;
temp varchar2(80);
timestart timestamp;
timeend timestamp;
diff number;
begin
select dbms_random.string('X',25)
bulk collect into array
from dual connect by level < 1000;
for k in 1..200 loop
timestart := systimestamp;
for j in 1..10000 loop
for i in 1..array.count loop
temp := array(i);
end loop;
end loop;
timeend := systimestamp;
diff := extract(second from timeEnd - timeStart);
dbms_output.put_line(diff || ' ' || timestart || ' ' || timeend);
insert into loop_test values
(1, diff);
commit;
end loop;
end;