本节描述了如何为 InnoDB 表配置持久化和非持久化优化器统计信息。This section describes how to configure persistent and
non-persistent optimizer statistics for InnoDB
tables.
持久化优化器统计信息在服务器重启后仍然保留,从而实现更高的 计划稳定性 和更一致的查询性能。持久化优化器统计信息还提供控制和灵活性,并具有以下额外好处:Persistent optimizer statistics are persisted across server restarts, allowing for greater plan stability and more consistent query performance. Persistent optimizer statistics also provide control and flexibility with these additional benefits:
您可以使用
innodb_stats_auto_recalc配置选项来控制在表发生重大更改后是否自动更新统计信息。You can use theinnodb_stats_auto_recalcconfiguration option to control whether statistics are updated automatically after substantial changes to a table.您可以在
CREATE TABLE和ALTER TABLE语句中使用STATS_PERSISTENT、STATS_AUTO_RECALC和STATS_SAMPLE_PAGES子句来为单个表配置优化器统计信息。You can use theSTATS_PERSISTENT,STATS_AUTO_RECALC, andSTATS_SAMPLE_PAGESclauses withCREATE TABLEandALTER TABLEstatements to configure optimizer statistics for individual tables.您可以查询
mysql.innodb_table_stats和mysql.innodb_index_stats表中的优化器统计数据。You can query optimizer statistics data in themysql.innodb_table_statsandmysql.innodb_index_statstables.您可以查看
mysql.innodb_table_stats和mysql.innodb_index_stats表的last_update列,以了解统计信息的最后更新时间。You can view thelast_updatecolumn of themysql.innodb_table_statsandmysql.innodb_index_statstables to see when statistics were last updated.您可以手动修改
mysql.innodb_table_stats和mysql.innodb_index_stats表,以强制使用特定的查询优化方案或在不修改数据库的情况下测试替代方案。You can manually modify themysql.innodb_table_statsandmysql.innodb_index_statstables to force a specific query optimization plan or to test alternative plans without modifying the database.
持久优化器统计信息功能默认启用 (innodb_stats_persistent=ON)。The persistent optimizer statistics feature is enabled by default
(innodb_stats_persistent=ON).
非持久化优化器统计信息在每次服务器重启以及某些其他操作后会被清除,并在下一次表访问时重新计算。因此,在重新计算统计信息时可能会产生不同的估计值,从而导致执行计划的选择不同,并引起查询性能的变化。Non-persistent optimizer statistics are cleared on each server restart and after some other operations, and recomputed on the next table access. As a result, different estimates could be produced when recomputing statistics, leading to different choices in execution plans and variations in query performance.
本节还提供了估算ANALYZE TABLE复杂性的信息,这在尝试在准确统计信息与ANALYZE TABLE执行时间之间取得平衡时可能有所帮助。This section also provides information about estimating
ANALYZE TABLE complexity, which may
be useful when attempting to achieve a balance between accurate
statistics and ANALYZE TABLE
execution time.