一次成功批量刪除oracle冗餘數據的經歷

問題描述:千辛萬苦往數據庫裏存了幾十萬條數據,發現因爲程序問題,有將近10萬條的冗餘數據,此時心裏是無比崩潰的,關於怎麼查詢是否有冗餘數據見上一篇文章(http://www.javashuo.com/article/p-zyfhwyol-gh.html)。html

嘗試1:首先想到的固然就是delete語句啦,以下所示:python

sql = "delete from all_askrep a where (a.askname, a.atime) in (select askname,atime from all_askrep group by askname,atime having count(*)>1) and askrepid not in (select min(askrepid) from all_askrep group by askname,atime having count(*)>1)"
cursor.execute(sql)
conn.commit()

而後,發現一個小時後仍然沒結束,我就終止了操做,以後我加了一個rownum < 1000,先看看刪除前一千行數據須要的時間,發現也須要個二十分鐘,這有點過度啊!!sql

嘗試2:PL/sql方式執行,因爲不太懂pl/sql語法,又急需對數據進行刪除操做,因此此次放棄了,相關例子可參考博文(https://www.cnblogs.com/nayitian/p/3238251.html數據庫

嘗試3:利用臨時表刪除數據,實踐證實這才符合個人需求,兩秒內執行完全部刪除操做。在嘗試第二種方法的時候安裝了plsqldev工具,藉助工具執行的刪除操做,固然了,不用工具是同樣的。代碼以下(建立臨時表-》藉助臨時表刪除相關數據-》刪除臨時表):工具

create table tempaskrep as select a.askname, a.atime, a.askrepid as dataid from all_askrep a where a.askrepid not in (select max(b.askrepid) from all_askrep b group by b.askname,b.atime);
commit;
delete from all_askrep where askrepid in (select dataid from tempaskrep);
commit;
drop table tempaskrep;
commit;
相關文章
相關標籤/搜索