问题描述
示例示例:
/* 由于选择大量数据,此过程大约需要20-25分钟才能完成,
有什么方法可以减少执行时间?*/
/* 由于选择大量数据,此过程大约需要20-25分钟才能完成,
有什么方法可以减少执行时间?*/
procedure sample ( a_in IN varchar2)
IS
v_row number;
v1_row number;
v2_row number;
cursor c1 IS
select a_value, b_value.., from source_table;
/* cursor c1 selecting 46 millions record, but inserted few records to the below two destinations tables based on conditions,
source_table is a force view*/
Begin
for i in c1
loop
v_row := 0;
select count(1) into v_row from table_item where item = i.a_value||'_'||a_in;
if v_row > 0 then
select count(1) into v1_row from destination_table1
where item1 = (i.b_value||'_'||a_in);
if v1_row = 0 then
insert into destination_table1
(a_value, b_value)
values(i.a_value, i.b_value);
commit;
end if;
if i.b_value is not null then
v2_row := 0;
select count(1) into v2_row from destination_table2 where item2 = (i.a_value ||'_'||a_in) and
item3 = (i.b_value||'_'||a_in);
if v2_row = 0 then
insert into destination_table2 (item2, item3) values (i.a_value ||'_'||a_in,
i.b_value||'_'||a_in);
commit;
end if;
end if;
end if;
end loop;
End sample;
/* this procedure is taking approx. 20 mins to complete */
专家解答
cursor c1 selecting 46 millions record
所以你在循环4600万行?!
难怪这太慢了!
将SQL放入循环中 => sloooooow
如果你想要快速的SQL,理想情况下你想要一个改变所有行的语句。
在循环中进行承诺是另一种主要的罪过。这会减慢流程并破坏事务完整性。
可以用单个多表插入替换循环:
如果没有,那么用批量处理替换它应该会给你带来很大的性能提升
https://blogs.oracle.com/oraclemagazine/on-bulk-collect
所以你在循环4600万行?!
难怪这太慢了!
将SQL放入循环中 => sloooooow
如果你想要快速的SQL,理想情况下你想要一个改变所有行的语句。
在循环中进行承诺是另一种主要的罪过。这会减慢流程并破坏事务完整性。
可以用单个多表插入替换循环:
insert all when ... then insert into t1 when ... then insert into t2 select of 46 million rows
如果没有,那么用批量处理替换它应该会给你带来很大的性能提升
https://blogs.oracle.com/oraclemagazine/on-bulk-collect
文章转载自ASKTOM,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




