Run ANALYZE TABLE to refresh table statistics after large data changes, so the MySQL query optimizer can choose the most efficient execution plan.
Overview
The MySQL query optimizer selects an execution plan by estimating the cost of different access paths, using table statistics — such as row count and index cardinality — as input. When statistics become stale after a large data change, the optimizer may choose a suboptimal plan and cause slow queries.
Run ANALYZE TABLE to manually refresh statistics immediately after significant data changes.
Background information
Alibaba Cloud reminds you that:
When you perform operations that have risks, such as modifying instance configurations or data, check the disaster recovery and fault tolerance capabilities of the instances to ensure data security.
Before you modify the configurations and data of instances including but not limited to ECS and RDS instances, we recommend that you create snapshots or enable RDS log backup.
If you have authorized or submitted security information such as the logon account and password in the Alibaba Cloud Management Console, we recommend that you modify such information in a timely manner.
How InnoDB manages statistics automatically
InnoDB updates table statistics automatically when innodb_stats_auto_recalc = ON (the default), triggered after approximately 10% of rows change. This recalculation runs asynchronously, so updated statistics may not be available immediately. For tables with rapid data changes or queries that require plan stability, run ANALYZE TABLE manually to force an immediate, synchronous update.
To check your current settings:
SHOW VARIABLES LIKE 'innodb_stats_auto_recalc';
SHOW VARIABLES LIKE 'innodb_stats_persistent';
SHOW VARIABLES LIKE 'innodb_stats_persistent_sample_pages';
When to run ANALYZE TABLE
The following table lists common situations that require manual statistics collection:
|
Situation |
Action |
|
Bulk INSERT that changes more than 10% of rows |
Run |
|
Bulk DELETE |
Run |
|
Large UPDATE on indexed columns |
Run |
|
|
Run |
|
After restoring from a backup |
Run |
|
Scheduled maintenance for tables with high row churn |
Run weekly or monthly, depending on change rate |
How to collect statistics
Connect to your ApsaraDB RDS for MySQL instance, then run the following SQL statement:
ANALYZE TABLE <table_name>;
To update statistics on a read replica without writing the operation to the binary log, use the LOCAL option:
ANALYZE LOCAL TABLE <table_name>;
Replace <table_name> with the name of the table you want to analyze.
ANALYZE TABLE acquires a read lock on the table for the duration of execution. Run the statement during off-peak hours to minimize impact on your workload.

Verify the updated statistics
After running ANALYZE TABLE, query information_schema.STATISTICS to confirm that cardinality values were updated:
SELECT
TABLE_NAME,
INDEX_NAME,
COLUMN_NAME,
CARDINALITY,
SEQ_IN_INDEX
FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = '<table_name>'
ORDER BY INDEX_NAME, SEQ_IN_INDEX;
The CARDINALITY column shows the estimated number of unique values per index column. The optimizer uses this value to decide whether to use an index. After a successful ANALYZE TABLE, cardinality values reflect the current data distribution.
Best practices
Run
ANALYZE TABLEafter any bulk operation that changes more than 10% of rows.Use
ANALYZE LOCAL TABLEon read replicas to avoid writing the operation to the binary log and triggering unnecessary replication.For very large tables where default sampling produces inconsistent cardinality estimates, increase
innodb_stats_persistent_sample_pages.After each major data load, run
EXPLAINon critical queries to verify the optimizer is using the expected index.Avoid running
ANALYZE TABLEon small tables — InnoDB's automatic recalculation handles these efficiently.-
Schedule regular analysis for tables with high row churn as part of routine maintenance.
Application scope
ApsaraDB RDS for MySQL