先看一下第一种用法:
- mysql> explain t_order;
- +----------+------------------+------+-----+---------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +----------+------------------+------+-----+---------+----------------+
- | order_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
- | add_time | int(10) | NO | | NULL | |
- +----------+------------------+------+-----+---------+----------------+
- 2 rows in set (0.04 sec)
这其实和describe是等价的:
- mysql> describe t_order;
- +----------+------------------+------+-----+---------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +----------+------------------+------+-----+---------+----------------+
- | order_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
- | add_time | int(10) | NO | | NULL | |
- +----------+------------------+------+-----+---------+----------------+
- 2 rows in set (0.00 sec)
这返回的是表的结构,没有什么可说的。
重点是第二种用法,需要深入的了解。
先看一个例子:
- mysql> explain select * from t_order;
- +----+-------------+---------+------+---------------+------+---------+------+--------+-------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
- +----+-------------+---------+------+---------------+------+---------+------+--------+-------+
- | 1 | SIMPLE | t_order | ALL | NULL | NULL | NULL | NULL | 100453 | |
- +----+-------------+---------+------+---------------+------+---------+------+--------+-------+
- 1 row in set (0.03 sec)
加上extended后之后:
- mysql> explain extended select * from t_order;
- +----+-------------+---------+------+---------------+------+---------+------+--------+----------+-------+
- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
- +----+-------------+---------+------+---------------+------+---------+------+--------+----------+-------+
- | 1 | SIMPLE | t_order | ALL | NULL | NULL | NULL | NULL | 100453 | 100.00 | |
- +----+-------------+---------+------+---------------+------+---------+------+--------+----------+-------+
- 1 row in set, 1 warning (0.00 sec)
有必要解释一下这个长长的表格里每一列的含义:
| id | SELECT识别符。这是SELECT的查询序列号 |
| select_type | SELECT类型,可以为以下任何一种:
|
| table | 输出的行所引用的表 |
| type | 联接类型。下面给出各种联接类型,按照从最佳类型到最坏类型进行排序:
|
| possible_keys | 指出MySQL能使用哪个索引在该表中找到行 |
| key | 显示MySQL实际决定使用的键(索引)。如果没有选择索引,键是NULL。 |
| key_len | 显示MySQL决定使用的键长度。如 |