NOTE
1.16 MySQL Statistics
MySQL uses statistics to estimate query costs. These notes cover how statistics are stored and used when choosing execution plans.
This is a historical learning note and may contain outdated or incomplete understanding.
1. What Are MySQL Statistics
MySQL query cost is calculated based on statistics.
2. What Statistics Are There
2.1. Persistent Disk-Based Statistics
- These statistics are stored on disk, meaning they remain after the server restarts.
2.1.1. Storage Location
- Stored in two tables
innodb_table_statsstores statistics about tables. Each row corresponds to the statistics of one table.innodb_index_statsstores statistics about indexes. Each row corresponds to one statistical item for one index.
2.1.2. How to Update Them
- Enable
innodb_stats_auto_recalc.- When the number of changed records exceeds 10% of the table size, the statistics are updated.
- Manually invoke the
ANALYZE TABLEstatement to update statistics.
2.2. Non-Persistent Memory-Based Statistics
- These statistics are stored in memory. When the server is shut down, these statistics are cleared. After the server restarts, they are collected again only in certain appropriate scenarios.
Drag to pan, scroll to zoom, and click a node to expand or collapse it. Large maps start with deeper levels collapsed; click nodes to expand them.
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub