MySQL EXPLAIN给出了查询执行计划。EXPLAIN可以与SELECT,INSERT,DELETE,REPLACE和UPDATE一起使用。
为了避免对数据库进行完整的表扫描,您需要使用索引。让我们首先创建一个表-
mysql> create table DemoTable1488 -> ( -> StudentId int, -> StudentName varchar(20), -> StudentAge int -> );
这是创建索引的查询-
mysql> create index student_id_index on DemoTable1488(StudentId); Records: 0 Duplicates: 0 Warnings: 0
插入DemoTable1488值使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1488 values(101,'Sam',21); mysql> insert into DemoTable1488 values(102,'Bob',23); mysql> insert into DemoTable1488 values(103,'David',20);
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1488;
这将产生以下输出-
+-----------+-------------+------------+ | StudentId | StudentName | StudentAge | +-----------+-------------+------------+ | 101 | Sam | 21 | | 102 | Bob | 23 | | 103 | David | 20 | +-----------+-------------+------------+ 3 rows in set (0.00 sec)
现在,使用EXPLAIN-
mysql> explain select * from DemoTable1488 where StudentId=1;
这将产生以下输出-
+----+-------------+---------------+------------+------+------------------+------------------+---------+-------+------+----------+-------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+---------------+------------+------+------------------+------------------+---------+-------+------+----------+-------+ | 1 | SIMPLE | DemoTable1488 | NULL | ref | student_id_index | student_id_index | 5 | const | 1 | 100.00 | NULL | +----+-------------+---------------+------------+------+------------------+------------------+---------+-------+------+----------+-------+ 1 row in set, 1 warning (0.00 sec)