The innodb_stats_auto_recalc
variable, which is enabled by default, controls whether statistics are calculated automatically when a table undergoes changes to more than 10% of its rows. You can also configure automatic statistics recalculation for individual tables by specifying the STATS_AUTO_RECALC
clause when creating or altering a table.
Because of the asynchronous nature of automatic statistics recalculation, which occurs in the background, statistics may not be recalculated instantly after running a DML operation that affects more than 10% of a table, even when innodb_stats_auto_recalc
is enabled. Statistics recalculation can be delayed by few seconds in some cases. If up-to-date statistics are required immediately, run ANALYZE TABLE
to initiate a synchronous (foreground) recalculation of statistics.
If innodb_stats_auto_recalc
is disabled, you can ensure the accuracy of optimizer statistics by executing the ANALYZE TABLE
statement after making substantial changes to indexed columns. You might also consider adding ANALYZE TABLE
to setup scripts that you run after loading data, and running ANALYZE TABLE
on a schedule at times of low activity.
When an index is added to an existing table, or when a column is added or dropped, index statistics are calculated and added to the innodb_index_stats
table regardless of the value of innodb_stats_auto_recalc
.