子查询与连表查询
标签:数据库
子查询
mysql> explain select e.empno,e.ename,(select dname from dept d where e.deptno = d.deptno) as dname from emp e where e.deptno = 1;
+----+--------------------+-------+------------+--------+---------------+---------+---------+----------------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+--------------------+-------+------------+--------+---------------+---------+---------+----------------+------+----------+-------------+
| 1 | PRIMARY | e | NULL | ALL | NULL | NULL | NULL | NULL | 47 | 10.00 | Using where |
| 2 | DEPENDENT SUBQUERY | d | NULL | eq_ref | PRIMARY | PRIMARY | 4 | scott.e.deptno | 1 | 100.00 | NULL |
+----+--------------------+-------+------------+--------+---------------+---------+---------+----------------+------+----------+-------------+
2 rows in set (0.07 sec)
连表查询
mysql> explain select e.empno,e.ename,d.dname from emp e join dept d on e.deptno = d.deptno where e.deptno = 1;
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------------+
| 1 | SIMPLE | d | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL |
| 1 | SIMPLE | e | NULL | ALL | NULL | NULL | NULL | NULL | 47 | 10.00 | Using where |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------------+
2 rows in set (0.22 sec)
可以看出连表查询被优化器优化成了dept表关联empt表
子查询
mysql> explain select e.empno,e.ename,(select dname from dept d where e.deptno = d.deptno) as dname from emp e where e.job = '五散人';
+----+--------------------+-------+------------+--------+---------------+---------+---------+----------------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+--------------------+-------+------------+--------+---------------+---------+---------+----------------+------+----------+-------------+
| 1 | PRIMARY | e | NULL | ALL | NULL | NULL | NULL | NULL | 47 | 10.00 | Using where |
| 2 | DEPENDENT SUBQUERY | d | NULL | eq_ref | PRIMARY | PRIMARY | 4 | scott.e.deptno | 1 | 100.00 | NULL |
+----+--------------------+-------+------------+--------+---------------+---------+---------+----------------+------+----------+-------------+
2 rows in set (0.07 sec)
连表查询
mysql> explain select e.empno,e.ename,d.dname from emp e join dept d on e.deptno = d.deptno where e.job = '五散人';
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------------+
| 1 | SIMPLE | d | NULL | ALL | PRIMARY | NULL | NULL | NULL | 4 | 100.00 | NULL |
| 1 | SIMPLE | e | NULL | ALL | NULL | NULL | NULL | NULL | 47 | 2.13 | Using where; Using join buffer (Block Nested Loop) |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------------+
2 rows in set (0.07 sec)
结论
一般认为连表查询效率比子查询高
子查询需要多次扫子表,总共扫1+n
连表查询两个表各扫一次