1. Home
  2. Articles
  3. Courses
    1. Articles
  4. Community
  5. Definitions
  6. Files
    1. Terms Of Condition
  • Login
  • Register
  • Search
Course Articles
  • Everywhere
  • Articles
  • Pages
  • Forum
  • Definitions
  • Course Articles
  • Filebase Entry
  • More Options
  1. Scientific Tools
  2. Courses
  3. MySQL Guides

What is EXPLAIN statement in MySQL and what it does ?

  • ScientificTools.org
  • November 28, 2023 at 6:16 PM
  • 994 times read
Contents [hideshow]
  1. What is the EXPLAIN Statement?
  2. Why Use EXPLAIN?
  3. Basic Usage of EXPLAIN
    1. Example 1: Basic EXPLAIN
  4. Understanding EXPLAIN Output
    1. Example 2: Understanding EXPLAIN Output
  5. Using EXPLAIN with Joins
    1. Example 3: EXPLAIN with a Join

The EXPLAIN statement in MySQL is a powerful tool that provides insight into how MySQL executes a query. In this guide, we will talk about EXPLAIN and how to use it in MySQL, including practical examples.

What is the EXPLAIN Statement?

EXPLAIN is a MySQL statement used to obtain a query execution plan, which shows how MySQL will execute a query. It displays information about the query execution path, such as which indexes are used, how tables are joined, and how data is accessed.

Why Use EXPLAIN?

Using EXPLAIN helps in:

  1. Identifying inefficient queries.
  2. Understanding how MySQL processes a query.
  3. Optimizing query performance by tweaking indexes and query structures.

Basic Usage of EXPLAIN

To use EXPLAIN, simply prepend it to any SELECT query.

Example 1: Basic EXPLAIN

Suppose we have a table named Employees.

Query:

SQL
SELECT * FROM Employees WHERE Department = 'Sales';


Using EXPLAIN:

SQL
EXPLAIN SELECT * FROM Employees WHERE Department = 'Sales';

This will show how MySQL plans to execute the query, including which indexes, if any, it will use.

Understanding EXPLAIN Output

The output of EXPLAIN includes several columns, each providing valuable information:

  1. id: The sequence number of the SELECT within the query.
  2. select_type: The type of SELECT (e.g., SIMPLE, SUBQUERY).
  3. table: The table referred to by the row.
  4. type: The join type (e.g., ALL, index, range).
  5. possible_keys: Which indexes MySQL can choose from for this query.
  6. key: The index MySQL decided to use.
  7. rows: Estimated number of rows to be examined.
  8. Extra: Additional information about how MySQL will execute the query.

Example 2: Understanding EXPLAIN Output

Consider the following EXPLAIN output for the previous query:

Code
+----+-------------+-----------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table     | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+-----------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | Employees | ALL  | NULL          | NULL | NULL    | NULL | 1000 | Using where |
+----+-------------+-----------+------+---------------+------+---------+------+------+-------------+
  • type = ALL: Indicates a full table scan, which can be inefficient for large tables.
  • possible_keys = NULL: No potential indexes are considered.
  • Extra = Using where: Shows that a WHERE clause is used to filter the results.

Using EXPLAIN with Joins

EXPLAIN is also incredibly useful for queries involving joins, as it shows how MySQL joins tables.

Example 3: EXPLAIN with a Join

Assume we have another table, Departments, and we perform a join with Employees.

Query:

SQL
SELECT Employees.Name, Departments.DepartmentName FROM Employees JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;


Using EXPLAIN:

SQL
EXPLAIN SELECT Employees.Name, Departments.DepartmentName FROM Employees JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;

This EXPLAIN statement will reveal how the join is executed, including which indexes are utilized for the join conditions.

  • Previous Article Guide to Query Caching in MySQL
  • Next Article MySQL SELECT FROM Statement Guide

Categories

  • MySQL

Archive

  1. 2023 (33)
    1. November (27)
      • Guide to MySQL IN and NOT IN Operators
      • Guide to MySQL OR Operator
      • Guide to MySQL AND Operator
      • Guide to MySQL DISTINCT Clause
      • MySQL SELECT FROM Statement Guide
      • What is EXPLAIN statement in MySQL and what it does ?
      • Guide to Query Caching in MySQL
      • How to Optimize MySQL Queries?
      • Guide to Entity-Relationship (ER) Diagrams in MySQL with Examples
      • What is Normal Forms and Normalization in MySQL
      • How to design a MySQL database with high performance
      • Principals of Secure Database Design in MySQL
      • How to secure and harden MySQL
      • User Management MySQL: Guide to Create, Manage and Permissions
      • Guide to Views in MySQL: Creating Updating Deleting
      • Guide to Stored Procedures and Functions in MySQL
      • Guide to Indexes in MySQL: Creating and Managing Indexes
      • Advanced Filtering in MySQL guide and examples
      • Practical guide for UNION and UNION ALL in MySQL with examples
      • Joining Tables in MySQL guide with examples
      • Guide to GROUP BY and HAVING clauses in MySQL with examples
      • MySQL ORDER BY Clause guide with practical examples
      • Complete MySQL WHERE Clause guide with examples
      • What is a MySQL subquery and how are subqueries executed in MySQL?
      • How to do queries in MySQL
      • How to create, modify, rename and delete tables using MySQL
      • MySQL CRUD Operations (Create, Read, Update, Delete)
    2. October (2)
    3. August (2)
    4. July (2)
  1. Privacy Policy
  2. Legal Notice
Copyright© ScientificTools.org 2026. All rights reserved.
All the content posted on this website are licenses by MySecure Space GmbH under Creative Commons CC BY-NC-ND 4.0
Creative Commons CC BY-NC-ND 4.0
Developed & Hosted by: MySecure.Space | Powered by: WoltLab Suite™