Oracle中大批量删除数据的方法
程序员文章站
2023-11-29 20:41:22
写一个循环删除的过程。 create or replace procedure delbigtab(p_tablename ...
写一个循环删除的过程。
create or replace procedure delbigtab(p_tablename in varchar2,p_condition in varchar2,p_count in varchar2)
as
pragma autonomous_transaction;
n_delete number:=0;
begin
while 1=1 loop
execute immediate
'delete from '||p_tablename||' where '||p_condition||' and rownum <= :10000'
using p_count;
if sql%notfound then
exit;
else
n_delete:=n_delete + sql%rowcount;
end if;
commit;
end loop;
commit;
dbms_output.put_line('finished!');
dbms_output.put_line('totally '||to_char(n_delete)||' records deleted!');
end delbigtab;
调用:
sql> set timing on
sql> exec delbigtab('hs_dlf_downlog_history','numdlflogguid < 11100000','10000');
pl/sql procedure successfully completed.
elapsed: 00:00:18.54
方法虽好,但我应用在一个亿级数据库时还是觉得慢得不行。就算删一点点数据也觉得好象挺慢的。
create or replace procedure delbigtab(p_tablename in varchar2,p_condition in varchar2,p_count in varchar2)
as
pragma autonomous_transaction;
n_delete number:=0;
begin
while 1=1 loop
execute immediate
'delete from '||p_tablename||' where '||p_condition||' and rownum <= :10000'
using p_count;
if sql%notfound then
exit;
else
n_delete:=n_delete + sql%rowcount;
end if;
commit;
end loop;
commit;
dbms_output.put_line('finished!');
dbms_output.put_line('totally '||to_char(n_delete)||' records deleted!');
end delbigtab;
调用:
sql> set timing on
sql> exec delbigtab('hs_dlf_downlog_history','numdlflogguid < 11100000','10000');
pl/sql procedure successfully completed.
elapsed: 00:00:18.54
方法虽好,但我应用在一个亿级数据库时还是觉得慢得不行。就算删一点点数据也觉得好象挺慢的。