It is 4:40 p.m. on a Friday. The dashboard that worked fine for months now takes eleven seconds to load, and someone in the team chat has typed the four most dreaded words in software: "Is the database down?" It is not down. It is just doing far more work than anyone realized, and it has been politely doing that work for a long time.

The good news is that MySQL will tell you exactly what it is doing, if you ask in the right way. That question is a single keyword placed in front of your query: EXPLAIN. This guide walks through how to read its output, how to fix the most common problems with indexes, and how to prove the fix worked, all on MySQL 8.x.

A slow query is rarely a mystery. It is usually a query nobody has looked at yet.

Start with a realistic table

Let's use a simple orders table. The values below are sample data, but the shape is one you have almost certainly met in real life.

CREATE TABLE orders (
  id          BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  customer_id INT UNSIGNED NOT NULL,
  status      VARCHAR(20)  NOT NULL,
  total       DECIMAL(10,2) NOT NULL,
  created_at  DATETIME     NOT NULL
) ENGINE=InnoDB;

Imagine two million rows in it, and a query the dashboard runs on every page load:

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

With only the primary key in place, MySQL has no shortcut. It must look at rows one by one until it has found the matches, and then sort them. That is fine at ten thousand rows and painful at two million.

Reading EXPLAIN without panic

Put the keyword in front and run it:

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

You will get a small table with many columns. You can ignore most of them at first. Four columns tell most of the story:

ColumnWhat it tells youWhat you want to see
typeHow MySQL finds rowsref, range, or const, not ALL
keyWhich index was actually usedAn index name, not NULL
rowsEstimated rows MySQL expects to examineAs small as possible
ExtraHidden extra workNo Using filesort or Using temporary on hot paths

For our un-indexed query, you would see type: ALL, key: NULL, and rows close to two million. That ALL means a full table scan: MySQL is reading the entire table to answer a question about one customer. The Extra column will likely add Using where; Using filesort, which means a second round of sorting work on top.

Takeaway: read type and key first. If you see ALL and NULL on a big table, you have found your problem in about five seconds.

Build a composite index in the right order

The instinct is to add an index on every column in the WHERE clause. A better move is a single composite index designed around how the query actually behaves.

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

Why this order? MySQL can use a composite index from left to right, a rule often called the leftmost prefix. Columns compared with equality (customer_id, status) go first. The column used for sorting or range filtering (created_at) goes last. With that layout, MySQL jumps straight to one customer's paid orders, and they are already stored in created_at order, so no separate sort is needed.

Run EXPLAIN again. You should now see type: ref, key: idx_customer_status_created, a rows estimate in the dozens instead of millions, and the filesort gone from Extra. Notice that MySQL 8.0 can even read this index backwards to satisfy ORDER BY ... DESC, so you do not need anything special for that case.

A quick example of the rule in action:

  • WHERE customer_id = 1 can use the index.
  • WHERE customer_id = 1 AND status = 'paid' can use the index more deeply.
  • WHERE status = 'paid' alone cannot use it efficiently, because the first column is skipped. (MySQL 8.0 has a limited "skip scan" optimization for some cases, but do not design around it.)

Prove it with EXPLAIN ANALYZE

EXPLAIN shows a plan and estimates. Estimates can be wrong. Since MySQL 8.0.18 you can run the query for real and compare the estimate with reality:

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

The output is a tree of steps, each with an estimated cost and the actual time and row count. Look for a step where the actual rows are wildly different from the estimate. That mismatch often points to stale statistics, which ANALYZE TABLE orders; can refresh.

Estimates are a forecast. EXPLAIN ANALYZE is the weather outside your window.

One caution: EXPLAIN ANALYZE really executes the statement. That is perfectly safe for a SELECT, but be thoughtful about running it against anything heavy on a busy production server.

Three mistakes that quietly disable your index

Even with a perfect index, the way a query is written can stop MySQL from using it.

Wrapping the column in a function. WHERE DATE(created_at) = '2026-11-01' forces MySQL to compute the function for every row. Rewrite it as a range:

WHERE created_at >= '2026-11-01'
  AND created_at <  '2026-11-02'

Leading wildcards. WHERE email LIKE '%@example.com' cannot use a normal B-tree index, because the start of the value is unknown. LIKE 'anna%' can.

Mismatched types. Comparing a string column to a number, such as WHERE phone = 5551234, makes MySQL convert every stored value. Keep the literal's type the same as the column's.

Each of these looks harmless in code review. Each shows up in EXPLAIN as key: NULL or a surprisingly large rows figure.

Add indexes carefully, and remove them safely

Indexes are not free. Every one of them takes disk space and makes INSERT, UPDATE and DELETE a little slower, because the index must be maintained too. A table with fifteen indexes is usually a sign that nobody was reading EXPLAIN.

MySQL 8.0 offers a gentle way to test whether an index is really needed: make it invisible. The optimizer ignores it, but it is still maintained, so you can bring it back instantly.

ALTER TABLE orders ALTER INDEX idx_old_unused INVISIBLE;
-- watch your slow query log and dashboards for a few days
-- if nothing regresses:
ALTER TABLE orders DROP INDEX idx_old_unused;
-- if something does regress:
ALTER TABLE orders ALTER INDEX idx_old_unused VISIBLE;

This turns a risky deletion into a reversible experiment, which is exactly how production changes should feel.

A simple routine you can reuse

When a query is slow, work through the same short loop every time:

  1. Capture the exact slow query (the slow query log is ideal).
  2. Run EXPLAIN and read type, key, rows and Extra.
  3. Fix the query shape or add one well-ordered composite index.
  4. Confirm with EXPLAIN ANALYZE and real timings.
  5. Re-check a week later, because data grows and plans change.

Summary

EXPLAIN turns "the database is slow" into a specific, fixable statement such as "this query scans two million rows because the index is missing its sort column." Learn to read four columns, order composite indexes with equality first and ordering last, avoid functions around indexed columns, and use EXPLAIN ANALYZE plus invisible indexes to make changes with confidence. Next Friday at 4:40 p.m., you may be the person who answers the chat message calmly, and then goes home on time.