How to restore system statistics in InnoDB?

InnoDB System Statistics: How to Restore and Maintain Them

In InnoDB, system statistics play a crucial role in query optimization and performance. These statistics help the MySQL query optimizer make informed decisions about query execution plans.

If the system statistics in InnoDB get lost or become outdated, it’s important to restore them to ensure optimal query performance. Here’s how you can restore system statistics in InnoDB:

  1. Enable InnoDB Persistent Statistics:InnoDB introduced persistent statistics in MySQL 8.0, which allows the storage and restoration of system statistics across server restarts. To enable persistent statistics, you need to set the innodb_stats_persistent parameter to ON in your MySQL configuration file (my.cnf) or dynamically using the SET GLOBAL command.
  2. Collect New Statistics:After enabling persistent statistics, InnoDB will automatically collect and persist new statistics as data changes. InnoDB monitors data modifications, and when a threshold is reached, it triggers the collection of statistics for the corresponding tables.To trigger an immediate collection of statistics for a specific table, you can use the ANALYZE TABLE statement:Running ANALYZE TABLE updates the statistics for the specified table and persists them in the system.
  3. Restore Statistics from Backup:If you have a backup of the system statistics, you can restore them by following these steps:Stop the MySQL server to ensure a consistent state.Restore the backup of the InnoDB system tablespace (ibdata1 file) and the associated log files (ib_logfile0, ib_logfile1).Start the MySQL server.After restoring the backup, the system statistics will be restored to the state they were in when the backup was taken.
  4. Generate Statistics from Existing Data:If you don’t have a backup of the system statistics, you can generate new statistics from the existing data using the ANALYZE TABLE statement. This process involves analyzing the tables and collecting fresh statistics based on the current data distribution.Running ANALYZE TABLE triggers the collection of statistics for the specified table and updates the system statistics accordingly.
  5. Monitor and Maintain Statistics:Once the system statistics are restored or generated, it’s crucial to regularly monitor and maintain them to ensure they stay up to date.InnoDB automatically updates the statistics as data changes, but you can also schedule periodic analysis using the ANALYZE TABLE statement to refresh the statistics for specific tables.Additionally, keep an eye on the system’s data distribution and workload patterns. If you notice significant changes in data distribution or query performance, consider updating the statistics for the affected tables to reflect the current state accurately.By restoring and maintaining system statistics in InnoDB, you ensure that the MySQL query optimizer has up-to-date information for making optimal query execution plans. This can significantly improve query performance and overall database efficiency.

ANALYZE TABLE your_table_name;

ANALYZE TABLE your_table_name;

innodb_stats_persistent = ON

About Shiv Iyer 330 Articles
Open Source Database Systems Engineer with a deep understanding of Optimizer Internals, Performance Engineering, Scalability and Data SRE. Shiv currently is the Founder, Investor, Board Member and CEO of multiple Database Systems Infrastructure Operations companies in the Transaction Processing Computing and ColumnStores ecosystem. He is also a frequent speaker in open source software conferences globally.