explain 是 MySQL 提供的 命令, 它可以对 SELECT 语句进行分析, 并输出 SELECT 执行的详细信息, 以供开发人员针对性优化.
示例
explain select * from users where user_id=5 and enable=1;
结果:
mysql> explain select * from articles where id=27;
+----+-------------+----------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| 1 | SIMPLE | articles | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL |
+----+-------------+----------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)
字段解释:
* id: SELECT 查询的标识符. 每个 SELECT 都会自动分配一个唯一的标识符.,根据id的顺序执行
* select_type: SELECT 查询的类型.
* table: 查询的是哪个表
* partitions: 匹配的分区
* type: 显示连接使用了何种类型,这个参数非常重要,结果值从好到坏依次是:system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL
* possible_keys: 此次查询中可能选用的索引
* key: 此次查询中确切使用到的索引.
* ref: 显示索引的哪一列被使用了,如果可能的话,是一个常数
* rows: 显示此查询一共扫描了多少行.
* filtered: 表示此查询条件所过滤的数据的百分比
* extra: 额外的信息息