您好,登錄后才能下訂單哦!
這篇文章給大家介紹ORACLE中怎么批量刪除無主鍵重復(fù)數(shù)據(jù),內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
TEST表情況說明:
按月進(jìn)行分區(qū)的分區(qū)表
未定義主鍵或唯一索引
包含COL1,COL2,COL3,INSERTTIME四列
現(xiàn)需要?jiǎng)h除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';
理論上而言需要?jiǎng)h除和需要保留的數(shù)據(jù)記錄數(shù)應(yīng)相等
--需要?jiǎng)h除的數(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)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。