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


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的区别

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值