跪拜 Guibai
← All articles
Backend · Database · MySQL

MySQL Tuning Past the Index Fix: Explain Estimates, Change Buffer, and Flush Stalls

By 挖掘狂人 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

A production MySQL shop eventually hits every one of these failure modes, and each produces a misleading symptom: fluctuating latency, a plan that looks wrong, or a query that stalls mid-day. Knowing which knob to turn and which index pattern actually helps beats guessing from explain output alone.

Summary

Stacking indexes does not fix a slow database. The execution layer decides most of the pain: explain's scan counts are sampling-based estimates that drift from reality, and the optimizer can prefer an index that scans more rows when it avoids a table lookup. When the estimate diverges, the working moves are analyze table, force index, rewriting the SQL, or rebuilding a misleading index.

Index design carries most of the remaining gain. Covering and composite indexes cut table lookups, index pushdown (5.6+) filters inside index traversal, and the change buffer only serves non-unique indexes, so it suits bill and log workloads but hurts read-after-write ones. Prefix indexes save space on long fields, and any function, implicit type conversion, or character set cast on an indexed column silently disables the index.

Flush behavior is the other stall source. When the redo log fills, updates pause while the checkpoint advances, which is a common cause of intermittent production stalls. Setting innodb_io_capacity from a fio IOPS test gives InnoDB an honest disk budget, and MySQL 8.0 already defaults innodb_flush_neighbors to 0 because neighbor flushes only pay off on mechanical disks.

Takeaways
— explain's scanned row counts are estimates derived from sampled index cardinality, not exact values.
— The optimizer chooses an index by total cost, which includes the price of table lookups; a plan scanning more rows can win when it avoids returning to the clustered index.
— On pre-8.0 MySQL, the query cache is invalidated entirely whenever any row in a table updates; benchmark with SQL_NO_CACHE to see real latency.
— When explain's estimate diverges badly, run analyze table, force index, rewrite the SQL, or drop and rebuild the offending index.
— Index pushdown, added in MySQL 5.6, filters on index fields during index traversal, reducing table lookups for combined conditions.
— The change buffer records updates for non-unique indexes so the data page is not read from disk immediately; unique indexes must check duplicates at write time and never use it.
— Change buffer helps write-heavy, read-light workloads and hurts workloads that read right after writing, since the merge triggers immediately.
— Function operations, implicit type conversion, and character set conversion on an indexed column all disable the index.
— Dirty pages are flushed when redo log fills, memory runs out, the server is idle, or at shutdown; a full redo log pauses all updates, a common cause of online stalls.
— Set innodb_io_capacity using a fio IOPS measurement, and MySQL 8.0 defaults innodb_flush_neighbors to 0 since neighbor flushes only help mechanical disks.
Conclusions

The optimizer's cost model, not raw row counts, decides index selection, which is why explain output alone misleads developers into thinking the chosen plan is wrong.

Every index-destroying trap in the article collapses to one rule: any function applied to an index column, including ones injected implicitly, kills the index.

The choice between normal and unique indexes is a workload decision, not a correctness one, since only the change buffer's deferral path changes.

Concepts & terms
EXPLAIN
A MySQL command that shows the execution plan for a query, including which index is used and an estimated number of scanned rows.
回表 (table lookup)
When a secondary index lacks the columns a query needs, MySQL returns to the clustered (primary key) index to fetch the rest of the row. Avoiding these lookups is why covering and composite indexes speed queries up.
Change buffer
An InnoDB area that records updates to non-unique indexes when the target data page is not in memory, deferring the disk read until the page is accessed again. Only non-unique indexes can use it because unique indexes must check duplicates immediately.
Index pushdown (索引下推)
A MySQL 5.6+ optimization that filters rows using columns already present in an index during index traversal, so conditions no longer force a table lookup for every matched row.
WAL (Write-Ahead Logging)
The strategy of writing a redo log before flushing changed data to disk. It makes InnoDB fast but leaves dirty pages out of sync; when the redo log fills, updates pause until the checkpoint advances.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗