批量清除一個表的數據,TB級別數據。

有個需求,要求清理8TB的數據,只保留一個月的數據,現有數據量由2010年至2018年底都需要清除,因此寫了通用的該清除腳本。該表爲沒有分區的含有BLOB的大表,一共由三列,file_no,BLOB,還有create_time列,其中file_no爲主鍵,create_time非空。

腳本如下:
create or replace procedure proc_batch_delete(PI_table_name in varchar2, -- table name which will delete data
PI_where_condition in varchar2, -- delete sql condition
PI_thread_count in varchar2, -- thread number of the sql which use parallel hint
PI_commit_count in varchar2) is -- per delete count and commit count
v_delete integer;
v_sql varchar2(2000);
begin
dbms_output.put_line('Delete table is ' || PI_table_name || ';');
dbms_output.put_line('Where condition is ' || PI_where_condition || ';');
dbms_output.put_line('Number of threads is ' || to_char(PI_thread_count) || ';');
dbms_output.put_line('Commit count is ' || to_char(PI_commit_count) || ';');
dbms_output.put_line('Now start to batch delete. Timestamp is ' || to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss'));

v_delete := 0;

execute immediate 'alter session enable parallel dml';

v_sql := 'delete /+ parallel('||PI_table_name || ' ' || PI_thread_count ||') / from '
|| PI_table_name ||' where '|| PI_where_condition || 'and rownum <= ' || PI_commit_count;
dbms_output.put_line('Sql is ' || v_sql);

while 1=1 loop
EXECUTE IMMEDIATE v_sql;
if SQL%NOTFOUND then
exit;
else
v_delete:=v_delete + SQL%ROWCOUNT;
end if;
commit;
dbms_output.put_line('already deleted :' || to_char(v_delete) ||'.Timestamp is' || to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss'));
end loop;
commit;

dbms_output.put_line('End to batch delete. Timestamp is ' || to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss'));

end proc_batch_delete;

1.該圖

批量清除一個表的數據,TB級別數據。

2.執行語句
批量清除一個表的數據,TB級別數據。
3.輸入值
批量清除一個表的數據,TB級別數據。

4.顯示值
pi_table_name 需要刪除數據的表名
pi_where_condition 選擇條件
pi_thread_count併發線程數
pi_commit_count 每次刪除和提交事務數量

批量清除一個表的數據,TB級別數據。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章