Insights

Added an Index but the Query Is Still Slow? EXPLAIN Shows Why MySQL Skips It

November 6, 2026
0:00
Here's a common assumption: if a MySQL query is slow, add an index on the column in the WHERE clause and the problem goes away. When the page is just as slow afterward, people decide indexes don't help their workload, or that the database server is underpowered. Usually neither is true. The index is fine. The query is written in a way that stops MySQL or MariaDB from using it, and EXPLAIN will show you that in one line. This comes up often at the support desk. Someone adds an index through phpMyAdmin or a migration, sees no change, and asks for a bigger plan. Before anyone talks about resources, it's worth spending two minutes checking whether the index is being used at all.
A standard MySQL index is a B-tree. Think of it as a sorted copy of the column's values, each pointing back to its row. The database can jump to a value, or to the start of a range, without reading the whole table. That only works when the query asks for something the sorted structure can answer directly, like created_at >= X or email = 'Y'. If the query asks for something the index wasn't sorted by, such as the result of a function applied to the column or a match in the middle of a string, the sorted order is useless. The only safe option is to read every row and check each one. That's a full table scan, and on a big table it's exactly as slow as having no index. The optimizer (the part of the server that decides how to run a query) doesn't rewrite your query to make the index usable. It works with the query you give it.
Put EXPLAIN in front of the slow SELECT and run it in the MySQL client or the SQL tab of phpMyAdmin. It doesn't run the query. It shows the plan the optimizer picked. Say an orders table has an index on created_at and the slow query looks like this:
SQL
EXPLAIN SELECT id, total
FROM orders
WHERE DATE(created_at) = '2026-01-15';
Illustrative output format (trimmed to the useful columns):
TEXT
+----+-------------+--------+------+---------------+------+---------+------+--------+-------------+
| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows   | Extra       |
+----+-------------+--------+------+---------------+------+---------+------+--------+-------------+
|  1 | SIMPLE      | orders | ALL  | NULL          | NULL | NULL    | NULL | 500000 | Using where |
+----+-------------+--------+------+---------------+------+---------+------+--------+-------------+
Five columns do most of the work:
  • type is the access method. ALL means a full table scan. range, ref, eq_ref, and const mean an index is being used to narrow things down.
  • possible_keys lists indexes the optimizer considered usable. NULL here means it couldn't use any of them for this condition, which points to the query rather than the index.
  • key is the index it actually chose. NULL means none.
  • rows is an estimate of how many rows it expects to examine. It's an estimate, but a number close to the table size means a scan.
  • Extra adds detail. Using where with type: ALL means rows are filtered one by one after being read.
The pattern to watch for is an index you know exists, missing from possible_keys. That almost always means one of the issues below. If it shows up in possible_keys but key is still NULL, the optimizer could have used it and chose not to, which is a different problem covered further down. On newer MySQL versions, EXPLAIN ANALYZE actually runs the query and reports real row counts and timings. MariaDB has a similar ANALYZE SELECT statement. Both are useful when the estimates look off, but they do execute the query, so be careful with slow ones on a busy server.
The example above is the most common case. DATE(created_at) asks for a computed value, and the index stores raw created_at timestamps, not their dates. The same applies to YEAR(created_at), LOWER(email), SUBSTRING(sku, 1, 3), and arithmetic like price * 1.1 > 100. Once the column sits inside an expression, the index on that column usually can't be used for the lookup. The fix is to leave the column alone and move the work to the other side of the comparison:
SQL
-- Instead of DATE(created_at) = '2026-01-15', use a half-open range
SELECT id, total FROM orders
WHERE created_at >= '2026-01-15' AND created_at < '2026-01-16';

-- Instead of price * 1.1 > 100, move the math to the constant
SELECT id FROM products WHERE price > 100 / 1.1;

-- If phone is VARCHAR, compare it to a string, not a number
SELECT id FROM customers WHERE phone = '628123456789';
The half-open range (>= start, < next day) is safer than BETWEEN ... '23:59:59' because it doesn't miss fractional seconds. Run EXPLAIN again and type should change to range, with your index in key. LOWER(email) is often unnecessary anyway. With the case-insensitive collations most MySQL and MariaDB installs use by default, email = '[email protected]' already matches regardless of case. Check the column's collation with SHOW CREATE TABLE before assuming you need the function. If you can't change the query, for example because it lives inside a plugin, recent MySQL versions support functional indexes built on the expression itself. MariaDB and older MySQL can get a similar result with an indexed generated column. Syntax and support depend on the version, so check before relying on it.
LIKE 'invoice%' can use an index. The values are sorted, so the server jumps to where entries starting with invoice begin and reads until they stop. LIKE '%invoice%' or LIKE '%invoice' can't. Matches could be anywhere in the sorted list, so it has to check every row. This shows up a lot on WordPress sites. Search and filter plugins often run LIKE '%term%' against wp_postmeta.meta_value, which is a long text column with no regular index. Adding one won't help a leading-wildcard search even if the column type allowed it. The options depend on what the search really needs. If users type the start of a value, such as an order number prefix, drop the leading %. If they need word search inside text, a FULLTEXT index with MATCH ... AGAINST is designed for that. It behaves differently from LIKE, though: it matches whole words and has its own rules for minimum word length and stopwords, so test it before swapping it in. For heavy search on large datasets, a dedicated search engine is often the cleaner long-term answer.
This one is easy to miss because the query returns correct results. Suppose phone is a VARCHAR column with an index, and the application runs WHERE phone = 628123456789 with no quotes. MySQL compares a string with a number by converting the string to a number for every row. Many different strings convert to the same number ('0628123456789', ' 628123456789'), so the index on the string values can't be used to find matches. The result is a full scan. The reverse case is usually harmless. If user_id is an INT and the query says user_id = '42', the constant gets converted once and the index still works. The trouble is specifically a string column compared to a numeric literal. The fix is to quote the value, or bind it as a string in the application. Joins have a related trap. If orders.customer_code and customers.code use different character sets or collations, for example one utf8 and one utf8mb4 after a partial migration, the server may need to convert values to compare them, and the index on one side can become unusable. Whether that happens depends on the exact charsets, collations, and version, so check it with EXPLAIN rather than assuming. SHOW CREATE TABLE on both tables shows the definitions side by side.
Sometimes possible_keys lists your index and key is still NULL. Here the optimizer looked at the index and estimated that a scan would be cheaper. That's often reasonable. If a condition matches a large share of the table, like status = 'active' where nearly every row is active, jumping from index to row for each match can cost more than reading the table straight through. There's no fixed threshold. It depends on table statistics and server version. A few other things produce the same symptom:
  • Composite indexes are used from the left. An index on (customer_id, created_at) helps WHERE customer_id = 5, but not WHERE created_at > '2026-01-01' on its own.
  • OR across different columns can stop a single index from covering the condition. Splitting it into a UNION sometimes helps.
  • Stale statistics can push the optimizer toward a bad estimate. ANALYZE TABLE orders; refreshes them. It's usually quick on InnoDB, but check how your version handles locking before running it on a busy production table.
Forcing an index with FORCE INDEX is possible. Treat it as a last resort, because it locks in a decision that may be wrong once the data changes.
When a query is slow and an index didn't help, don't add another one yet. Run EXPLAIN, then look at possible_keys and key. If your index is missing from possible_keys, look at the WHERE clause for a function wrapped around the column, a LIKE that starts with %, or a value whose type doesn't match the column, and rewrite that part so the bare column sits on one side of the comparison. If the index is listed but not chosen, look at how selective the condition is and whether the statistics are current. Run EXPLAIN once more after each change. When type moves off ALL and rows drops, you'll know the index is finally doing its job.

Join My Newsletter & Get Updates!

Get notified whenever a new technical guide, server infrastructure breakdown, or personal reflection goes live, plus a curated weekly digest.