
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Using EXPLAIN keyword in MySQL
MySQL EXPLAIN gives a query execution plan. EXPLAIN can be used in the beginning with SELECT, INSERT, DELETE, REPLACE, and UPDATE.
To avoid the complete table scan in database, you need to use index. Let us first create a table −
mysql> create table DemoTable1488 -> ( -> StudentId int, -> StudentName varchar(20), -> StudentAge int -> ); Query OK, 0 rows affected (2.18 sec)
Here is the query to create index −
mysql> create index student_id_index on DemoTable1488(StudentId); Query OK, 0 rows affected (0.90 sec) Records: 0 Duplicates: 0 Warnings: 0
insert into DemoTable1488 valueInsert some records in the table using insert command −
mysql> insert into DemoTable1488 values(101,'Sam',21); Query OK, 1 row affected (0.32 sec) mysql> insert into DemoTable1488 values(102,'Bob',23); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable1488 values(103,'David',20); Query OK, 1 row affected (0.21 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1488;
This will produce the following output −
+-----------+-------------+------------+ | StudentId | StudentName | StudentAge | +-----------+-------------+------------+ | 101 | Sam | 21 | | 102 | Bob | 23 | | 103 | David | 20 | +-----------+-------------+------------+ 3 rows in set (0.00 sec)
Now, use EXPLAIN −
mysql> explain select * from DemoTable1488 where StudentId=1;
This will produce the following output −
+----+-------------+---------------+------------+------+------------------+------------------+---------+-------+------+----------+-------+ | 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)
- Related Articles
- Fetch info with MySQL EXPLAIN KEYWORD?
- Explain structures using typedef keyword in C language
- Explain Keyword driven framework.
- Explain JavaScript "this" keyword?
- The MySQL EXPLAIN keyword executes the query or just explains the query?
- Explain the importance of import keyword in java?
- Using the new keyword in C#
- How to implement a Keyword Search in MySQL?
- What does the KEY keyword mean in MySQL?
- MySQL CREATE statement with KEY keyword
- Perform MySQL ORDER BY keyword match?
- Create variables in MySQL stored procedure with DECLARE keyword
- Explain the use of AVG() function in MySQL using Python?
- Selecting a column that is also a keyword in MySQL?
- Using final keyword to Prevent Overriding in Java

Advertisements