MySQLnotin嵌套查询如何改写成外连接方式
这期内容当中小编将会给大家带来有关MySQL not in嵌套查询如何改写成外连接方式,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
公司主营业务:成都网站建设、网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联推出通川免费做网站回馈大家。
在MySQL中,not in 嵌套查询会在数据库里面创建一张临时表,导致执行效率很低,可以改成外连接的方式处理,效率会好很多。
not in方式
mysql> select * from dept where deptno not in (select deptno from emp);
+--------+------------+---------+
| deptno | dname | loc |
+--------+------------+---------+
| 40 | OPERATIONS | BOSTON |
| 50 | Research | BeiJing |
+--------+------------+---------+
2 rows in set (0.00 sec)
mysql> explain select * from dept where deptno not in (select deptno from emp);
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| 1 | PRIMARY | dept | ALL | NULL | NULL | NULL | NULL | 5 | Using where |
| 2 | SUBQUERY | emp | ALL | NULL | NULL | NULL | NULL | 14 | NULL |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
2 rows in set (0.00 sec)
外连接方式
mysql> select * from dept e left join emp d on e.deptno=d.deptno where d.deptno is null;
+--------+------------+---------+-------+-------+------+------+----------+------+------+--------+
| deptno | dname | loc | empno | ename | job | mgr | hiredate | sal | com | deptno |
+--------+------------+---------+-------+-------+------+------+----------+------+------+--------+
| 40 | OPERATIONS | BOSTON | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 50 | Research | BeiJing | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
+--------+------------+---------+-------+-------+------+------+----------+------+------+--------+
2 rows in set (0.00 sec)
mysql> explain select * from dept e left join emp d on e.deptno=d.deptno where d.deptno is null;
+----+-------------+-------+------+---------------+------+---------+------+------+----------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------------------------------------------+
| 1 | SIMPLE | e | ALL | NULL | NULL | NULL | NULL | 5 | NULL |
| 1 | SIMPLE | d | ALL | NULL | NULL | NULL | NULL | 14 | Using where; Using join buffer (Block Nested Loop) |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------------------------------------------+
2 rows in set (0.00 sec)
上述就是小编为大家分享的MySQL not in嵌套查询如何改写成外连接方式了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。
当前题目:MySQLnotin嵌套查询如何改写成外连接方式
链接分享:http://scjbc.cn/article/pcpoej.html