1. 程式人生 > >刪除重複資料,建立唯一性索引

刪除重複資料,建立唯一性索引

             一開始資料庫中未建立唯一性索引,但是在業務邏輯上是有唯一性約束的。在執行一段時間後,才發現這個問題,記錄一下

對應的表T_ROUTE_ORBIT,唯一限制欄位: longitude, latitude, task_id

刪除重複記錄,保留第一條:

delete from T_ROUTE_ORBIT a
 where (a.longitude, a.latitude, a.task_id) in
       (select longitude, latitude, task_id
          from T_ROUTE_ORBIT
         group by longitude, latitude, task_id
        having count(*) > 1)
   and rowid not in (select min(rowid)
                       from T_ROUTE_ORBIT
                      group by longitude, latitude, task_id
                     having count(*) > 1) order by time 

建立唯一性索引:

 create unique index route_orbit on T_ROUTE_ORBIT(longitude,latitude,task_id)