天天看點

oracle海量資料表删除重複記錄

從網上下載下傳的解決方案如下

删除的幾種方法:

(1)通過建立臨時表來實作

SQL>create table temp_emp as (select distinct * from employee) 

SQL> truncate table employee; (清空employee表的資料)

SQL> insert into employee select * from temp_emp;  (再将臨時表裡的内容插回來)

( 2)通過唯一rowid實作删除重複記錄.在Oracle中,每一條記錄都有一個rowid,rowid在整個資料庫中是唯一的,rowid确定了每條記錄是在Oracle中的哪一個資料檔案、塊、行上。在重複的記錄中,可能所有列的内容都相同,但rowid不會相同,是以隻要确定出重複記錄中那些具有最大或最小rowid的就可以了,其餘全部删除。

SQL>delete from employee e2 where rowid not in (

        select max(e1.rowid) from employee e1 where

        e1.emp_id=e2.emp_id and e1.emp_name=e2.emp_name and e1.salary=e2.salary);--這裡用min(rowid)也可以。

SQL>delete from employee e2 where rowid <(

        select max(e1.rowid) from employee e1 where

        e1.emp_id=e2.emp_id and e1.emp_name=e2.emp_name and

                  e1.salary=e2.salary);

(3)也是通過rowid,但效率更高。

SQL>delete from employee where rowid not in (

        select max(t1.rowid) from employee t1 group by

         t1.emp_id,t1.emp_name,t1.salary);--這裡用min(rowid)也可以。

本文來自CSDN部落格,轉載請标明出處:http://blog.csdn.net/blueshine2/archive/2009/03/28/4032691.aspx

第一種方案涉及到對表的删改,相信大多數人不會輕易嘗試;

第二種方案經過驗證,如果條件很多,通過rowid作删除操作的效率也不是很高,特别是使用了group by …having 後,效率會大大降低,故應盡量避免