sql1:
delete from table1 t1 left join table2 t2 on t1.field1=t2.field1 and t1.field2=t2.field2 and t1.field3=t2.field3
where t2.field3 is not null
sql2:
delete from table1 t1
where exists (select 1 from table2 t2 where t1.field1=t2.field1 and t1.field2=t2.field2 and t1.field3=t2.field3
where t2.field3 is not null)
其中可以保證table1 中 field3是一定存在的,那這兩個sql洗掉的資料是一樣的 嗎?求指導
在執行大資料量(千萬級)洗掉時sql2會洗掉的超級慢。能不能換成sql1
uj5u.com熱心網友回復:
可以替換,執行結果是一樣的。uj5u.com熱心網友回復:
一般來說洗掉速度: drop> truncate > delete。如果想洗掉部分資料行用delete,注意帶上where子句, 回滾段要足夠大 。想洗掉表,當然用drop想保留表而將所有資料洗掉。 如果和事務無關,用truncate即可。如果和事務有關,或者想觸發trigger,還是用delete 就看你自己啦。但是還是要小心使用drop 和truncate,尤其沒有備份的時候.否則哭都找不到太平洋呀!!!
uj5u.com熱心網友回復:
Oracle 不支持join連接做update和delete 吧uj5u.com熱心網友回復:
delete from table1 t1where exists (select 1 from table2 t2 where t1.field1=t2.field1 and t1.field2=t2.field2 and t1.field3=t2.field3
where t2.field3 is not null) 你看看field3條件 允許為空? 還作為關聯欄位?
如果是
where length(t2.field3)>0 試試
uj5u.com熱心網友回復:
不要直接洗掉海量資料,建議:先備份table1資料導table1_bak,再truncate table1,最后再將table1_bak中不洗掉的插入table1。在此操作以前,必須將主鍵、索引洗掉,資料處理完成后再恢復主鍵、索引。uj5u.com熱心網友回復:
delete就這樣uj5u.com熱心網友回復:
資料量大的表做磁區表,洗掉的時候就會比較方便。uj5u.com熱心網友回復:
兩邊的結果不一樣,如果field3一定存在,那么第一個陳述句,應該是會把table1的資料全部刪掉。第二個之刪掉和table2有關聯的資料。sql2慢,可以加索引。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/43214.html
標籤:開發
上一篇:oracle
下一篇:定義游標
