Fetch info with MySQL EXPLAIN KEYWORD?


Let us first create a table −

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

Here is the query to create an index on the column −

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

Insert some records in the table using insert command −

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)

Display all records from the table using select statement −

mysql> select * from DemoTable1541;

This will produce the following output −

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

Following is the query to use EXPLAIN −

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

This will produce the following output −

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

Updated on: 12-Dec-2019

99 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements