删除大数量的表中数据之存储过程模板

来源:互联网 发布:苏联暴行知乎 编辑:程序博客网 时间:2024/05/20 18:20

create or replace procedure large_table_clean_template(var_tablename varchar2,var_condition varchar2) astype 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;beginselect count(1) into i_number from user_tables where table_name=upper(var_tablename);if(i_number = 0) thenreturn;end if;select_sqlText := 'select rowid from ' || var_tablename || ' where ' || var_condition;open cur_rowids for select_sqlText;loopfetch cur_rowids bulk collect into rowid_array limit i_arraysize;forall i in 1..rowid_array.countexecute immediate 'delete from ' || var_tablename || ' where rowid=:1' using rowid_array(i);if(mod(cur_rowids%rowcount,20000)=0) thencommit;end if;exit when cur_rowids%notfound;end loop;commit;if(cur_rowids%isopen) thenclose cur_rowids;end if;exceptionwhen others thenrollback;if(cur_rowids%isopen) thenclose 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的区别
for循环和forall循环的区别