Databases 4 min read

Why MySQL Optimizer Picks the Wrong Index and How to Fix It

The MySQL optimizer can select an inappropriate index due to inaccurate statistics or sampling errors, but you can correct this by refreshing statistics with ANALYZE TABLE, increasing sample pages, or explicitly forcing the right index using the FORCE INDEX hint.

Senior Tony
Senior Tony
Senior Tony
Why MySQL Optimizer Picks the Wrong Index and How to Fix It

When interviewers ask why the MySQL optimizer chooses the wrong index, the answer isn’t simply “index not used.” The optimizer decides based on a cost model and table statistics such as index cardinality, page count, data distribution, and sampled rows.

If the statistics are stale or the cost model doesn’t match the real workload, the optimizer may pick a sub‑optimal index.

Two common reasons cause inaccurate statistics:

Massive inserts or updates leave the statistics outdated. After a bulk update, a column’s selectivity can drop, yet the optimizer still prefers the old index.

MySQL samples only a limited number of data pages instead of scanning the whole table. This random sampling can miss skewed distributions, leading the optimizer to favor a slower index.

To address these issues you have two main strategies:

Run ANALYZE TABLE to recompute statistics and increase the number of sampled pages (e.g., from the default 20 to 50 or 100).

When statistics remain unreliable, use the FORCE INDEX hint to tell the optimizer to prioritize a specific index, overriding its automatic choice.

While FORCE INDEX can improve query performance when the optimizer’s choice is wrong, it doesn’t guarantee optimal execution if the underlying data distribution is still mismatched.

By understanding the optimizer’s decision process and applying these techniques, you can confidently explain and resolve index‑selection problems in interviews.

ANALYZE TABLE illustration
ANALYZE TABLE illustration
FORCE INDEX illustration
FORCE INDEX illustration
MySQLDatabase PerformanceoptimizerFORCE INDEXIndex SelectionANALYZE TABLE
Senior Tony
Written by

Senior Tony

Former senior tech manager at Meituan, ex‑tech director at New Oriental, with experience at JD.com and Qunar; specializes in Java interview coaching and regularly shares hardcore technical content. Runs a video channel of the same name.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.