Related knowledge points:
INNODB_STATS_PERSIST=ON
Or STATS_PERSIST=1
when defining a single table, optimizer statistics are persisted to disk. By default, innodb_stats_persistent
it is enabled.
Persistent statistics are stored in mysql.innodb_table_stats
and mysql.innodb_index_stats
tables.
The variable enabled by default innodb_stats_auto_recalc
controls whether statistics are automatically calculated when more than 10% of the rows in the table change. STATS_AUTO_RECALC
Automatic statistics recalculation can be configured for an individual table by specifying the clause when creating or altering the table .
Due to the asynchronous nature of automatic statistics recalculation that occurs in the background, even if enabled innodb_stats_auto_recalc
, statistics will not be recalculated immediately after running a DML operation that affects more than 10% of the table. In some cases, the recalculation of statistics may be delayed by a few seconds. If the latest statistics are needed immediately, run ANALYZE TABLE to initiate a synchronous (foreground) recalculation of statistics.
If disabled innodb_stats_auto_recalc
, you can ensure the accuracy of optimizer statistics by executing an ANALYZE TABLE statement after making a large number of changes to an index column.
Optimizer statistics are not persisted to disk when creating or altering a single table INNODB_STATS_PERSIST=OFF
using . STATS_PERSIST=0
Instead, statistics are stored in memory and are lost when the server is shut down. Statistics are also updated periodically through certain actions and under certain conditions.
When an index is added to an existing table, or when a column is added or removed, innodb_stats_auto_recalc
index statistics are calculated and added to innodb_index_stats
the table regardless of the value of .
Five parameters that affect statistics
-
innodb_stats_persistent
: Specifies whether InnoDB index statistics are persisted to disk. It is enabled by default. -
innodb_stats_persistent_sample_pages
: The number of index pages to sample when estimating cardinality and other statistics for index columns (such as those calculated by the analysis table). Increasing this value can improve the accuracy of index statistics, butinnodb_stats_persistent_sample_pages
setting a higher value may cause analysis tables to take longer to execute. -
innodb_stats_auto_recalc
: Causes InnoDB to automatically recalculate persistent statistics after significant changes to the data in the table. The threshold is 10% of the number of rows in the table and is turned on by default. -
innodb_stats_include_delete_marked
: Whether InnoDB includes records marked for deletion when calculating persistent optimizer statistics, turned off by default. -
innodb_stats_transient_sample_pages
: The number of index pages to sample when estimating cardinality and other statistics for index columns (such as those calculated by the analysis table). The default value is 8. Increasing this value can improve the accuracy of index statistics, thereby improving query execution plans, but at the cost of increased I/O when opening InnoDB tables or recalculating statistics. This parameter only applies if disabled for the tableinnodb_stats_persistent
, if enabledINNODB_STATS_PERSIST
it is appliedINNODB_STATS_PERSIST_SAMPLE_PAGES
insteadinnodb_stats_sample_pages
Summarize:
1. Non-persistent statistical information will be automatically updated in the following situations:
- Execute ANALYZE TABLE
innodb_stats_on_metadata=ON
In this case, execute SHOW TABLE STATUS, SHOW INDEX, and query the TABLES and STATISTICS under INFORMATION_SCHEMA.- With the --auto-rehash function enabled, use mysql client to log in
- The table is opened for the first time
- Since the last update of statistical information, the data in table 1/16 has been modified.
The disadvantages of non-persistent statistics are obvious. If a large number of tables start to update statistics after the database is restarted, it will have a great impact on the instance, so persistent statistics are currently used.
2. Persistence statistics will be automatically updated in the following situations:
-
INNODB_STATS_AUTO_RECALC=ON
In this case, 10% of the data in the table is modified -
Add new index
3. Handling of inaccurate statistical information
We checked the execution plan and found that the correct index was not used. If it is caused by a large difference in the statistical information in innodb_index_stats, it can be handled in the following ways:
- Manually update statistics, please note that read locks will be added during execution:
ANALYZETABLE TABLE_NAME;
- If the statistical information is still inaccurate after updating, you can consider adding data pages for table sampling. There are two ways to modify it:
a. The INNODB_STATS_PERSISTENT_SAMPLE_PAGES
default value of global variables is 20;
b. A single table can specify the sampling of the table:
ALTER TABLE TABLE_NAME STATS_SAMPLE_PAGES=40;
After testing, STATS_SAMPLE_PAGES
the maximum value here is 65535. If it exceeds, an error will be reported.
c. Manually update innodb_table_stats
and innodb_index_stats
table statistics (modifying these two tables will not generate binlog), and then use FLUSH TABLE tbl_name
statements to load the updated statistics.
Enjoy GreatSQL :)
About GreatSQL
GreatSQL is a domestic independent open source database suitable for financial-level applications. It has many core features such as high performance, high reliability, high ease of use, and high security. It can be used as an optional replacement for MySQL or Percona Server and is used in online production environments. , completely free and compatible with MySQL or Percona Server.
Related links: GreatSQL Community Gitee GitHub Bilibili
GreatSQL Community:
Community reward suggestions and feedback: https://greatsql.cn/thread-54-1-1.html
Community blog prize-winning submission details: https://greatsql.cn/thread-100-1-1.html
(If you have any questions about the article or have unique insights, you can go to the official community website to ask or share them~)
Technical exchange group:
WeChat & QQ group:
QQ group: 533341697
WeChat group: Add GreatSQL Community Assistant (WeChat ID: wanlidbc
) as a friend and wait for the community assistant to add you to the group.