使用 MySQL EXPLAIN KEYWORD 获取信息?

mysqlmysqli database

首先我们创建一个表 −

mysql> create table DemoTable1541
   -> (
   -> EmployeeId int,
   -> EmployeeFirstName varchar(20) NOT NULL
   -> );
Query OK, 0 rows affected (0.94 sec)

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

mysql> create index emp_name_index on DemoTable1541(EmployeeFirstName);
Query OK, 0 rows affected (0.75 sec)
Records: 0  Duplicates: 0  Warnings: 0

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

mysql> insert into DemoTable1541 values(1,'Robert');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable1541 values(2,'Adam');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable1541 values(3,'Mike');
Query OK, 1 row affected (0.14 sec)

使用 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)

相关文章