批量清除一个表的数据,TB级别数据。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了批量清除一个表的数据,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.该图
2.执行语句
3.输入值
4.显示值
pi_table_name 需要删除数据的表名
pi_where_condition 选择条件
pi_thread_count并发线程数
pi_commit_count 每次删除和提交事务数量
以上是关于批量清除一个表的数据,TB级别数据。的主要内容,如果未能解决你的问题,请参考以下文章