博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Oracle中大批量删除数据的方法
阅读量:6767 次
发布时间:2019-06-26

本文共 2834 字,大约阅读时间需要 9 分钟。

  hot3.png

批量删除海量数据通常都是很复杂及缓慢的,方法也很多,但是通常的概念是:分批删除,逐次提交。

下面是我的删除过程,我的数据表可以通过主键删除,测试过Delete和For all两种方法,for all在这里并没有带来性能提高,所以仍然选择了批量直接删除。
首先创建一下过程,使用自制事务进行处理:

create or replace procedure delBigTab(p_TableName       in    varchar2,p_Condition       in    varchar2,p_Count        in    varchar2)aspragma autonomous_transaction;n_delete number:=0;beginwhile 1=1 loopEXECUTE IMMEDIATE'delete from '||p_TableName||' where '||p_Condition||' and rownum <= :rn'USING p_Count;if SQL%NOTFOUND thenexit;elsen_delete:=n_delete + SQL%ROWCOUNT;end if;commit;end loop;commit;DBMS_OUTPUT.PUT_LINE('Finished!');DBMS_OUTPUT.PUT_LINE('Totally '||to_char(n_delete)||' records deleted!');end;

以下是删除过程及时间:

SQL> create or replace procedure delBigTab  2  (  3    p_TableName       in    varchar2,  4    p_Condition       in    varchar2,  5    p_Count        in    varchar2  6  )  7  as  8   pragma autonomous_transaction;  9   n_delete number:=0; 10  begin 11   while 1=1 loop 12     EXECUTE IMMEDIATE 13       'delete from '||p_TableName||' where '||p_Condition||' and rownum <= :rn' 14     USING p_Count; 15     if SQL%NOTFOUND then 16        exit; 17     else 18              n_delete:=n_delete + SQL%ROWCOUNT; 19     end if; 20     commit; 21   end loop; 22   commit; 23   DBMS_OUTPUT.PUT_LINE('Finished!'); 24   DBMS_OUTPUT.PUT_LINE('Totally '||to_char(n_delete)||' records deleted!'); 25  end; 26  /Procedure created.SQL> set timing onSQL> select min(NUMDLFLOGGUID) from HS_DLF_DOWNLOG_HISTORY;MIN(NUMDLFLOGGUID)------------------          11000000Elapsed: 00:00:00.23SQL> exec delBigTab('HS_DLF_DOWNLOG_HISTORY','NUMDLFLOGGUID < 11100000','10000');PL/SQL procedure successfully completed.Elapsed: 00:00:18.54SQL> select min(NUMDLFLOGGUID) from HS_DLF_DOWNLOG_HISTORY;MIN(NUMDLFLOGGUID)------------------          11100000Elapsed: 00:00:00.18SQL> set serveroutput onSQL> exec delBigTab('HS_DLF_DOWNLOG_HISTORY','NUMDLFLOGGUID < 11200000','10000');Finished!Totally 96936 records deleted!PL/SQL procedure successfully completed.Elapsed: 00:00:18.6110万记录大约19sSQL> exec delBigTab('HS_DLF_DOWNLOG_HISTORY','NUMDLFLOGGUID < 11300000','10000');Finished!Totally 100000 records deleted!PL/SQL procedure successfully completed.Elapsed: 00:00:18.62SQL> exec delBigTab('HS_DLF_DOWNLOG_HISTORY','NUMDLFLOGGUID < 11400000','10000');Finished!Totally 100000 records deleted!PL/SQL procedure successfully completed.Elapsed: 00:00:18.85SQL> SQL> exec delBigTab('HS_DLF_DOWNLOG_HISTORY','NUMDLFLOGGUID < 13000000','10000');Finished!Totally 1000000 records deleted!PL/SQL procedure successfully completed.Elapsed: 00:03:13.87100万记录大约3分钟SQL> exec delBigTab('HS_DLF_DOWNLOG_HISTORY','NUMDLFLOGGUID < 20000000','10000'); Finished!Totally 6999977 records deleted!PL/SQL procedure successfully completed.Elapsed: 00:27:24.69700万大约27分钟以上过程仅供参考.

转载于:https://my.oschina.net/90888/blog/829643

你可能感兴趣的文章
中层管理者的难题,“结果”还是“过程”?
查看>>
linux 目录及文件操作
查看>>
QPainter类 详解
查看>>
SpringMVC的工作流程
查看>>
我的友情链接
查看>>
Scala的bounds
查看>>
Project Euler 8 Largest product in a series
查看>>
TinyXml 使用注意事项
查看>>
virtualbox-网络地址转换()NAT)模式下搭建个人局域网
查看>>
如何使用multipart/form-data格式上传文件
查看>>
django路由和模板
查看>>
nginx socket支持php
查看>>
/etc/skel/ 家目录模板
查看>>
左值、右值、指针、传址、传值与数组
查看>>
maven scope含义的说明
查看>>
多张jpg转pdf
查看>>
Qt 工程启动
查看>>
Win 8并没有使PC销量上升
查看>>
玩转docker-镜像加速器
查看>>
C++ std::function 和 std::bind
查看>>