Using the EXPLAIN statement in MySQL 8

Updated: January 27, 2024 By: Guest Contributor Post a comment

Understanding how your MySQL queries are executed is a fundamental part of optimizing performance and diagnosing issues within a database. One of the key tools available to developers and database administrators for this purpose is the EXPLAIN statement. This comprehensive guide will introduce you to the EXPLAIN statement in MySQL 8, providing you with a deeper understanding of how it works and how you can leverage it to optimize your queries.

Introduction to the EXPLAIN statement

The EXPLAIN statement is used to obtain a query execution plan, showing how MySQL intends to execute a SELECT statement. This information can help identify bottlenecks and optimize query performance. To use EXPLAIN, simply prefix your SELECT query with the word EXPLAIN like so:

EXPLAIN SELECT * FROM users;

The output of this command provides details about the query execution plan, including the tables involved, the join type, possible keys, and other important information.

Reading the EXPLAIN Output

When you run an EXPLAIN statement in MySQL 8, you’ll receive a table with several columns. Some key columns to note are:

  • id: A unique identifier for each part of the query.
  • select_type: The type of SELECT, such as SIMPLE (one table) or SUBQUERY.
  • table: The table name or alias, if applicable.
  • partitions: The matching partitions for the query.
  • type: The join type, such as ALL, index, range, and more.
  • possible_keys: Potential indexes the planner may choose.
  • key: The index actually chosen by the planner.
  • key_len: The length of the chosen key, in bytes.
  • ref: The columns or constants that are compared to the index.
  • rows: An estimate of the number of rows MySQL believes it must examine to execute the query.
  • filtered: An estimate of the percentage of rows that will be filtered by table condition.
  • Extra: Additional information regarding the query execution.

Basic Use of EXPLAIN

Let’s explore a simple query and its EXPLAIN output:

EXPLAIN SELECT name FROM users WHERE age > 18;

After running this command, MySQL will return a table describing how the query will be executed. From there, you could look at how indexes are being used or if a full table scan is performed.

Understanding Joins with EXPLAIN

Now consider a more complex query involving joins:

EXPLAIN SELECT users.name, orders.amount FROM users JOIN orders ON users.id = orders.user_id WHERE users.age > 18;

EXPLAIN helps you to see whether the join is efficient, what kind of join is performed (like an INNER JOIN or a LEFT JOIN), and how the order of tables can affect the query plan.

Advanced EXPLAIN Usage

EXPLAIN for Derived Tables

Derived tables or subqueries can also be analyzed using EXPLAIN:

EXPLAIN SELECT name FROM (SELECT * FROM users) as derived_users WHERE age > 18;

This output helps you understand how MySQL resolves subqueries and can guide you in restructuring your query for better performance.

EXPLAIN for UPDATE and DELETE Statements

In MySQL 8, EXPLAIN can be used not only for SELECT statements but also for UPDATE and DELETE statements to understand how those operations will be carried out:

EXPLAIN DELETE FROM users WHERE last_login < '2021-01-01';

This helps in optimizing your data manipulation queries just as you would with SELECT statements.

Using EXPLAIN with JSON Output

MySQL 8 also introduces JSON-formatted EXPLAIN output, which provides a more detailed and machine-readable format. Here’s how to use it:

EXPLAIN FORMAT=JSON SELECT * FROM users WHERE age > 18;

The JSON output provides intricacies like estimated costs and the potential for filtering rows that are not present in the tabular output.

Conclusion

The EXPLAIN statement is an invaluable tool for developers and database administrators looking to optimize MySQL queries. By understanding the execution plan that EXPLAIN provides, informed decisions can be made regarding indexes, query structures, and performance tuning. By consistently using the EXPLAIN statement, you can ensure that your MySQL 8 database is running as efficiently as possible.