Most slow queries start life as fast ones. A table holds a few thousand rows during development, every query returns instantly, and nobody thinks much about indexes. A year later the same table holds a few million rows, and a page that used to load in 80 milliseconds takes four seconds.
The good news is that MySQL will tell you exactly why. EXPLAIN shows the plan the optimizer picked before it runs anything, and with a little practice you can read one in under a minute.
Find the queries worth fixing first
Before reading any plans, find out which queries actually hurt. Guessing usually leads to indexing the wrong table.
Turn on the slow query log, either in your config file or at runtime:
SET GLOBAL slow_query_log = 'ON';
SET GLOBAL long_query_time = 0.5;
Leave it running for a day of normal traffic, then summarise it with mysqldumpslow or pt-query-digest. Sort by total time, not by the slowest single run. A query that takes 200 ms but runs 50,000 times a day costs far more than a nightly report that takes 30 seconds.
On a managed database where you cannot read the log file, the same information is in performance_schema.events_statements_summary_by_digest.
Running EXPLAIN
Put EXPLAIN in front of the query:
EXPLAIN
SELECT id, total
FROM orders
WHERE customer_id = 42
AND status = 'paid'
ORDER BY created_at DESC
LIMIT 20;
On an orders table with nothing but a primary key, the important columns of the result look something like this:
| type | key | rows | Extra |
|---|---|---|---|
| ALL | NULL | 1,948,210 | Using where; Using filesort |
Three columns tell you most of the story.
type is the access method, and it is the first thing to look at. From worst to best, the values you will see most often are:
ALL: a full table scan. Every row is read.index: a full scan of an index. Better thanALLonly because the index is smaller than the table.range: a bounded slice of an index, used forBETWEEN,<,>andIN.ref: rows matched through a non-unique index with an equality condition.eq_ref: exactly one row per join, through a primary key or unique index.const: a single row, looked up once and treated as a constant.
rows is the optimizer's estimate of how many rows it will examine. Compare it with how many rows you actually return. Examining two million rows to return twenty is the whole problem in one number.
Extra holds the warnings. Using filesort means MySQL has to sort the result itself instead of reading it in index order. Using temporary means it builds an internal temporary table, which is common with GROUP BY and DISTINCT on unindexed columns. Using index is good news: the query was answered from the index alone, without touching the table rows.
Designing the right composite index
For the query above, the fix is a single composite index:
CREATE INDEX idx_customer_status_created
ON orders (customer_id, status, created_at);
Column order matters more than anything else here. MySQL uses a composite index from the left, so the rule of thumb is:
- Columns compared with equality (
=, orINwith a short list) go first. - The column you sort by, or the one with a range condition, goes next.
- Columns after a range condition cannot be used to narrow the search.
With this index, MySQL jumps straight to the entries for customer 42 with status paid, and they are already stored in created_at order. It reads twenty of them from the end and stops. Run EXPLAIN again and you should see type: ref, the new index in the key column, a rows estimate in the dozens or hundreds instead of millions, and no filesort.
Swap the order to (created_at, customer_id, status) and MySQL can no longer jump to customer 42. At best it walks the index in date order, checking every entry until it has found twenty matches.
Covering indexes
If a hot query only needs a few columns, you can add them to the index so the table itself is never read:
CREATE INDEX idx_customer_status_created_total
ON orders (customer_id, status, created_at, total);
InnoDB secondary indexes always contain the primary key, so id comes along for free. EXPLAIN now shows Using index in the Extra column. This is worth doing for queries that run constantly, but do not turn every index into a copy of the table. Each extra column makes the index bigger and every write slower.
Common reasons an index is ignored
You added the index, but EXPLAIN still shows ALL. These are the usual suspects.
A function wrapped around the column. MySQL cannot use an index on created_at for WHERE DATE(created_at) = '2026-09-01', because it would have to compute DATE() for every row first. Rewrite it as a range:
WHERE created_at >= '2026-09-01'
AND created_at < '2026-09-02'
MySQL 8.0.13 and later can index an expression directly, but the range rewrite works everywhere and is easier to reason about.
Implicit type conversion. If phone is a VARCHAR and you write WHERE phone = 5551234, MySQL converts every stored string to a number before comparing, and the index is skipped. Quote the value.
A leading wildcard. LIKE '%smith' cannot use a B-tree index, because the start of the string is unknown. LIKE 'smith%' can. For real text search, use a full-text index or a search engine.
Low selectivity. An index on a column with three possible values, such as a status flag, is often not worth reading on its own. The optimizer may decide a table scan is cheaper, and it is usually right. Columns like that earn their place as part of a composite index, next to more selective columns.
Stale statistics. The optimizer works from estimates. After a large import or delete, run ANALYZE TABLE orders; so it has a current picture of the data.
Check the real numbers with EXPLAIN ANALYZE
EXPLAIN shows estimates. From MySQL 8.0.18, EXPLAIN ANALYZE actually runs the query and reports the real time and row counts for each step of the plan:
EXPLAIN ANALYZE
SELECT id, total FROM orders
WHERE customer_id = 42 AND status = 'paid'
ORDER BY created_at DESC LIMIT 20;
When estimated and actual rows differ by orders of magnitude, the optimizer is working from bad information, and that is often the real cause of a poor plan. Because the statement really executes, be careful running it on very heavy queries against a busy production database.
Deep pagination is a separate problem
A good index will not save LIMIT 20 OFFSET 200000. MySQL still has to walk past 200,000 rows only to throw them away. If users or jobs page deep into a result set, switch to keyset pagination and remember where the previous page ended:
SELECT id, total, created_at
FROM orders
WHERE customer_id = 42
AND status = 'paid'
AND created_at < '2026-08-14 09:12:44'
ORDER BY created_at DESC
LIMIT 20;
The same composite index serves every page at the same speed, whether it is the first or the thousandth. If two rows can share a timestamp, add the id as a tiebreaker so no row is skipped between pages.
Indexes are not free
Every index speeds up some reads and slows down every write, because each insert and update has to maintain it. Indexes also take space in the buffer pool, which is the same memory your hot data needs.
Before adding one, check whether an existing index already starts with the same columns. An index on (customer_id) is redundant once (customer_id, status, created_at) exists. In MySQL 8, the sys.schema_redundant_indexes and sys.schema_unused_indexes views point out candidates for removal.
A quick checklist
- Find slow queries in the slow log, ranked by total time.
- Run
EXPLAINand readtype,rowsandExtrafirst. - Build composite indexes with equality columns first, then the sort or range column.
- Keep functions and type conversions away from indexed columns in
WHERE. - Confirm with
EXPLAIN ANALYZE, and measure again after deploying. - Drop indexes that nothing uses.
Most database performance work comes down to that loop: measure, read the plan, change one thing, measure again. It is rarely exciting, but it is usually the cheapest speedup available.
Where this matters in our own work
Our OCPP Management Platform runs on MySQL 8 across roughly 39 tables, and charger heartbeats and meter values are exactly the kind of fast-growing tables where a missing index turns a dashboard query into a full scan. If a slow query is already hurting your application, tracking it down is a typical first task in our support and optimization work.



