使用MySQL EXPLAIN关键字获取信息?

让我们首先创建一个表-

mysql> create table DemoTable1541
   -> (
   -> EmployeeId int,
   -> EmployeeFirstName varchar(20) NOT NULL
   -> );

这是在列上创建索引的查询-

mysql> create index emp_name_index on DemoTable1541(EmployeeFirstName);
Records: 0  Duplicates: 0  Warnings: 0

使用插入命令在表中插入一些记录-

mysql> insert into DemoTable1541 values(1,'Robert');
mysql> insert into DemoTable1541 values(2,'Adam');
mysql> insert into DemoTable1541 values(3,'Mike');

使用select语句显示表中的所有记录-

mysql> select * from DemoTable1541;

这将产生以下输出-

+------------+-------------------+
| EmployeeId | EmployeeFirstName |
+------------+-------------------+
|          1 | Robert            |
|          2 | Adam              |
|          3 | Mike              |
+------------+-------------------+
3 rows in set (0.00 sec)

以下是使用EXPLAIN的查询-

mysql> explain select * from DemoTable1541 where EmployeeFirstName='Mike';

这将产生以下输出-

+----+-------------+---------------+------------+------+----------------+----------------+---------+-------+------+----------+-------+
| id | select_type | table         | partitions | type | possible_keys  | key            | key_len | ref   | rows | filtered | Extra |
+----+-------------+---------------+------------+------+----------------+----------------+---------+-------+------+----------+-------+
|  1 | SIMPLE      | DemoTable1541 | NULL       | ref  | emp_name_index | emp_name_index | 62      | const |    1 | 100.00   | NULL |
+----+-------------+---------------+------------+------+----------------+----------------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)