简体   繁体   English

sql比oracle中的plsql快吗

[英]is sql faster than plsql in oracle

update using bulk collect or normal merge ? 使用批量收集或常规合并更新?

I am trying to check the performance of update by using bulk collect and normal merge. 我试图通过使用批量收集和正常合并来检查更新的性能。 I see that the performance is better when we use simple merge in a anonymous block. 我看到当我们在匿名块中使用简单合并时,性能会更好。 When I use bulk collect, it takes more time. 使用批量收集时,需要花费更多时间。

If normal update (merge) is faster than bulk collect, then why oracle has introduced it? 如果正常更新(合并)比批量收集更快,那么oracle为什么引入它呢? Where do we actually see the benefit of bulk collect? 我们在哪里真正看到批量收集的好处?

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;

I get 0.797 sec in the merge query and 171.352 with bulk collect 我在合并查询中得到0.797秒,对批量收集得到171.352秒

If you can do it in SQL, it is almost always more efficient to do it in SQL. 如果您可以在SQL中执行此操作,那么在SQL中执行此操作几乎总是更有效。 If you have to resort to PL/SQL because you're doing some processing that benefits from procedural code, doing a bulk collect and a forall will be more efficient than the old style row-by-row processing (though if you're using implicit cursors, recent versions of Oracle will be doing a bulk collect automatically behind the scenes so the difference isn't as big as it was). 如果你不得不求助于PL / SQL,因为你正在做一些处理,从程序代码的好处,做bulk collectforall会比旧款行由行处理更有效(但如果你正在使用隐式游标,Oracle的最新版本将在幕后自动进行bulk collect ,因此差异不大。

In your test, I'd expect the commit in the loop to account for most of the difference in runtime. 在您的测试中,我希望循环中的提交能够解决运行时中的大部分差异。 Obviously, that is functionally different than the SQL solution. 显然,这在功能上与SQL解决方案有所不同。

If you can do it in SQL it will be always faster but even then 171.352 is very high value. 如果您可以在SQL中执行此操作,它将始终更快,但是即使这样171.352也非常有价值。 So I did my test and I added an index on test111(col1) and with same pl/sql block it has completed in 0.20 secs. 因此,我进行了测试,并在test111(col1)上添加了索引,并使用相同的pl / sql块在0.20秒内完成了索引。

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

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