mysql>explainselect*from emp e join dept d on e.deptno = d.deptno;+----+-------------+-------+------------+------+---------------+-----------+---------+-----------------+------+----------+-------+| 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| ref | fk_deptno | fk_deptno |5| oracle.d.deptno |1|100.00|NULL|+----+-------------+-------+------------+------+---------------+-----------+---------+-----------------+------+----------+-------+
1
2
3
4
5
6
7
2、如果id不同,如果是子查询,id的序号会递增,id值越大优先级越高,越先被执行。
mysql>explainselect*from emp e whereexists(select d.deptno from dept d where d.dname ='SALES'and d.deptno=e.deptno);+----+--------------------+-------+------------+--------+---------------+---------+---------+-----------------+------+----------+-------------+| id | select_type |table| partitions |type| possible_keys |key| key_len | ref |rows| filtered | Extra |+----+--------------------+-------+------------+--------+---------------+---------+---------+-----------------+------+----------+-------------+|1|PRIMARY| e |NULL|ALL|NULL|NULL|NULL|NULL|14|100.00|Usingwhere||2| DEPENDENT SUBQUERY | d |NULL| eq_ref |PRIMARY|PRIMARY|4| oracle.e.deptno |1|25.00|Usingwhere|+----+--------------------+-------+------------+--------+---------------+---------+---------+-----------------+------+----------+-------------+
mysql>explainselect*from emp e join salgrade sg on e.sal between sg.losal and sg.hisal whereexists(select d.deptno from dept d where d.dname ='SALES'and d.deptno=e.deptno);+----+--------------------+-------+------------+--------+---------------+---------+---------+-----------------+------+----------+----------------------------------------------------+| id | select_type |table| partitions |type| possible_keys |key| key_len | ref |rows| filtered | Extra |+----+--------------------+-------+------------+--------+---------------+---------+---------+-----------------+------+----------+----------------------------------------------------+|1|PRIMARY| sg |NULL|ALL|NULL|NULL|NULL|NULL|5|100.00|NULL||1|PRIMARY| e |NULL|ALL|NULL|NULL|NULL|NULL|14|11.11|Usingwhere;Usingjoin buffer (Block Nested Loop)||2| DEPENDENT SUBQUERY | d |NULL| eq_ref |PRIMARY|PRIMARY|4| oracle.e.deptno |1|25.00|Usingwhere|+----+--------------------+-------+------------+--------+---------------+---------+---------+-----------------+------+----------+----------------------------------------------------+
1
2
3
4
5
6
7
8
select_type
select_type主要用来分辨查询的类型,是普通查询还是联合查询还是子查询。
select_type Value
Meaning
SIMPLE
Simple SELECT (not using UNION or subqueries)
PRIMARY
Outermost SELECT
UNION
Second or later SELECT statement in a UNION
DEPENDENT UNION
Second or later SELECT statement in a UNION, dependent on outer query
UNION RESULT
Result of a UNION.
SUBQUERY
First SELECT in subquery
DEPENDENT SUBQUERY
First SELECT in subquery, dependent on outer query
DERIVED
Derived table
UNCACHEABLE SUBQUERY
A subquery for which the result cannot be cached and must be re-evaluated for each row of the outer query
UNCACHEABLE UNION
The second or later select in a UNION that belongs to an uncacheable subquery (see UNCACHEABLE SUBQUERY)
simple:简单查询,不包含子查询和union。
mysql>explainselect*from emp;+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+| id | select_type |table| partitions |type| possible_keys |key| key_len | ref |rows| filtered | Extra |+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+|1|SIMPLE| emp |NULL|ALL|NULL|NULL|NULL|NULL|14|100.00|NULL|+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
mysql>explainselect*from emp e whereexists(select d.deptno from dept d where d.dname ='SALES'and d.deptno=e.deptno);+----+--------------------+-------+------------+--------+---------------+---------+---------+-----------------+------+----------+-------------+| id | select_type |table| partitions |type| possible_keys |key| key_len | ref |rows| filtered | Extra |+----+--------------------+-------+------------+--------+---------------+---------+---------+-----------------+------+----------+-------------+|1|PRIMARY| e |NULL|ALL|NULL|NULL|NULL|NULL|14|100.00|Usingwhere||2| DEPENDENT SUBQUERY | d |NULL| eq_ref |PRIMARY|PRIMARY|4| oracle.e.deptno |1|25.00|Usingwhere|+----+--------------------+-------+------------+--------+---------------+---------+---------+-----------------+------+----------+-------------+
1
2
3
4
5
6
7
union:若第二个select出现在union之后,则被标记为union。
union result:从union表获取结果的select。
mysql>explainselect*from emp where deptno =10unionselect*from emp where sal >2000;+----+--------------+------------+------------+------+---------------+-----------+---------+-------+------+----------+-----------------+| id | select_type |table| partitions |type| possible_keys |key| key_len | ref |rows| filtered | Extra |+----+--------------+------------+------------+------+---------------+-----------+---------+-------+------+----------+-----------------+|1|PRIMARY| emp |NULL| ref | fk_deptno | fk_deptno |5| const |3|100.00|NULL||2|UNION| emp |NULL|ALL|NULL|NULL|NULL|NULL|14|33.33|Usingwhere||NULL|UNION RESULT |<union1,2>|NULL|ALL|NULL|NULL|NULL|NULL|NULL|NULL|Usingtemporary|+----+--------------+------------+------------+------+---------------+-----------+---------+-------+------+----------+-----------------+
using index:这个表示当前的查询是覆盖索引的,直接从索引中读取数据,而不用访问数据表(回表)。如果同时出现using where表明索引被用来执行索引键值的查找,如果没有,表明索引被用来读取数据,而不是真的查找。
mysql>explainselect deptno,count(*)from emp group
by deptno limit10;+----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+-------------+| id | select_type |table| partitions |type| possible_keys |key| key_len | ref |rows| filtered | Extra |+----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+-------------+|1|SIMPLE| emp |NULL|index| fk_deptno | fk_deptno |5|NULL|10|100.00|Usingindex|+----+-------------+-------+------------+-------+---------------+-----------+---------+------+------+----------+-------------+
1
2
3
4
5
6
using where:使用where进行条件过滤。
mysql>explainselect*from emp where empno >7000;+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+| id | select_type |table| partitions |type| possible_keys |key| key_len | ref |rows| filtered | Extra |+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+|1|SIMPLE| emp |NULL| range |PRIMARY|PRIMARY|4|NULL|14|100.00|Usingwhere|+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
1
2
3
4
5
6
using join buffer:使用连接缓存。
mysql>explainselect*from emp e join salgrade sg on e.sal between sg.losal and sg.hisal whereexists(select d.deptno from dept d where d.dname ='SALES'and d.deptno=e.deptno);+----+--------------------+-------+------------+--------+---------------+---------+---------+-----------------+------+----------+----------------------------------------------------+| id | select_type |table| partitions |type| possible_keys |key| key_len | ref |rows| filtered | Extra |+----+--------------------+-------+------------+--------+---------------+---------+---------+-----------------+------+----------+----------------------------------------------------+|1|PRIMARY| sg |NULL|ALL|NULL|NULL|NULL|NULL|5|100.00|NULL||1|PRIMARY|
e |NULL|ALL|NULL|NULL|NULL|NULL|14|11.11|Usingwhere;Usingjoin buffer (Block Nested Loop)||2| DEPENDENT SUBQUERY | d |NULL| eq_ref |PRIMARY|PRIMARY|4| oracle.e.deptno |1|25.00|Usingwhere|+----+--------------------+-------+------------+--------+---------------+---------+---------+-----------------+------+----------+----------------------------------------------------+
1
2
3
4
5
6
7
8
no matching row in const table:where语句的结果总是false。
mysql>explainselect*from emp where empno =7469;+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+--------------------------------+| id | select_type |table| partitions |type| possible_keys |key| key_len | ref |rows| filtered | Extra |+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+--------------------------------+|1|SIMPLE|NULL|NULL|NULL|NULL|NULL|NULL|NULL|NULL|NULL|no matching rowin const table|+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+--------------------------------+