環境:oracle 11g
現有a表與b表通過a01字段關聯,要查詢出a表的數據在b表沒有數據的數據;sql如下
select count(1) from (select a.*,(select count(1) from b where b.a01=a.a01) as flag from a) where flag=0
因為flag是虛擬字段沒有走不了索引導致這條sql執行起來特別慢 310W條數據查總數花費2秒左右。
利用not exists優化sql如下
select count(1) from a where not exists(select 1 from b where a.a01=b.b01)
利用not exists走索引,執行花費時間大約為0.2秒
