以下是我写的关于删除一个表中某些记录的过程
create or replace procedure Del_pur_order
(
p_tablename in varchar2, --表名
p_condition in varchar2, --条件
p_count in number --每次删除条数
)
as
pragma autonomous_transaction;
n_delete number:=0;
Asql varchar2(100);
begin
while 1=1 loop
n_delete:=p_count;
EXECUTE IMMEDIATE
'delete from '||p_tablename||' where '||p_condition||' and rownum <=:1'
USING p_count;-- Asql:= 'delete from'''||p_tablename||''' where'''|| p_condition||''' and rownum<=''';--||p_count||'''';
dbms_output.put_line(Asql);
if sql%notfound then
exit;
else
n_delete := n_delete + sql%rowcount;
end if;
commit;
end loop;
commit;
end Del_pur_order;
经过测试发现没有比直接用delete from||p_tablename|| where|| p_condition删除快.这是什么原因,有没有那位大哥有更好的删除办法.能使删除的速度更快.
create or replace procedure Del_pur_order
(
p_tablename in varchar2, --表名
p_condition in varchar2, --条件
p_count in number --每次删除条数
)
as
pragma autonomous_transaction;
n_delete number:=0;
Asql varchar2(100);
begin
while 1=1 loop
n_delete:=p_count;
EXECUTE IMMEDIATE
'delete from '||p_tablename||' where '||p_condition||' and rownum <=:1'
USING p_count;-- Asql:= 'delete from'''||p_tablename||''' where'''|| p_condition||''' and rownum<=''';--||p_count||'''';
dbms_output.put_line(Asql);
if sql%notfound then
exit;
else
n_delete := n_delete + sql%rowcount;
end if;
commit;
end loop;
commit;
end Del_pur_order;
经过测试发现没有比直接用delete from||p_tablename|| where|| p_condition删除快.这是什么原因,有没有那位大哥有更好的删除办法.能使删除的速度更快.
可以先考虑优化相应的 select 语句。
delete xx where id= xx+i;
--或者其他条件,由i循环
commit;
next
--这样的好处:减少redo的空间,每次提交 清空缓冲区的reundo
大量的数据要分批的删除,这样速度快