GreatSQL statistical information related knowledge points

Related knowledge points:

INNODB_STATS_PERSIST=ONOr STATS_PERSIST=1when defining a single table, optimizer statistics are persisted to disk. By default, innodb_stats_persistentit is enabled.

Persistent statistics are stored in mysql.innodb_table_statsand mysql.innodb_index_statstables.

The variable enabled by default innodb_stats_auto_recalccontrols whether statistics are automatically calculated when more than 10% of the rows in the table change. STATS_AUTO_RECALCAutomatic 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=OFFusing . STATS_PERSIST=0Instead, 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_recalcindex statistics are calculated and added to innodb_index_statsthe 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, but innodb_stats_persistent_sample_pagessetting 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 table innodb_stats_persistent, if enabled INNODB_STATS_PERSISTit is applied INNODB_STATS_PERSIST_SAMPLE_PAGESinsteadinnodb_stats_sample_pages

Summarize:

1. Non-persistent statistical information will be automatically updated in the following situations:

  1. Execute ANALYZE TABLE
  2. innodb_stats_on_metadata=ONIn this case, execute SHOW TABLE STATUS, SHOW INDEX, and query the TABLES and STATISTICS under INFORMATION_SCHEMA.
  3. With the --auto-rehash function enabled, use mysql client to log in
  4. The table is opened for the first time
  5. 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:

  1. INNODB_STATS_AUTO_RECALC=ONIn this case, 10% of the data in the table is modified

  2. 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:

  1. Manually update statistics, please note that read locks will be added during execution:

ANALYZETABLE TABLE_NAME;

  1. 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_PAGESdefault 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_PAGESthe maximum value here is 65535. If it exceeds, an error will be reported.

​ c. Manually update innodb_table_statsand innodb_index_statstable statistics (modifying these two tables will not generate binlog), and then use FLUSH TABLE tbl_namestatements 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:

image

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.

Linus took matters into his own hands to prevent kernel developers from replacing tabs with spaces. His father is one of the few leaders who can write code, his second son is the director of the open source technology department, and his youngest son is a core contributor to open source. Huawei: It took 1 year to convert 5,000 commonly used mobile applications Comprehensive migration to Hongmeng Java is the language most prone to third-party vulnerabilities. Wang Chenglu, the father of Hongmeng: open source Hongmeng is the only architectural innovation in the field of basic software in China. Ma Huateng and Zhou Hongyi shake hands to "remove grudges." Former Microsoft developer: Windows 11 performance is "ridiculously bad " " Although what Laoxiangji is open source is not the code, the reasons behind it are very heartwarming. Meta Llama 3 is officially released. Google announces a large-scale restructuring
{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/GreatSQL/blog/11054458