Oracle中大批量刪除資料的方法

2021-04-14 17:21:13 字數 3639 閱讀 5983

批量刪除海量資料通常都是很複雜及緩慢的,方法也很多,但是通常的概念是:分批刪除,逐次提交。

下面是我的刪除過程,我的資料表可以通過主鍵刪除,測試過delete和for all兩種方法,for all在這裡並沒有帶來效能提高,所以仍然選擇了批量直接刪除。

首先建立一下過程,使用自製事務進行處理:

create or replace procedure delbigtab

(p_tablename in varchar2,

p_condition in varchar2,

p_count in varchar2)as

pragmaautonomous_transaction;

n_delete number:=0;

begin

while 1=1 loop

execute immediate

'delete from '||p_tablename||' where '||p_condition||' and rownum <= :rn'

using p_count;

if sql%notfound then

exit;

else

n_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 on

sql> select min(numdlflogguid) from hs_dlf_downlog_history;

min(numdlflogguid)

------------------

11000000

elapsed: 00:00:00.23

sql> exec delbigtab('hs_dlf_downlog_history','numdlflogguid < 11100000','10000');

pl/sql procedure successfully completed.

elapsed: 00:00:18.54

sql> select min(numdlflogguid) from hs_dlf_downlog_history;

min(numdlflogguid)

------------------

11100000

elapsed: 00:00:00.18

sql> set serveroutput on

sql> exec delbigtab('hs_dlf_downlog_history','numdlflogguid < 11200000','10000');

finished!

totally96936records deleted!

pl/sql procedure successfully completed.

elapsed:00:00:18.61

10萬記錄大約19s

sql> exec delbigtab('hs_dlf_downlog_history','numdlflogguid < 11300000','10000');

finished!

totally 100000 records deleted!

pl/sql procedure successfully completed.

elapsed: 00:00:18.62

sql> exec delbigtab('hs_dlf_downlog_history','numdlflogguid < 11400000','10000');

finished!

totally 100000 records deleted!

pl/sql procedure successfully completed.

elapsed: 00:00:18.85

sql>

sql> exec delbigtab('hs_dlf_downlog_history','numdlflogguid < 13000000','10000');

finished!

totally 1000000 records deleted!

pl/sql procedure successfully completed.

elapsed: 00:03:13.87

100萬記錄大約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.69

700萬大約27分鐘

以上過程僅供參考. 

Oracle中大批量刪除資料的方法

寫乙個迴圈刪除的過程。create or replace procedure delbigtab p tablename in varchar2,p condition in varchar2,p count in varchar2 aspragma autonomous transaction n...

C 中大批量資料匯入

database db databasefactory.createdatabase using sqlconnection connection sqlconnection db.createconnection bulk.writetoserver dspayment.tables 1 bulk...

Oracle儲存過程刪除大批量資料

參考 批量刪除海量資料通常都是很複雜及緩慢的,方法也很多,但是通常的概念是 分批刪除,逐次提交。下面是我的刪除過程,我的資料表可以通過主鍵刪除,測試過delete和for all兩種方法,for all在這裡並沒有帶來效能提高,所以仍然選擇了批量直接刪除。操作如下 建立日誌記錄表 create ta...