MySQL EXPLAIN
Also Known As
MySQL EXPLAIN query
query execution plan MySQL
EXPLAIN ANALYZE
TL;DR
A MySQL query analysis command showing the execution plan — which indexes are used, how many rows are scanned, and where bottlenecks are.
Explanation
EXPLAIN prefixed to a SELECT/UPDATE/DELETE shows the query execution plan. Key columns: 'type' (ALL = full scan, ref = index lookup, eq_ref = unique index — lower is better), 'key' (which index was chosen), 'rows' (estimated rows examined), 'Extra' (Using filesort, Using temporary — both indicate optimisation opportunities). EXPLAIN ANALYZE (MySQL 8+) executes the query and shows actual vs estimated row counts.
Common Misconception
✗ EXPLAIN shows the actual query execution. Standard EXPLAIN shows the estimated plan only — use EXPLAIN ANALYZE (MySQL 8+) or EXPLAIN FORMAT=JSON for actual execution data.
Why It Matters
EXPLAIN is the primary tool for diagnosing slow queries. A single EXPLAIN output immediately reveals whether a query is doing a full table scan, which index it uses, and whether a sort operation is hitting disk.
Common Mistakes
- Not running EXPLAIN before adding an index — the index may already exist or may not be used.
- Ignoring the 'Extra' column — 'Using filesort' and 'Using temporary' are expensive operations.
- Running EXPLAIN ANALYZE on a slow write query in production — it executes the query, causing the write.
Avoid When
- Do not run EXPLAIN ANALYZE on write queries (INSERT/UPDATE/DELETE) in production — it actually executes them.
When To Use
- Run EXPLAIN on any query that feels slow or affects many rows.
- Run EXPLAIN before and after adding an index to verify it is actually used.
Code Examples
✗ Vulnerable
-- Debugging slow query by guessing
-- Adding indexes blindly without checking EXPLAIN first
✓ Fixed
-- Check execution plan
EXPLAIN SELECT * FROM orders WHERE user_id = 42 AND status = 'pending';
-- Look for: type=ref or better, key=idx_user_status, rows=small number
-- MySQL 8+: actual execution stats
EXPLAIN ANALYZE SELECT * FROM orders WHERE user_id = 42;
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
31 Mar 2026
Views
19
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Perplexity 6
Google 2
Unknown AI 2
Meta AI 1
Ahrefs 1
Also referenced
How they use it
crawler 11
pre-tracking 1
Related categories
⚡
DEV INTEL
Tools & Severity
⚙ Fix effort: Low
⚡ Quick Fix
Run EXPLAIN SELECT ... on any slow query — if type=ALL and rows is large, add an index on the WHERE/JOIN column
📦 Applies To
web
cli
🔗 Prerequisites
🔍 Detection Hints
Slow query log entries or queries without index visible in EXPLAIN
Auto-detectable:
✗ No
⚠ Related Problems
🤖 AI Agent
Confidence: Low
False Positives: Low
✗ Manual fix
Fix: Medium
Context: File