這篇文章給大家介紹ORACLE中怎么批量刪除無主鍵重復(fù)數(shù)據(jù),內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
成都創(chuàng)新互聯(lián)公司主要從事成都做網(wǎng)站、成都網(wǎng)站制作、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)偏關(guān),10余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18982081108
TEST表情況說明:
按月進(jìn)行分區(qū)的分區(qū)表
未定義主鍵或唯一索引
包含COL1,COL2,COL3,INSERTTIME四列
現(xiàn)需要刪除2019年3月31日當(dāng)天存在的重復(fù)數(shù)據(jù)
SELECT COUNT(1) FROM ( SELECT COL1,COL2,COL3,INSERTTIME FROM TEST PARTITION(P201903) A WHERE INSERTTIME >= DATE'2019-03-31' AND INSERTTIME < DATE'2019-04-01' GROUP BY COL1,COL2,COL3 );
由于原表A數(shù)據(jù)量特別大,此處新建一張表將需要處理的數(shù)據(jù)單獨(dú)存放
CREATE TABLE TEST_TMP NOLOGGING AS SELECT /*PARALLEL +8 */ A.*,A.ROWID ROWID_OLD FROM TEST PARTITION(P201903) A WHERE INSERTTIME >= DATE'2019-03-31' AND INSERTTIME < DATE'2019-04-01';
理論上而言需要刪除和需要保留的數(shù)據(jù)記錄數(shù)應(yīng)相等
--需要刪除的數(shù)據(jù)記錄數(shù) SELECT COUNT(1) FROM TEST PARTITION(P201903) A WHERE ROWID IN ( SELECT MIN(ROWID_OLD) ROWID_OLD FROM TEST_TMP WHERE INSERTTIME >= DATE'2019-03-31' AND INSERTTIME < DATE'2019-04-01' GROUP BY COL1,COL2,COL3,INSERTTIME HAVING COUNT(1) > 1) AND INSERTTIME >= DATE'2019-03-31' AND INSERTTIME < DATE'2019-04-01' --需要保留的數(shù)據(jù)記錄數(shù) SELECT COUNT(1) FROM TEST PARTITION(P201903) A WHERE ROWID NOT IN ( SELECT MIN(ROWID_OLD) ROWID_OLD FROM TEST_TMP WHERE INSERTTIME >= DATE'2019-03-31' AND INSERTTIME < DATE'2019-04-01' GROUP BY COL1,COL2,COL3,INSERTTIME HAVING COUNT(1) > 1) AND INSERTTIME >= DATE'2019-03-31' AND INSERTTIME < DATE'2019-04-01'
DECLARE TYPE ROWID_LIST IS TABLE OF UROWID INDEX BY BINARY_INTEGER; ROWID_INFOS ROWID_LIST; I NUMBER; CURSOR C_ROWIDS IS (SELECT MIN(ROWID_OLD) ROWID_OLD FROM TEST_TMP WHERE INSERTTIME >= DATE'2019-03-31' AND INSERTTIME < DATE'2019-04-01' GROUP BY COL1,COL2,COL3,INSERTTIME HAVING COUNT(1) > 1); BEGIN OPEN C_ROWIDS; LOOP --此處LIMIT后的值為分批提交的記錄數(shù),可以根據(jù)實(shí)際情況調(diào)整 FETCH C_ROWIDS BULK COLLECT INTO ROWID_INFOS LIMIT 10000; FORALL I IN 1..ROWID_INFOS.COUNT --如下的DELETE語句為分批提交實(shí)際需要執(zhí)行的部分 DELETE FROM TEST WHERE ROWID=ROWID_INFOS(I); COMMIT; EXIT WHEN ROWID_INFOS.COUNT<10000; END LOOP; CLOSE C_ROWIDS; END;
SELECT * FROM ( SELECT COL1,COL2,COL3,INSERTTIME FROM TEST PARTITION(P201903) A WHERE INSERTTIME >= DATE'2019-03-31' AND INSERTTIME < DATE'2019-04-01' GROUP BY COL1,COL2,COL3,INSERTTIME HAVING COUNT(1)>1 ) );
關(guān)于ORACLE中怎么批量刪除無主鍵重復(fù)數(shù)據(jù)就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。