mysql> explain select * from test where id = 1000; +----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+ | 1 | SIMPLE | test | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL | +----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
2.2、primary
最外层的select查询
1 2 3 4 5 6 7
mysql> explain select * from (select * from test where id = 1000) a; +----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+ | 1 | PRIMARY | <derived2> | system | NULL | NULL | NULL | NULL | 1 | NULL | | 2 | DERIVED | test | const | PRIMARY | PRIMARY | 8 | const | 1 | NULL | +----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+
2.3、union
union中的第二个或随后的select查询,不依赖于外部查询的结果集
1 2 3 4 5 6 7 8
mysql> explain select * from test where id = 1000 union all select * from test2 ; +----+--------------+------------+-------+---------------+---------+---------+-------+-------+-----------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+--------------+------------+-------+---------------+---------+---------+-------+-------+-----------------+ | 1 | PRIMARY | test | const | PRIMARY | PRIMARY | 8 | const | 1 | NULL | | 2 | UNION | test2 | ALL | NULL | NULL | NULL | NULL | 67993 | NULL | | NULL | UNION RESULT | <union1,2> | ALL | NULL | NULL | NULL | NULL | NULL | Using temporary | +----+--------------+------------+-------+---------------+---------+---------+-------+-------+-----------------+
2.4、dependent union
union中的第二个或随后的select查询,依赖于外部查询的结果集
1 2 3 4 5 6 7 8 9
mysql> explain select * from test where id in (select id from test where id = 1000 union all select id from test2) ; +----+--------------------+------------+--------+---------------+---------+---------+-------+-------+-----------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+--------------------+------------+--------+---------------+---------+---------+-------+-------+-----------------+ | 1 | PRIMARY | test | ALL | NULL | NULL | NULL | NULL | 68505 | Using where | | 2 | DEPENDENT SUBQUERY | test | const | PRIMARY | PRIMARY | 8 | const | 1 | Using index | | 3 | DEPENDENT UNION | test2 | eq_ref | PRIMARY | PRIMARY | 8 | func | 1 | Using index | | NULL | UNION RESULT | <union2,3> | ALL | NULL | NULL | NULL | NULL | NULL | Using temporary | +----+--------------------+------------+--------+---------------+---------+---------+-------+-------+-----------------+
2.5、subquery
子查询中的第一个 select 查询,不依赖与外部查询的结果集
1 2 3 4 5 6 7 8
mysql> explain select * from test where id = (select id from test where id = 1000); +----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+ | 1 | PRIMARY | test | const | PRIMARY | PRIMARY | 8 | const | 1 | NULL | | 2 | SUBQUERY | test | const | PRIMARY | PRIMARY | 8 | const | 1 | Using index | +----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+
2.6、dependent subquery
子查询中的第一个select查询,依赖于外部查询的结果集
1 2 3 4 5 6 7 8 9
mysql> explain select * from test where id in (select id from test where id = 1000 union all select id from test2) ; +----+--------------------+------------+--------+---------------+---------+---------+-------+-------+-----------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+--------------------+------------+--------+---------------+---------+---------+-------+-------+-----------------+ | 1 | PRIMARY | test | ALL | NULL | NULL | NULL | NULL | 68505 | Using where | | 2 | DEPENDENT SUBQUERY | test | const | PRIMARY | PRIMARY | 8 | const | 1 | Using index | | 3 | DEPENDENT UNION | test2 | eq_ref | PRIMARY | PRIMARY | 8 | func | 1 | Using index | | NULL | UNION RESULT | <union2,3> | ALL | NULL | NULL | NULL | NULL | NULL | Using temporary | +----+--------------------+------------+--------+---------------+---------+---------+-------+-------+-----------------+
2.7、derived
用于from子句中有子查询的情况,mysql会递归执行这些子查询,此结果集放在临时表中
1 2 3 4 5 6 7 8
mysql> explain select * from (select * from test2 where id = 1000)a; +----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+ | 1 | PRIMARY | <derived2> | system | NULL | NULL | NULL | NULL | 1 | NULL | | 2 | DERIVED | test2 | const | PRIMARY | PRIMARY | 8 | const | 1 | NULL | +----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+
3、table
table用来表示输出行所引用的表名。
4、type(重要)
type表示访问类型,下面一次解释各种类型,类型顺序从最好到最差排列。
4.1、system
表仅有一行,是 cons 类型的一个特例。因为子查询只有一行数据,模拟了单表只有一行数据,此时 type 为 system。
1 2 3 4 5 6 7
mysql> explain select * from (select * from test2 where id = 1000)a; +----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+ | 1 | PRIMARY | <derived2> | system | NULL | NULL | NULL | NULL | 1 | NULL | | 2 | DERIVED | test2 | const | PRIMARY | PRIMARY | 8 | const | 1 | NULL | +----+-------------+------------+--------+---------------+---------+---------+-------+------+-------+
mysql> explain select * from test where id > 1; +----+-------------+-------+-------+---------------+---------+---------+------+-------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+---------------+---------+---------+------+-------+-------------+ | 1 | SIMPLE | test | range | PRIMARY | PRIMARY | 8 | NULL | 34252 | Using where | +----+-------------+-------+-------+---------------+---------+---------+------+-------+-------------+
11.3、using temporary
表示mysql需要使用临时表来存储结果集,常见于排序和分组查询。
1 2 3 4 5 6
mysql> explain select * from test where id in (1,2) group by bnet_id; +----+-------------+-------+-------+-----------------------------------------+---------+---------+------+------+----------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+-----------------------------------------+---------+---------+------+------+----------------------------------------------+ | 1 | SIMPLE | test | range | PRIMARY,IDX(event_key-bnet_Id),idx_bnet | PRIMARY | 8 | NULL | 2 | Using where; Using temporary; Using filesort | +----+-------------+-------+-------+-----------------------------------------+---------+---------+------+------+----------------------------------------------+