1. 程式人生 > >ORACLE刪除重複資料的一種高效的方法

ORACLE刪除重複資料的一種高效的方法

刪除重複資料的一種高效的方法

表demo是重複拷貝自dba_objects,有88萬左右,不重複的是27323,沒有索引
方法一:delete   from   demo   a   where   a.rowid   <>   (select   max(rowid)   from   demo   b   where
                                                                                                                                            b.object_id=a.object_id);
                        耗時:幾個小時以上


方法二:   delete   from   demo   where   rowid   in  
                            (select   rid   from  
                                                    (select   rowid   rid,row_number()   over(partition   by   object_id   order   by   rowid)   rn
                                                          from   demo)
                              where   rn   <>   1   );
      耗時:30秒

方法三:   create   table   demo2   as  
                            select   object_id,owner...   from
                                              (select   demo.*,row_number()   over(partition   by   object_id   order   by   rowid)   rn   from   demo)
                                                  where   rn   =   1;
                            truncate   table   demo;   insert   into   demo   select   *   from   demo2;   drop   table   demo2;

共耗時:   10秒,適合大資料量的情況,產生更少回滾量;

來源:http://blog.csdn.net/xjzdr/article/details/2047530