in和exists哪一个性能更优sql
上面的sql中 订单表中(orders) 存在user_id,而又有用户表(users),因此咱们用orders表中user_id和user表中的id 来in 和 exists。性能优化
1.where后面是小表性能
(1)select count(1) from orders o where o.user_id in(select u.id from users u);优化
(2)select count(1) from orders o where exists (select 1 from users u where u.id = o.user_id);spa
2.where后面是大表blog
(1)select count(1) from users u where u.id in (select o.user_id from orders o);select
(2)select count(1) from users u where exists (select 1 from orders o where o.user_id = u.id);循环
咱们用下面的这两条语句分析:遍历
select count(1) from orders o where o.user_id in(select u.id from users u); select count(1) from orders o where exists (select 1 from users u where u.id = o.user_id);
1.in:先查询in后面的users表,而后再去orders中过滤,也就是先执行子查询,结果出来后,再遍历主查询,遍历主查询是根据user_id和id相等查询的。im
即查询users表至关于外层循环,主查询就是外层循环
小结:in先执行子查询,也就是in()所包含的语句。子查询查询出数据之后,将前面的查询分为n次普通查询(n表示在子查询中返回的数据行数)
2.exists:主查询是内层循环,先查询出orders,查询orders就是外层循环,而后会判断是否是存在order_id和 users表中的id相等,相等才保留数据,查询users表就是内层循环
这里所说的外层循环和内层循环就是咱们所说的嵌套循环,而嵌套循环应该遵循“外小内大”的原则,这就比如你复制不少个小文件和复制几个大文件的区别
小结:若是子查询查到数据,就返回布尔值true;若是没有,就返回布尔值false。返回布尔值true则将该条数据保存下来,不然就舍弃掉。也就是说exists查询,是查询出一条数据就执行一次子查询
小表驱动大表。
in适合于外表大而内表小的状况,exists适合于外表小而内表大的状况。