The MySQL EXPLAIN keyword executes the query or just explains the query?


The EXPLAIN keyword tells how MySQL executes the query. Let us first create a table −

mysql> create table DemoTable1375
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> FirstName varchar(20),
   -> INDEX FIRST_INDEX(FirstName)
   -> );
Query OK, 0 rows affected (0.73 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1375(FirstName) values('Chris');
Query OK, 1 row affected (0.25 sec)
mysql> insert into DemoTable1375(FirstName) values('Bob');
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable1375(FirstName) values('Sam');
Query OK, 1 row affected (1.06 sec)
mysql> insert into DemoTable1375(FirstName) values('David');
Query OK, 1 row affected (0.09 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1375;

This will produce the following output −

+----+-----------+
| Id | FirstName |
+----+-----------+
|  2 | Bob       |
|  1 | Chris     |
|  4 | David     |
|  3 | Sam       |
+----+-----------+
4 rows in set (0.00 sec)

Here is the query using MySQL EXPLAIN −

mysql> explain  select * from DemoTable1375;

This will produce the following output −

+----+-------------+---------------+------------+-------+---------------+-------------+---------+------+------+----------+-------------+
| 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)

Updated on: 11-Nov-2019

54 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements