Your app was fast last month. Now one page takes four seconds to load. The query looks innocent โ a simple SELECT with a WHERE and an ORDER BY โ but MySQL is doing far more work than the result deserves.
Slow queries are rarely a mystery. In most apps, a handful of queries cause almost all of the database load, and each one is slow for one of a few repeatable reasons: a missing index, a query written in a way the optimizer cannot use, or a table that has grown until its old indexes stopped being selective. The fix starts with seeing what MySQL actually does โ and that is exactly what EXPLAIN is for.
Step 1: Find the Slow Queries First
Never guess which query is slow โ turn on the slow query log and let MySQL tell you. The default threshold is 10 seconds, too high to catch much: set it to 1 or 2 seconds in production, and to 0 in development to catch everything:
-- Check the current settings
SHOW VARIABLES LIKE 'slow_query%';
SHOW VARIABLES LIKE 'long_query_time';
-- Log every query slower than 1 second
SET GLOBAL slow_query_log = 'ON';
SET GLOBAL long_query_time = 1;
SET GLOBAL log_queries_not_using_indexes = 'ON';
log_queries_not_using_indexes also catches queries that skip an index while the table is still small โ those are tomorrow's slow queries.
Find the log file with SHOW VARIABLES LIKE 'slow_query_log_file';, then group it by total time. MySQL ships a summarizer, and Percona's pt-query-digest groups similar queries together:
# Built-in summarizer: top 10 by total time
mysqldumpslow -s t -t 10 /var/log/mysql/slow.log
# Grouped report with averages and rows-examined ratios
pt-query-digest /var/log/mysql/slow.log
Work on the worst offenders first โ tuning at random wastes time on queries that were never the problem.
Step 2: Run EXPLAIN on the Worst Query
Put EXPLAIN in front of the query and MySQL shows you the execution plan it picked โ which indexes it uses, the order it reads tables in, and roughly how many rows it expects to examine:
EXPLAIN
SELECT * FROM orders
WHERE user_id = 1234
ORDER BY created_at DESC
LIMIT 20;
Six columns do most of the work:
| Column | What it tells you | What to watch for |
|---|---|---|
type |
How MySQL reads the table | ALL means a full table scan โ avoid it on anything that grows |
key |
The index MySQL actually chose | NULL means no index was used at all |
possible_keys |
Indexes MySQL considered | If this is NULL, no existing index applies โ add one |
key_len |
How many bytes of the index are used | Shows how much of a composite index is used |
rows |
Estimated rows to examine | A high number against a tiny result set means wasted work |
Extra |
Flags like Using filesort |
Sorting and temp tables are expensive |
Aim for ref, eq_ref, or const in the type column.
On MySQL 8.0.18 and later, EXPLAIN ANALYZE actually runs the query and reports real times and row counts instead of estimates. Use it when you doubt the estimates โ but it really executes the statement, so never run it on a DELETE or UPDATE you are not ready to execute.
Step 3: Fix 1 โ Add the Missing Index
The most common slow query is the simplest one: a WHERE on a column with no index. The latest 20 orders for one user, on a table with 500,000 rows and only a primary key:
EXPLAIN SELECT * FROM orders WHERE user_id = 1234 ORDER BY created_at DESC LIMIT 20;
-- type=ALL key=NULL rows=498489 Extra=Using where; Using filesort
MySQL scans half a million rows, then sorts them. One index changes everything:
ALTER TABLE orders ADD INDEX idx_user (user_id);
-- type=ref key=idx_user rows=25 Extra=Using filesort
Now only about 25 rows are touched โ but they still have to be sorted. The sort disappears when the index matches both the filter and the sort order:
ALTER TABLE orders ADD INDEX idx_user_created (user_id, created_at);
-- type=ref key=idx_user_created rows=25 Extra=Backward index scan
The index is already ordered by created_at within each user_id, so MySQL reads it backwards and stops after 20 rows. No scan, no sort. This is the rule behind most index design: put the equality columns first, then the columns you sort or range-filter on, in the same order the query uses them.
Step 4: Fix 2 โ Stop Hiding the Index from MySQL
Sometimes the index exists and MySQL still does not use it, because the query hides the column from the optimizer. The classic case is wrapping an indexed column in a function:
-- Slow: the function defeats the index on created_at
SELECT * FROM orders WHERE DATE(created_at) = '2026-11-20';
-- Fast: a range the index can serve
SELECT * FROM orders
WHERE created_at >= '2026-11-20 00:00:00'
AND created_at < '2026-11-21 00:00:00';
The same trap appears with LIKE '%text' (a leading wildcard cannot use a B-tree index), with comparing a column to a value of the wrong type, and with OR across two different columns where a UNION of two indexed lookups would be faster. Whenever key is NULL but possible_keys lists something, read the WHERE clause again โ the query is usually hiding the column from its own index.
Step 5: Fix 3 โ Fix the Pagination That Gets Slower on Every Page
Deep pagination is the slow query people never suspect, because page 1 is fast:
-- Page 1,000: MySQL reads and discards 20,000 rows to show you 20
SELECT * FROM orders ORDER BY id LIMIT 20 OFFSET 20000;
OFFSET does not skip work โ MySQL still reads and throws away every row before the offset. The fix is keyset (cursor) pagination: remember the last id you showed and ask for the next rows after it:
-- Same page, no wasted rows โ the index on id does the work
SELECT * FROM orders WHERE id > 20000 ORDER BY id LIMIT 20;
If your UI genuinely needs numbered pages, keep the offsets shallow. For infinite-scroll lists and APIs, keyset pagination is almost always the right answer.
Step 6: Verify the Fix โ and Watch for Regression
Never trust a fix you have not measured. Re-run EXPLAIN after every change: type should move away from ALL, key should name the new index, and rows should drop. Then re-run the real query and time it.
Two warnings. First, an index is not free: every extra index slows down INSERT, UPDATE, and DELETE on that table and costs disk space, so add the index the query needs โ not one for every column. Second, check the plan again after any major schema change or bulk import. A table that doubled in size can quietly invalidate an index that used to work, and the slow query log from Step 1 will catch the regression before a customer does.
Frequently Asked Questions
Why is my MySQL query suddenly slow when nothing changed? Usually something did change: the table grew past the point where a full scan was cheap, or a bulk import changed the data distribution. Check the slow query log, then EXPLAIN the worst query.
What does "Using filesort" mean in EXPLAIN? MySQL had to sort the result itself, in memory or on disk, instead of reading rows already in order from an index. On large result sets it is usually fixed by a composite index that covers the filter and the sort order, as in Step 3. "Using temporary" is the same idea for grouping: MySQL built an internal temporary table, common with GROUP BY or DISTINCT on non-indexed columns, and an index on the grouped columns usually removes it.
How do I know which index MySQL will actually use? Run EXPLAIN and read the key column โ that is the index the optimizer chose. If possible_keys lists an index but key is NULL, the query is probably written in a way that hides the column from the index; see Step 4.
Is EXPLAIN ANALYZE safe to run in production? It executes the query, so it is safe for SELECT but not for UPDATE or DELETE unless you are ready for them to run. On a busy primary, prefer plain EXPLAIN first and save EXPLAIN ANALYZE for a replica or a quiet moment.
Conclusion
Fixing a slow MySQL query comes down to one habit: look at the plan before you change anything. Turn on the slow query log, pick the worst offender, and run EXPLAIN. If type is ALL and key is NULL, you are missing an index โ add one that matches the filter and the sort. If the index exists but is not used, the query is hiding the column behind a function or a bad comparison โ rewrite it so the index can serve. If pagination slows down page by page, switch to keyset pagination. Verify every fix with EXPLAIN again.