
在使用explain关键字时,只需要在所执行语句前加上explain即可
- mysql> explain select * from stu where id=3;
- +----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
- | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
- +----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
- | 1 | SIMPLE | stu | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL |
- +----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
- 1 row in set, 1 warning (0.00 sec)



- mysql> explain select * from stu where id=3;
- +----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
- | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
- +----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
- | 1 | SIMPLE | stu | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL |
- +----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
- 1 row in set, 1 warning (0.00 sec)
- mysql> explain select 'a';
- +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+
- | 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 tables used |
- +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+
- 1 row in set, 1 warning (0.00 sec)

显示这张表上可能用到的索引,一个或者多个
实际使用的索引,如果为NULL,则没有使用索引
表示索引中使用的字节数,该值为索引字段最大可能长度,并非实际使用长度,在不损失精确性的前提下,长度越短越好
MySQL认为必须要执行查询的行数,在innodb引擎的表中,是一个估计值,可能并不总是准确的。
表示返回结果的行数栈读取行数的百分比,filtered的值越大越好