← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

MySQL EXPLAIN

database Intermediate

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;

Added 31 Mar 2026
Views 19
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 1 ping T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Perplexity 6 Google 2 Unknown AI 2 Meta AI 1 Ahrefs 1
crawler 11 pre-tracking 1
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

✓ schema.org compliant