码迷,mamicode.com
首页 > 数据库 > 详细

Oracle 删除大表中部分数据

时间:2015-04-17 20:19:31      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

需求:

项目中有一张表大概有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;

Oracle 删除大表中部分数据

标签:

原文地址:http://www.cnblogs.com/gw811/p/4435777.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!