下面来分析为什么用用not exists 代替not in
有两个简单例子,以说明 “exists”和“in”的效率问题
1) select * from t1 where exists(select 1 from t2 where t1.a=t2.a) ;
t1数据量小而t2数据量非常大时,t1<<t2 时,1) 的查询效率高。
2) select * from t1 where t1.a in (select t2.a from t2) ;
t1数据量非常大而t2数据量小时,t1>>t2 时,2) 的查询效率高。
有两个简单例子,以说明 “exists”和“in”的效率问题
1) select * from t1 where exists(select 1 from t2 where t1.a=t2.a) ;
t1数据量小而t2数据量非常大时,t1<<t2 时,1) 的查询效率高。
2) select * from t1 where t1.a in (select t2.a from t2) ;
t1数据量非常大而t2数据量小时,t1>>t2 时,2) 的查询效率高。