您好,登錄后才能下訂單哦!
這篇文章給大家介紹ORACLE中怎么批量刪除無主鍵重復數據,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
TEST表情況說明:
按月進行分區的分區表
未定義主鍵或唯一索引
包含COL1,COL2,COL3,INSERTTIME四列
現需要刪除2019年3月31日當天存在的重復數據
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數據量特別大,此處新建一張表將需要處理的數據單獨存放
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';
理論上而言需要刪除和需要保留的數據記錄數應相等
--需要刪除的數據記錄數 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' --需要保留的數據記錄數 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后的值為分批提交的記錄數,可以根據實際情況調整 FETCH C_ROWIDS BULK COLLECT INTO ROWID_INFOS LIMIT 10000; FORALL I IN 1..ROWID_INFOS.COUNT --如下的DELETE語句為分批提交實際需要執行的部分 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 ) );
關于ORACLE中怎么批量刪除無主鍵重復數據就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。