繁体   English   中英

sql比oracle中的plsql快吗

[英]is sql faster than plsql in oracle

使用批量收集或常规合并更新?

我试图通过使用批量收集和正常合并来检查更新的性能。 我看到当我们在匿名块中使用简单合并时,性能会更好。 使用批量收集时,需要花费更多时间。

如果正常更新(合并)比批量收集更快,那么oracle为什么引入它呢? 我们在哪里真正看到批量收集的好处?

declare 
l_start integer;
l_end integer;
begin
l_start := dbms_utility.get_time;
merge into test111 t1
using test112 t2
on (t1.col1 = t2.col3)
when matched then update 
set t1.col2 = t1.col2*5;
l_end := dbms_utility.get_time;
dbms_output.put_line(l_end - l_start);
end;
declare
type nt_test is table of test112.col3%TYPE;
nt_val nt_test := nt_test();
cursor c is select col3 from test112;
c_limit integer := 100;
l_start integer;
l_end integer;
begin
l_start := DBMS_UTILITY.get_time;
open c;
loop

fetch c 
bulk collect into nt_val limit c_limit;
exit when nt_val.count = 0;

forall i in indices of nt_val
update test111 set col2 = col2/ 5
where col1 = nt_val(i);
commit;

end loop;
l_end := dbms_utility.get_time;
dbms_output.put_line(l_end - l_start);
end;

我在合并查询中得到0.797秒,对批量收集得到171.352秒

如果您可以在SQL中执行此操作,那么在SQL中执行此操作几乎总是更有效。 如果你不得不求助于PL / SQL,因为你正在做一些处理,从程序代码的好处,做bulk collectforall会比旧款行由行处理更有效(但如果你正在使用隐式游标,Oracle的最新版本将在幕后自动进行bulk collect ,因此差异不大。

在您的测试中,我希望循环中的提交能够解决运行时中的大部分差异。 显然,这在功能上与SQL解决方案有所不同。

如果您可以在SQL中执行此操作,它将始终更快,但是即使这样171.352也非常有价值。 因此,我进行了测试,并在test111(col1)上添加了索引,并使用相同的pl / sql块在0.20秒内完成了索引。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM