Oracle 删除大表中部分数据

需求:

项目中有一张表大概有7000多万条数据,造成表空间已满,需要清理部分数据,打算清理3000万。

 

2B 做法:

delete from table_name where ID > '40000000';

备注:select count(1) from table_name where ID > 'his_batch_4000000';  的结果大概有3000万条数据。

 

影响:

删了N个小时也没执行完,最终强制停止,造成表被锁。(没有管理员权限,需要联系DBA 才能解锁)

 

改进:

declare
ncount number;
nrownumber number;
begin
nrownumber := 0;
loop
ncount := 0;

select count(1)
into ncount
from table_name 
where ID > 'his_batch_4000000'
and rownum < 10000;
if ncount > 0 then
delete from table_name 
where ID > 'his_batch_4000000'
and rownum < 10000;
commit;
nrownumber := nrownumber + ncount;
dbms_output.put_line(nrownumber);
else
exit;
end if;

end loop;
end;

posted @ 2015-04-17 19:14  zero516cn  阅读(3676)  评论(0编辑  收藏  举报