create or replace procedure large_table_clean_template(var_tablename varchar2,var_condition varchar2)
as
type type_rowid_array is table of rowid;
type ref_cursor is ref cursor;
rowid_array type_rowid_array;
cur_rowids ref_cursor;
select_sqlText varchar2(500);
i_arraysize integer := 100;
i_number integer;
begin
select count(1) into i_number from user_tables where table_name=upper(var_tablename);
if(i_number = 0) then
return;
end if;
select_sqlText := 'select rowid from ' || var_tablename || ' where ' || var_condition;
open cur_rowids for select_sqlText;
loop
fetch cur_rowids bulk collect into rowid_array limit i_arraysize;
forall i in 1..rowid_array.count
execute immediate 'delete from ' || var_tablename || ' where rowid=:1' using rowid_array(i);
if(mod(cur_rowids%rowcount,20000)=0) then
commit;
end if;
exit when cur_rowids%notfound;
end loop;
commit;
if(cur_rowids%isopen) then
close cur_rowids;
end if;
exception
when others then
rollback;
if(cur_rowids%isopen) then
close cur_rowids;
end if;
end;
/
1、通过bulk collect减少loop处理的开销,使用Bulk Collect提高Oracle查询效率
Oracle8i中首次引入了Bulk Collect特性,该特性可以让我们在PL/SQL中能使用批查询,批查询在某些情况下能显著提高查询效率。
采用bulk collect可以将查询结果一次性地加载到collections中。
而不是通过cursor一条一条地处理。
可以在select into,fetch into,returning into语句使用bulk collect。
注意在使用bulk collect时,所有的into变量都必须是collections
2、forall与for的区别