第二个**‘PARTITIONS’ is deprecated and will be removed in a future**
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
mysql> explain partitions select*from film where id=1; +----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+ | id | select_type |table| partitions | type | possible_keys | key | key_len |ref|rows| filtered | Extra | +----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+ |1| SIMPLE | film |NULL| const |PRIMARY|PRIMARY|4| const |1|100.00|NULL| +----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+ 1rowinset, 2 warnings (0.00 sec)
mysql>show warnings; +---------+------+----------------------------------------------------------------------------------+ | Level | Code | Message | +---------+------+----------------------------------------------------------------------------------+ | Warning |1681|'PARTITIONS'is deprecated and will be removed in a future release. | | Note |1003|/* select#1 */select'1'AS id,'film1'AS name from dbtest.film where1| +---------+------+----------------------------------------------------------------------------------+ 2rowsinset (0.00 sec)
mysql>
所以只使用explain就足够了 。
explain重要列说明
1 2 3 4 5 6 7 8
mysql> explain select*from film_actor a where a.actor_id = (select id from actor where name ='a'); +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+ | id | select_type |table| partitions | type | possible_keys | key | key_len |ref|rows| filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+ |1|PRIMARY| a |NULL|ALL|NULL|NULL|NULL|NULL|3|33.33|Usingwhere| |2| SUBQUERY | actor |NULL|ALL|NULL|NULL|NULL|NULL|3|33.33|Usingwhere| +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+ 2rowsinset, 1 warning (0.00 sec)
mysql> explain select*from film where id=1; +----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+ | id | select_type |table| partitions | type | possible_keys | key | key_len |ref|rows| filtered | Extra | +----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+ |1| SIMPLE | film |NULL| const |PRIMARY|PRIMARY|4| const |1|100.00|NULL| +----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+ 1rowinset, 1 warning (0.00 sec)
mysql>
primary
复杂查询中最外层的 select
1 2 3 4 5 6 7 8
mysql> explain select*from film_actor a where a.actor_id = (select id from actor where name ='a'); +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+ | id | select_type |table| partitions | type | possible_keys | key | key_len |ref|rows| filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+ |1|PRIMARY| a |NULL|ALL|NULL|NULL|NULL|NULL|3|33.33|Usingwhere| |2| SUBQUERY | actor |NULL|ALL|NULL|NULL|NULL|NULL|3|33.33|Usingwhere| +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+ 2rowsinset, 1 warning (0.00 sec)
subquery
包含在select 中的子查询(不在 from 子句中)
1 2 3 4 5 6 7 8
mysql> explain select*from film_actor a where a.actor_id = (select id from actor where name ='a'); +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+ | id | select_type |table| partitions | type | possible_keys | key | key_len |ref|rows| filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+ |1|PRIMARY| a |NULL|ALL|NULL|NULL|NULL|NULL|3|33.33|Usingwhere| |2| SUBQUERY | actor |NULL|ALL|NULL|NULL|NULL|NULL|3|33.33|Usingwhere| +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+ 2rowsinset, 1 warning (0.00 sec)
mysql> explain select (select1from actor where id =1 ) from (select*from film where id =1 ) t ; +----+-------------+------------+------------+--------+---------------+---------+---------+-------+------+----------+-------------+ | id | select_type |table| partitions | type | possible_keys | key | key_len |ref|rows| filtered | Extra | +----+-------------+------------+------------+--------+---------------+---------+---------+-------+------+----------+-------------+ |1|PRIMARY|<derived3>|NULL|system|NULL|NULL|NULL|NULL|1|100.00|NULL| |3| DERIVED | film |NULL| const |PRIMARY|PRIMARY|4| const |1|100.00|NULL| |2| SUBQUERY | actor |NULL| const |PRIMARY|PRIMARY|4| const |1|100.00|Using index | +----+-------------+------------+------------+--------+---------------+---------+---------+-------+------+----------+-------------+ 3rowsinset, 1 warning (0.00 sec)
mysql>
union
在union 中的第二个和随后的 select
1 2 3 4 5 6 7 8 9 10 11
mysql> EXPLAIN select1unionselect1 ; +----+--------------+------------+------------+------+---------------+------+---------+------+------+----------+-----------------+ | id | select_type |table| partitions | type | possible_keys | key | key_len |ref|rows| filtered | Extra | +----+--------------+------------+------------+------+---------------+------+---------+------+------+----------+-----------------+ |1|PRIMARY|NULL|NULL|NULL|NULL|NULL|NULL|NULL|NULL|NULL|No tables used | |2|UNION|NULL|NULL|NULL|NULL|NULL|NULL|NULL|NULL|NULL|No tables used | |NULL|UNIONRESULT|<union1,2>|NULL|ALL|NULL|NULL|NULL|NULL|NULL|NULL|Using temporary | +----+--------------+------------+------------+------+---------------+------+---------+------+------+----------+-----------------+ 3rowsinset, 1 warning (0.00 sec)
mysql>
table
表示explain 的一行正在访问哪个表
1 2 3 4 5 6 7 8 9
mysql> explain select (select1from actor where id =1 ) from (select*from film where id =1 ) t ; +----+-------------+------------+------------+--------+---------------+---------+---------+-------+------+----------+-------------+ | id | select_type |table| partitions | type | possible_keys | key | key_len |ref|rows| filtered | Extra | +----+-------------+------------+------------+--------+---------------+---------+---------+-------+------+----------+-------------+ |1|PRIMARY|<derived3>|NULL|system|NULL|NULL|NULL|NULL|1|100.00|NULL| |3| DERIVED | film |NULL| const |PRIMARY|PRIMARY|4| const |1|100.00|NULL| |2| SUBQUERY | actor |NULL| const |PRIMARY|PRIMARY|4| const |1|100.00|Using index | +----+-------------+------------+------------+--------+---------------+---------+---------+-------+------+----------+-------------+ 3rowsinset, 1 warning (0.00 sec)
mysql> EXPLAIN select*from film_actor a leftjoin film b on a.film_id = b.id ; +----+-------------+-------+------------+--------+---------------+---------+---------+------------------+------+----------+-------+ | id | select_type |table| partitions | type | possible_keys | key | key_len |ref|rows| filtered | Extra | +----+-------------+-------+------------+--------+---------------+---------+---------+------------------+------+----------+-------+ |1| SIMPLE | a |NULL|ALL|NULL|NULL|NULL|NULL|3|100.00|NULL| |1| SIMPLE | b |NULL| eq_ref |PRIMARY|PRIMARY|4| dbtest.a.film_id |1|100.00|NULL| +----+-------------+-------+------------+--------+---------------+---------+---------+------------------+------+----------+-------+ 2rowsinset, 1 warning (0.00 sec)
mysql>show warnings; +-------+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Level | Code | Message | +-------+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Note |1003|/* select#1 */select dbtest.a.id AS id,dbtest.a.film_id AS film_id,dbtest.a.actor_id AS actor_id,dbtest.a.remark AS remark,dbtest.b.id AS id,dbtest.b.name AS name from dbtest.film_actor a leftjoin dbtest.film b on((dbtest.b.id = dbtest.a.film_id)) where1| +-------+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1rowinset (0.00 sec)