MySQL interview question · Most asked
Why might MySQL not use an index you created?
Summary
Common causes: a function or expression on the column (WHERE DATE(created_at) = ...), implicit type conversion (comparing a VARCHAR column to a number), a leading wildcard (LIKE '%abc'), not using the leftmost prefix of a composite index, OR across different columns, low selectivity, or stale statistics.
Longer Answer
Fixes:
- Rewrite to a range:
created_at >= '2026-10-01' AND created_at < '2026-10-02'instead ofDATE(created_at) = ...; or index the expression (functional indexes, 8.0.13+). - Compare with the right type:
WHERE phone = '0123', not= 123(string-to-number conversion disables the index and can match wrong rows). - Different collations or character sets between joined columns also prevent index use.
ORacross columns: useUNION ALLof two indexed queries, or rely on index merge.- If the optimiser estimates that a large share of the table matches, a full scan can genuinely be faster.
- Refresh statistics with
ANALYZE TABLE; as a last resort useFORCE INDEX(and document why).
Key Points
- Functions on columns, type conversion, leading wildcards.
- Leftmost prefix not used; OR across columns.
- Collation mismatches in joins.
- Low selectivity can make a scan cheaper.
More most-asked MySQL questions
- Why is OFFSET pagination slow on large tables, and what is keyset pagination?
- How do you read EXPLAIN output in MySQL?
- What is a clustered index in InnoDB, and why does primary key choice matter so much?
- What are the transaction isolation levels, and which does InnoDB use by default?
- How does a composite index work, and what is the leftmost-prefix rule?
- What causes deadlocks in MySQL, and how do you handle them?
- What are gap locks and next-key locks, and why do they surprise developers?
- How does MySQL replication work? Explain the binary log, asynchronous and semi-synchronous replication, and GTIDs.