MySQL-exists和in的区别

SQL查询中in和exists的区别分析

对于一些不可不免的查询场景,我们难免要用到子查询

那么in和exists那个的效率更高一点呢

SQL查询中in和exists的区别分析

select * from A where id in (select id from B);

select * from A where exists (select 1 from B where A.id=B.id);

  


对于以上两种情况,in是在内存里遍历比较,而exists需要查询数据库,所以当B表数据量较大时,exists效率优于in。

原文地址:https://www.cnblogs.com/ricklz/p/10602974.html