Databases 29 min read

Understanding MySQL EXPLAIN Output and Query Optimization

This article explains how MySQL generates execution plans using EXPLAIN, details the meaning of each column such as id, select_type, table, type, possible_keys, key, rows, and Extra, and demonstrates how to interpret and improve query performance through examples and code snippets.

Top Architect
Top Architect
Top Architect
Understanding MySQL EXPLAIN Output and Query Optimization

MySQL performs query optimization by generating an execution plan with EXPLAIN, which first creates a cost‑based plan and then executes it. The article introduces the purpose of EXPLAIN and shows how to view its output.

Key columns in the EXPLAIN result are explained:

id : identifier for each SELECT in the query.

select_type : describes the type of SELECT (e.g., SIMPLE, PRIMARY, UNION, SUBQUERY, DEPENDENT SUBQUERY, DERIVED, MATERIALIZED).

table : the table name accessed.

type : access method (system, const, eq_ref, ref, ref_or_null, index_merge, unique_subquery, index_subquery, range, index, ALL).

possible_keys and key : indexes that could be used and the actual index used.

key_len : length of the used index.

ref : column or constant used for index lookup.

rows : estimated number of rows examined.

filtered : percentage of rows filtered after applying conditions.

Extra : additional information such as "Using where", "Using index", "Using join buffer (Block Nested Loop)", "LooseScan", etc.

The article provides numerous EXPLAIN examples with SQL statements and their output tables, illustrating how different queries (simple SELECT, joins, subqueries, UNION, derived tables) affect the execution plan.

It also covers advanced concepts like subquery materialization, semi‑join strategies (Table pullout, DuplicateWeedout, LooseScan, FirstMatch), index merge types (Intersection, Union, Sort‑Union), and how MySQL uses temporary tables and filesort.

Throughout, code snippets are presented within ... tags to show exact MySQL commands and results, helping readers understand and apply optimization techniques to improve SQL performance.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

explainSubqueriesquery-optimization
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.