浅析MySQL中exists与in的使用

1.select * from A where A.id in (select AId from B )
2.select * from A where A.id exits (select * from B where B.AId = A.id )

1的过程类似于:

Array B=select AId from B;
for(b:B){
  select * from A where A.id = b;
}

走的是A的索引,in所以适合数据量A>B的情况
2的过程类似于:

Array A = select * from A;
for(a:A){
  select * from B where B.AId = a.id
}

走的是B的索引,所以适合数据量B>A的情况

3.select * from A where A.id not in (select AId from B )
4.select * from A where A.id not exits (select * from B where B.AId = A.id )
3的效果类似于

Array B=select AId from B;
for(b:B){
  select * from A where A.id != b;
}

!=是不走索引的,走的全文扫描
4的效果类似于

Array A = select * from A ;
for(a:A){
  !(select * from B where B.AId = a.id)
}

还是走的B的索引

转载自http://sunxiaqw.blog.163.com/blog/static/990654382013430105130443/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。