Skip to content

MySQL and InnoDB in depth · Part 9 of 10

Why MySQL is not using your index

You added an index and the query is still slow. The usual causes are functions on the column, type mismatches, leading wildcards, mixed collations and an optimizer that judged a scan cheaper.

· 4 min read

mysqlindexingperformance

An index is only useful if the optimizer can use it for the query you wrote. Most "my index is ignored" cases come from a short list of causes. Check them in order, and confirm each with EXPLAIN before and after the change.

Functions wrapped around the column

An index is sorted by the stored value. If you apply a function to the column, MySQL would have to compute the function for every row to compare, so it cannot seek.

SQL
-- cannot seek on an index over created_at
SELECT * FROM payments WHERE DATE(created_at) = '2026-01-15';

-- same result, index friendly: compare the bare column with a range
SELECT * FROM payments
WHERE created_at >= '2026-01-15' AND created_at < '2026-01-16';

If you must filter on an expression, MySQL 8.0.13 and later can index it directly with a functional index, for example CREATE INDEX idx_email_lower ON users ((LOWER(email)));, and queries that use the same expression can seek on it. A generated column with an ordinary index works on older versions too.

Type mismatches and implicit conversion

When the two sides of a comparison have different types, MySQL converts one side. If it has to convert the column, the index cannot be used. Comparing a varchar column with a number is the classic case: WHERE phone = 84901234567 converts every stored string to a number, while WHERE phone = '84901234567' seeks normally. The reverse is harmless, because comparing an integer column with a string literal converts the literal once. Keep parameter types equal to column types.

Leading wildcards and other pattern traps

  • LIKE 'abc%' can use an index because the prefix is known. LIKE '%abc' and LIKE '%abc%' cannot, since the sorted order says nothing about the end of a string. For real text search use a full-text index or a search engine.
  • OR across different columns often forces a scan, although MySQL can sometimes merge two indexes. Rewriting it as a UNION ALL of two indexed queries is often faster.
  • !=, NOT IN and NOT LIKE match most of the table, so the optimizer rarely finds an index worthwhile.

Collation and character set mismatches in joins

If two tables are joined on columns with different character sets or collations, for example utf8mb3 on one side and utf8mb4 on the other, MySQL must convert one column and cannot use its index for the join. Align the definitions of columns that are compared or joined. This is a common leftover from old schemas.

When the optimizer chooses a scan on purpose

Sometimes the index is usable and MySQL still ignores it, because it estimates that a scan is cheaper.

  • The condition matches a large share of the table. Reading many rows through a secondary index, each with a lookup, costs more than reading the table in order.
  • The table is tiny, so one page read beats any index.
  • The statistics are stale. After large data changes, ANALYZE TABLE refreshes them.
  • The query selects columns that are not in the index, forcing a lookup for each row.

You can compare the plans with an index hint such as FORCE INDEX, but use it as an experiment. If forcing the index is really faster, find out why the optimizer's estimate was wrong, and fix that.

A checklist. Work through these in order.

  1. Run EXPLAIN and look at key and rows.
  2. Is the column wrapped in a function or arithmetic?
  3. Do the types of both sides of each comparison match?
  4. Is there a leading wildcard, an OR or a negation?
  5. Do joined columns share character set and collation?
  6. Is the index order right for the query, as covered in the previous article?
  7. Are the statistics fresh, and is the condition selective enough to justify an index?

Takeaway

Write predicates so the bare column is compared with a value of the same type, keep joined columns consistent, and treat an ignored index as a clue rather than a mystery. EXPLAIN tells you which of these causes is in play.

A knowledge article, drafted with AI assistance and checked with automated tests. It is general background, not a description of my own experience or of any client's work.

Comments

Leave a comment

No account or email needed. Comments appear after review. A one-way hash of your IP address is kept for 30 days to fight spam, then erased.