Oracle 删除大表中部分数据

需求:oop

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

 

2B 作法:table

delete from table_name where ID > '40000000';select

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

 

影响:数据

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

 

改进:db

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

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;loop

相关文章
相关标签/搜索