exits in left join性能比較 ,筆者使用的是MYSQL數據庫,這三個關鍵字方法在其他的關系數據庫里也是大同小異,如果各種有興趣,自行比較。
我這里有一個249_account 表,總數為8538507 249001_account_temp表 總數為 8242734
其中有部分數據,249_account里有,249_account_temp表里沒有,要求找出249_account多余數據
使用上面三種方法進行查找,三種查到的數據條數都是 294451條
not exits
select count(*) from cunjk.249_account a where not EXISTS (select card_id from bak.249001_account_temp b where a.card_id = b.card_id ); 32.401s
left join
select count(*) from cunjk.249_account a left JOIN bak.249001_account_temp b on a.card_id = b.card_id where b.card_id is null ; 22.888s
in
select count(*) from cunjk.249_account where card_id not in (select card_id from bak.249001_account_temp ); 40.306s
in 用時 40.306s
not exits 32.401s
left join 22.888s
對於數據量的情況
left join 最省時效率更高,in 最效率最低
如果通過上述三種方式,刪除多余的數據:
left join
delete a from cunjk.249_account a left OUTER JOIN bak.249001_account_temp b on a.card_id = b.card_id where b.card_id is null ;
not exits
delete from cunjk.249_account a where not EXISTS (select * from bak.249001_account_temp b where a.card_id = b.card_id ) ;
in
delete from cunjk.249_account where card_id not in (select card_id from bak.249001_account_temp ) ;
總結:雖然in 方法寫起來很簡單,但是性能不夠好,left join 外連接,需要關聯條件,看起來有一些復雜,但是性能最優。
請適合自己的方法
