IT貓撲網(wǎng):您身邊最放心的安全下載站! 最新更新|軟件分類|軟件專題|手機版|論壇轉(zhuǎn)貼|軟件發(fā)布

您當(dāng)前所在位置: 首頁數(shù)據(jù)庫Oracle → Oracle大批量刪除數(shù)據(jù)方法

Oracle大批量刪除數(shù)據(jù)方法

時間:2015-06-28 00:00:00 來源:IT貓撲網(wǎng) 作者:網(wǎng)管聯(lián)盟 我要評論(0)

  批量刪除海量數(shù)據(jù)通常都是很復(fù)雜及緩慢的,方法也很多,但是通常的概念是:分批刪除,逐次提交。

  下面是我的刪除過程,我的數(shù)據(jù)表可以通過主鍵刪除,測試過Delete和For all兩種方法,for all在這里并沒有帶來性能提高,所以仍然選擇了批量直接刪除。

  首先創(chuàng)建一下過程,使用自制事務(wù)進(jìn)行處理:

  create or replace procedure delBigTab

  (

  p_TableName in varchar2,

  p_Condition in varchar2,

  p_Count in varchar2

  )

  as

  pragma autonomous_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 /

#p#副標(biāo)題#e#

  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!

  Totally 96936 records 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分鐘

  以上過程僅供參考.

關(guān)鍵詞標(biāo)簽:Oracle大批量刪除數(shù)據(jù)

相關(guān)閱讀

文章評論
發(fā)表評論

熱門文章 Oracle中使用alter table來增加,刪除,修改列的語法 Oracle中使用alter table來增加,刪除,修改列的語法 oracle中使用SQL語句修改字段類型-oracle修改SQL語句案例 oracle中使用SQL語句修改字段類型-oracle修改SQL語句案例 誤刪Oracle數(shù)據(jù)庫實例的控制文件 誤刪Oracle數(shù)據(jù)庫實例的控制文件 為UNIX服務(wù)器設(shè)置Oracle全文檢索 為UNIX服務(wù)器設(shè)置Oracle全文檢索

相關(guān)下載

    人氣排行 oracle中使用SQL語句修改字段類型-oracle修改SQL語句案例 Oracle中使用alter table來增加,刪除,修改列的語法 ORACLE SQL 判斷字符串是否為數(shù)字的語句 ORACLE和SQL語法區(qū)別歸納(1) oracle grant 授權(quán)語句 ORACLE修改IP地址后如何能夠使用 如何加速Oracle大批量數(shù)據(jù)處理 Oracle刪除表的幾種方法