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

相關文章
相關標籤/搜索