【发布时间】:2020-01-13 08:54:48
【问题描述】:
使用批量收集或正常合并更新?
我正在尝试使用批量收集和正常合并来检查更新的性能。 我发现当我们在匿名块中使用简单合并时性能会更好。 当我使用批量收集时,需要更多时间。
如果普通更新(合并)比批量收集快,那么为什么 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 秒
【问题讨论】:
-
关注多少行?为什么以
100为限制?循环提交通常是性能杀手。 -
SQL 到 PL/SQL 引擎是一个消耗更多时间和资源的上下文切换。只需运行 MERGE 语句而不使用任何匿名块。如果需要执行时间,则在sqlplus中运行,设置time on timing on。
-
如果我给你一个勺子、一个桶和一个装满水的浴缸,然后让你尽快倒空,你会用什么?您可能会说水桶,但实际上拔插头更快!用数据库术语来说,spoon 代表逐行处理(例如一个 cursor for loop),bucket 代表批量收集处理,拔掉插头就是 SQL 解决方案。
-
如果普通更新(合并)比批量收集快,那Oracle为什么要引入呢?因为它们不一样。您可能出于其他原因想要填充数组。数组可能会作为参数传递到您的过程中,等等。
标签: sql plsql oracle11g bulkinsert query-performance