MySQL Indexing: Reading EXPLAIN Like an Engineer

How to read an execution plan, spot a full table scan and design composite indexes that actually get used.

A slow query is rarely a mystery. MySQL tells you exactly what it plans to do - you just have to read the plan. This guide walks through EXPLAIN output column by column and shows how to turn it into the right index.

Why EXPLAIN comes first

Adding indexes by guesswork feels productive, but every index slows down writes, takes memory in the buffer pool and makes the optimiser's job harder. EXPLAIN shows the plan before you change anything, so you fix the query that is actually slow - not the one you suspect.

Run EXPLAIN against a copy of production data. On a near-empty development database the optimiser often prefers a full scan because it genuinely is cheaper there, and the plan tells you nothing.

The columns that matter

Take a typical query behind an order history page:

SELECT id, total, created_at
FROM orders
WHERE customer_id = 4821
  AND status = 'paid'
ORDER BY created_at DESC
LIMIT 20;

Prefix it with EXPLAIN and, without a suitable index, you get this:

Column Value What it tells you
type ALL Full table scan - every row is read.
possible_keys NULL No index could even be considered.
key NULL No index was used.
rows 1 248 300 Estimated rows examined to return 20.
Extra Using where; Using filesort Filtering and sorting happen after reading.
EXPLAIN output for the order history query, before indexing.

type: how rows are found

The type column is the first thing to read. From best to worst:

  • const, eq_ref - a single row through the primary key or a unique index.
  • ref - all rows matching an equality on a non-unique index.
  • range - an index range, for BETWEEN, IN or comparisons.
  • index - the whole index is scanned; cheaper than the table, but still a scan.
  • ALL - the whole table is scanned. On a large table, this is the problem.

key and rows

possible_keys lists the indexes the optimiser considered, key the one it chose. If possible_keys has a value but key is empty, the optimiser decided the index was not worth it - usually because the filter matches too large a share of the table.

rows is an estimate, not a count. What matters is the ratio: if MySQL expects to examine a million rows to return twenty, the index is missing or in the wrong order.

Extra: the warning signs

  • Using filesort - the result is sorted after reading. Fine for a few hundred rows, expensive for thousands.
  • Using temporary - an internal temporary table is built, typical for GROUP BY on unindexed columns.
  • Using index - the good one: the query is answered from the index alone, without touching the table.

Designing composite indexes

A composite index is read from left to right, so column order decides whether it can be used at all. The rule we follow:

  1. Columns filtered with equality first (customer_id, status).
  2. Then the column used for range or sorting (created_at).
  3. Optionally the selected columns, to make the index covering.
ALTER TABLE orders
  ADD INDEX idx_orders_customer_status_created (customer_id, status, created_at);

An index is a promise about how you will query the data. Design it from the query, not from the table.

Diagram of a composite index: equality columns first, then the sort column
Column order in a composite index: equality filters first, then the sort column.

Checking the fix

Run EXPLAIN again. The plan should now show type: ref, the new index in key, a few dozen rows instead of a million, and no filesort - the index already returns rows in created_at order. On MySQL 8, EXPLAIN ANALYZE runs the query and adds real timings:

-> Limit: 20 row(s)  (cost=41.6 rows=20) (actual time=0.071..0.093 rows=20 loops=1)
    -> Index lookup on orders using idx_orders_customer_status_created
       (customer_id=4821, status='paid') (reverse)  (cost=41.6 rows=37)
       (actual time=0.069..0.089 rows=20 loops=1)

Measured on MySQL 8.4 with 1.2 million rows. Your numbers will differ, the shape of the plan should not.

In the application, keep the query a prepared statement so the plan stays the same for every customer:

$stmt = $pdo->prepare(
    'SELECT id, total, created_at
     FROM orders
     WHERE customer_id = :customer AND status = :status
     ORDER BY created_at DESC
     LIMIT 20'
);
$stmt->execute(['customer' => $customerId, 'status' => 'paid']);

Common questions

How many indexes is too many?

There is no fixed number. Every index has to earn its place with a query that uses it - check sys.schema_unused_indexes after a few weeks in production and drop what never shows up.

Does column order really matter?

Yes. An index on (status, customer_id) cannot serve a query that filters on customer_id alone, while (customer_id, status) can serve both.

Should I index every foreign key?

InnoDB already requires and creates one for each foreign key. The question is whether a composite index starting with that column would serve your real queries better - if so, it replaces the automatic one.


Stuck on a slow query? Send us the EXPLAIN output and an experienced engineer will tell you what to change.

Ask an engineer

Was this guide helpful?

[Name Surname]
Written by
[Role]

[1-2 sentences about the author: years in production, focus area, what they build at dotasolutions.]