EXPLAIN关键字告诉MySQL如何执行查询。让我们首先创建一个表-
create table DemoTable1375 -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> FirstName varchar(20), -> INDEX FIRST_INDEX(FirstName) -> );
使用插入命令在表中插入一些记录-
insert into DemoTable1375(FirstName) values('Chris'); insert into DemoTable1375(FirstName) values('Bob'); insert into DemoTable1375(FirstName) values('Sam'); insert into DemoTable1375(FirstName) values('David');
使用select语句显示表中的所有记录-
select * from DemoTable1375;
这将产生以下输出-
+----+-----------+ | Id | FirstName | +----+-----------+ | 2 | Bob | | 1 | Chris | | 4 | David | | 3 | Sam | +----+-----------+ 4 rows in set (0.00 sec)
这是使用MySQL EXPLAIN的查询-
explain select * from DemoTable1375;
这将产生以下输出-
+----+-------------+---------------+------------+-------+---------------+-------------+---------+------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+---------------+------------+-------+---------------+-------------+---------+------+------+----------+-------------+ | 1 | SIMPLE | DemoTable1375 | NULL | index | NULL | FIRST_INDEX | 63 | NULL | 4 | 100.00 | Using index | +----+-------------+---------------+------------+-------+---------------+-------------+---------+------+------+----------+-------------+ 1 row in set, 1 warning (0.03 sec)