sql中in和exists的原理及使用场景。 SQL语句中exists和in的区别

在咱们的工做中可能会遇到这样的情形:html

咱们须要查询a表里面的数据,可是要以b表做为约束。数据库

举个例子,好比咱们须要查询订单表中的数据,可是要以用户表为约束,也就是查询出来的订单的user_id要在用户表里面存在才返回。缓存

表结构和表数据以下:oop

table1 usertb;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(30) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
+----+-----------+
| id | name |
+----+-----------+
| 1 | panchao |
| 2 | tangping |
| 3 | yinkaiyue |
+----+-----------+post


table2 ordertb;
+------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | YES | | NULL | |
| order_name | varchar(50) | YES | | NULL | |
+------------+-------------+------+-----+---------+----------------+
+----+---------+-------------------+
| id | user_id | order_name |
+----+---------+-------------------+
| 1 | 1 | tangping's order |
| 2 | 2 | yinkaiyue's order |
| 3 | 0 | zhangtian's order |
+----+---------+-------------------+性能

 

看过表事后,你们在脑海中可能已经想出了不少方法了,对吧。url

主要三种方法:left join、in、exists。spa

咱们分别来看看。他们的查询结果和explain的结果。htm

一、left join:blog

MariaDB [test]> select * from ordertb a left join usertb b on a.user_id = b.id;
+----+---------+-------------------+------+----------+
| id | user_id | order_name | id | name |
+----+---------+-------------------+------+----------+
| 1 | 1 | tangping's order | 1 | panchao |
| 2 | 2 | yinkaiyue's order | 2 | tangping |
| 3 | 0 | zhangtian's order | NULL | NULL |
+----+---------+-------------------+------+----------+
MariaDB [test]> explain select * from ordertb a left join usertb b on a.user_id= b.id;
+------+-------------+-------+--------+---------------+---------+---------+----------------+------+-------------+
| id | select_type | table | type | possible_keys | key      | key_len | ref      | rows | Extra |
+------+-------------+-------+--------+---------------+---------+---------+----------------+------+-------------+
| 1  | SIMPLE      | a      | ALL  | NULL               | NULL   | NULL    | NULL | 3      |          |
| 1 | SIMPLE | b | eq_ref | PRIMARY | PRIMARY | 4 | test.a.user_id | 1 | Using where |
+------+-------------+-------+--------+---------------+---------+---------+----------------+------+-------------+

二、in:

MariaDB [test]> select * from ordertb where ordertb.user_id in (select id from usertb);
+----+---------+-------------------+
| id | user_id | order_name |
+----+---------+-------------------+
| 1 | 1 | tangping's order |
| 2 | 2 | yinkaiyue's order |
+----+---------+-------------------+
MariaDB [test]> explain select * from ordertb where ordertb.user_id in (select id from usertb);
+------+-------------+---------+--------+---------------+---------+---------+----------------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+---------+--------+---------------+---------+---------+----------------------+------+-------------+
| 1 | PRIMARY | ordertb | ALL | NULL | NULL | NULL | NULL | 3 | Using where |
| 1 | PRIMARY | usertb | eq_ref | PRIMARY | PRIMARY | 4 | test.ordertb.user_id | 1 | Using index |
+------+-------------+---------+--------+---------------+---------+---------+----------------------+------+-------------+

三、exists:

MariaDB [test]> select * from ordertb where exists(select 1 from usertb where usertb.id = ordertb.user_id);
+----+---------+-------------------+
| id | user_id | order_name |
+----+---------+-------------------+
| 1 | 1 | tangping's order |
| 2 | 2 | yinkaiyue's order |
+----+---------+-------------------+
MariaDB [test]> explain select * from ordertb where exists(select 1 from usertbwhere usertb.id = ordertb.user_id);
+------+-------------+---------+--------+---------------+---------+---------+----------------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+---------+--------+---------------+---------+---------+----------------------+------+-------------+
| 1 | PRIMARY | ordertb | ALL | NULL | NULL | NULL | NULL | 3 | Using where |
| 1 | PRIMARY | usertb | eq_ref | PRIMARY | PRIMARY | 4 | test.ordertb.user_id | 1 | Using index |
+------+-------------+---------+--------+---------------+---------+---------+----------------------+------+-------------+

 

咱们能够看到,这三种查询的explain结果大体相同,惟一不一样的是left join中的Extra没有用到Useing Where。说明left join相比于其余两个查询效率要低一些,而且left join中有冗余数据。

咱们再来看 in 和 exists ,从表面上来看好像xiaolv同样。其实否则。咱们来深刻分析一下这两个语句。

一、in。

其中usertb咱们用B来代替,ordertb咱们用A来代替。

in()只执行一次,它查出B表中的全部id字段并缓存起来.以后,检查A表的user_id是否与B表中的id相等,若是相等则将A表的记录加入结果集中,直到遍历完A表的全部记录. 它的查询过程相似于如下过程

List resultSet=[]; Array A=(select * from A); Array B=(select id from B);
for(int i=0;i<A.length;i++) {    for(int j=0;j<B.length;j++) {       if(A[i].id==B[j].id) {          resultSet.add(A[i]);          break;       }    } } return resultSet;

能够看出,当B表数据较大时不适合使用in(),由于它会B表数据所有遍历一次. 如:A表有10000条记录,B表有1000000条记录,那么最多有可能遍历10000*1000000次,效率不好. 再如:A表有10000条记录,B表有100条记录,那么最多有可能遍历10000*100次,遍历次数大大减小,效率大大提高.

二、exists。

exists()会执行A.length次,它并不缓存exists()结果集,由于exists()结果集的内容并不重要,重要的是结果集中是否有记录,若是有则返回true,没有则返回false. 它的查询过程相似于如下过程

List resultSet=[]; Array A=(select * from A)
for(int i=0;i<A.length;i++) {    if(exists(A[i].id) {    //执行select 1 from B b where b.id=a.id是否有记录返回        resultSet.add(A[i]);    } } return resultSet;

当B表比A表数据大时适合使用exists(),由于它没有那么遍历操做,只须要再执行一次查询就行. 如:A表有10000条记录,B表有1000000条记录,那么exists()会执行10000次去判断A表中的id是否与B表中的id相等. 如:A表有10000条记录,B表有100000000条记录,那么exists()仍是执行10000次,由于它只执行A.length次,可见B表数据越多,越适合exists()发挥效果. 再如:A表有10000条记录,B表有100条记录,那么exists()仍是执行10000次,还不如使用in()遍历10000*100次,由于in()是在内存里遍历比较,而exists()须要查询数据库,咱们都知道查询数据库所消耗的性能更高,而内存比较很快.

 

结论:exists()适合B表比A表数据大的状况

当A表数据与B表数据同样大时,in与exists效率差很少,可任选一个使用.

区别及应用场景

in 和 exists的区别:

若是子查询得出的结果集记录较少,主查询中的表较大且又有索引时应该用in, 反之若是外层的主查询记录较少,子查询中的表大,又有索引时使用exists。其实咱们区分in和exists主要是形成了驱动顺序的改变(这是性能变化的关键),若是是exists,那么之外层表为驱动表,先被访问,若是是IN,那么先执行子查询,因此咱们会以驱动表的快速返回为目标,那么就会考虑到索引及结果集的关系了 ,另外IN时不对NULL进行处理。

in 是把外表和内表做hash 链接,而exists是对外表做loop循环,每次loop循环再对内表进行查询。一直以来认为exists比in效率高的说法是不许确的。

 

更多细节,能够参考如下博客(SQL语句中exists和in的区别),由于我也是看了这个博客写的文章。

相关文章
相关标签/搜索