exist和in的区别:
select * from table1 where id in (select id from table2) ;
select * from table1 where id exists (select id from table2);
对于上述查询得到的结果是相同的,但是底层执行过程不同
使用in语句:先查子表再查主表
先查询table2从表,即先执行select id from table2;
对于主表table1数据较多时,我们使用in速度比exist更快!(会使用索引)
使用exist语句:先查主表再查从表
先查询table1主表,即先执行select * from table1;
从表table2较大时,使用exist查询速度更快(使用索引)