Skip to content

Lesson 3 of 9 · 28m

Indexing from EXPLAIN Plans, Not Hunches

Read MySQL execution plans, understand access types and key usage, and design composite indexes that match real predicates and ordering.

The fastest way to become dangerous with MySQL is to read EXPLAIN as a vibe meter. 'Using index' sounds good, 'Using filesort' sounds bad, and suddenly everyone is adding indexes without knowing what problem they solve. Real index design starts with the query's predicate shape, cardinality, ordering, and row lookup cost. The plan is evidence, not a scoreboard.

Start with the actual query and data shape

SELECT order_id, total_cents, created_at
FROM orders
WHERE customer_id = 9182
  AND status = 'open'
ORDER BY created_at DESC
LIMIT 20;

A natural index candidate is (customer_id, status, created_at). Equality columns generally come before range or ordering columns. customer_id and status filter equality, then created_at supports descending order in modern MySQL if the index direction or scan direction matches the plan.

ALTER TABLE orders
  ADD INDEX idx_customer_status_created (customer_id, status, created_at);

Do not guess cardinality from column names. Inspect value distribution with SELECT status, COUNT(*) FROM orders GROUP BY status before deciding that a column is selective.

Read EXPLAIN as a pipeline

Prefix the real query with EXPLAIN (or EXPLAIN FORMAT=JSON when you need machine-readable detail). Important fields include:

type           access method: const, eq_ref, ref, range, index, ALL
key            index actually selected
key_len        bytes of index prefix used
rows           optimizer estimate, not a promise
filtered       estimated percentage after conditions
Extra          using where, index condition, filesort, temporary, covering index

type=ALL is a full table scan. Sometimes that is correct for a query touching most rows. It is a problem when the intent was selective lookup. type=index is not automatically good; it can mean scanning an entire index rather than the entire table.

Covering indexes, filesorts, and write cost

A covering index contains all columns needed by the query, allowing InnoDB to avoid clustered row lookups. That can be a major win for hot reads, but it costs write time and storage. EXPLAIN ANALYZE in MySQL 8.0 shows estimated versus actual timing and rows, and it really executes the statement, so use it only on safe reads.

Filesort means MySQL could not fully use index order. Sorting twenty rows is irrelevant; sorting five million rows may be the incident. Likewise, every index is write amplification. Review low-value indexes with sys.schema_unused_indexes cautiously because it ignores queries not seen since restart and cannot know seasonal jobs.

Field notes

  • rows is an estimate; row skew can make a good-looking plan terrible for particular parameter values.
  • Functions around indexed columns often destroy index usefulness, as in DATE(created_at) = '2026-11-30'.
  • OR across unrelated columns often needs a different query shape or carefully planned indexes.
  • Prefix indexes on long strings trade lookup precision for size; collisions still require row checks.
  • Removing an index can change lock behavior because writes may scan more rows.

Was this lesson helpful?

Sign in to rate