您好,登錄后才能下訂單哦!
這期內(nèi)容當中小編將會給大家?guī)碛嘘P(guān)MySQL not in嵌套查詢?nèi)绾胃膶懗赏膺B接方式,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
在MySQL中,not in 嵌套查詢會在數(shù)據(jù)庫里面創(chuàng)建一張臨時表,導致執(zhí)行效率很低,可以改成外連接的方式處理,效率會好很多。
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嵌套查詢?nèi)绾胃膶懗赏膺B接方式了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。