14.6.12.1 Configuring Persistent Optimizer Statist

The persistent optimizer statistics feature improves plan stability by storing statistics to disk and making them persistent across server restarts so that the optimizer is more likely to make consistent choices each time for a given query.
持久化优化器统计功能通过将统计数据存储到磁盘并使其在服务器重新启动时保持不变,从而提高计划稳定性,从而优化器每次对给定查询更可能做出一致的选择。
Optimizer statistics are persisted to disk when innodb_stats_persistent=ON or when individual tables are created or altered with STATS_PERSISTENT=1. innodb_stats_persistent is enabled by default.
当innodb_stats_persistent = ON时或者在使用STATS_PERSISTENT = 1创建或更改单个表时,优化器统计信息会持久保存到磁盘。 innodb_stats_persistent默认启用。
Formerly, optimizer statistics were cleared on each server restart and after some other operations, and recomputed on the next table access. Consequently, different estimates could be produced when recalculating statistics, leading to different choices in query execution plans and thus variations in query performance.
以前,优化器统计信息在每次服务器重启时以及其他一些操作之后都被清除,并在下一次表访问时重新计算。 因此,重新计算统计信息时可能会产生不同的估计值,导致查询执行计划中的选择不同,从而导致查询性能发生变化。
Persistent statistics are stored in the mysql.innodb_table_stats and mysql.innodb_index_stats tables, as described in Section 14.6.12.1.5, “InnoDB Persistent Statistics Tables”.
持久统计信息存储在mysql.innodb_table_stats和mysql.innodb_index_stats表中
To revert to using non-persistent optimizer statistics, you can modify tables using an ALTER TABLE tbl_name STATS_PERSISTENT=0 statement. For related information, see Section 14.6.12.2, “Configuring Non-Persistent Optimizer Statistics Parameters”
要还原为使用非持久优化程序统计信息,可以使用ALTER TABLE tbl_name STATS_PERSISTENT=0语句修改表。具体信息请看Section 14.6.12.2, “Configuring Non-Persistent Optimizer Statistics Parameters”

14.6.12.1.1 Configuring Automatic Statistics Calculation for Persistent Optimizer Statistics
配置持久性优化器统计信息的自动统计计算

The innodb_stats_auto_recalc configuration option, which is enabled by default, determines whether statistics are calculated automatically whenever a table undergoes substantial changes (to more than 10% of the rows). You can also configure automatic statistics recalculation for individual tables using a STATS_AUTO_RECALC clause in a CREATE TABLE orALTER TABLE statement. innodb_stats_auto_recalc is enabled by default.
默认情况下启用的innodb_stats_auto_recalc配置选项决定了当表经历实质性更改(超过10%的行)时是否自动计算统计信息,您还可以在CREATE TABLE或ALTER TABLE语句中使用STATS_AUTO_RECALC子句为单个表配置自动统计信息重新计算。 innodb_stats_auto_recalc默认启用。

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 wheninnodb_stats_auto_recalc is enabled. In some cases, statistics recalculation may be delayed by a few seconds. If up-to-date statistics are required immediately after changing significant portions of a table, run ANALYZE TABLE to initiate a synchronous (foreground) recalculation of statistics.
由于自动统计重新计算(发生在后台)的异步性质,即使启用了innodb_stats_auto_recalc,运行影响超过10%表的DML操作后,统计信息也不会立即重新计算。在某些情况下,统计数据重新计算可能会延迟几秒钟。如果在更改表的重要部分后立即需要最新的统计信息,请运行ANALYZE TABLE以启动统计信息的同步(前台)重新计算。
If innodb_stats_auto_recalc is disabled, ensure the accuracy of optimizer statistics by issuing the ANALYZE TABLE statement for each applicable table after making substantial changes to indexed columns. You might run this statement in your setup scripts after representative data has been loaded into the table, and run it periodically after DML operations significantly change the contents of indexed columns, or on a schedule at times of low activity. When a new index is added to an existing table, index statistics are calculated and added to the innodb_index_stats table regardless of the value of innodb_stats_auto_recalc.
如果innodb_stats_auto_recalc被禁用,请在对索引列进行重大更改后,通过为每个适用的表发出ANALYZE TABLE语句来确保优化器统计信息的准确性。在将代表性数据加载到表中之后,您可以在安装脚本中运行这条语句,并在DML操作显著更改索引列的内容之后定期运行它,或者在活动较低的时候按计划运行它。将新索引添加到现有表时,无论innodb_stats_auto_recalc的值如何,都会计算索引统计信息并将其添加到innodb_index_stats表中。
Caution
To ensure statistics are gathered when a new index is created, either enable theinnodb_stats_auto_recalc option, or run ANALYZE TABLE after creating each new index when the persistent statistics mode is enabled.
要确保在创建新索引时收集统计信息,请启用innodb_stats_auto_recalc选项,或者在启用持久统计信息模式时创建每个新索引后运行ANALYZE TABLE。

14.6.12.1.2 Configuring Optimizer Statistics Parameters for Individual Tables
配置单个表的优化器统计参数

innodb_stats_persistent, innodb_stats_auto_recalc, and innodb_stats_persistent_sample_pages are global configuration options. To override these system-wide settings and configure optimizer statistics parameters for individual tables, you can define STATS_PERSISTENT, STATS_AUTO_RECALC, and STATS_SAMPLE_PAGES clauses in CREATE TABLE orALTER TABLE statements.
innodb_stats_persistent,innodb_stats_auto_recalc和innodb_stats_persistent_sample_pages是全局配置选项,要覆盖这些系统范围的设置并为每个表配置优化程序统计信息参数,可以在CREATE TABLE或ALTER TABLE语句中定义STATS_PERSISTENT,STATS_AUTO_RECALC和STATS_SAMPLE_PAGES子句。

  • STATS_PERSISTENT specifies whether to enable persistent statistics for an InnoDB table. The value DEFAULT causes the persistent statistics setting for the table to be determined by the innodb_stats_persistent configuration option. The value 1 enables persistent statistics for the table, while the value 0 turns off this feature. After enabling persistent statistics through a CREATE TABLE or ALTER TABLE statement, issue an ANALYZE TABLE statement to calculate the statistics, after loading representative data into the table.
  • STATS_PERSISTENT指定是否启用InnoDB表的持久统计信息。DEFAULT值导致表的持久统计信息设置由innodb_stats_persistent配置选项确定。值1启用该表的持续统计信息,而值0将关闭此功能。 通过CREATE TABLE或ALTER TABLE语句启用持久性统计信息后,在将代表性数据加载到表中后,发出ANALYZE TABLE语句来计算统计信息。
  • STATS_AUTO_RECALC specifies whether to automatically recalculate persistent statistics for an InnoDB table. The value DEFAULT causes the persistent statistics setting for the table to be determined by the innodb_stats_auto_recalc configuration option. The value 1 causes statistics to be recalculated when 10% of the data in the table has changed. The value 0 prevents automatic recalculation for this table; with this setting, issue an ANALYZE TABLE statement to recalculate the statistics after making substantial changes to the table.
  • STATS_AUTO_RECALC指定是否自动重新计算InnoDB表的持久统计信息。DEFAULT值导致表的持久统计信息设置由innodb_stats_auto_recalc 配置选项确定。当表中10%的数据发生更改时,值等于1将重新计算统计数据。值等于0防止自动重新计算该表。使用此设置,在对表进行重大更改之后,发出ANALYZE TABLE语句,重新计算统计数据。
  • STATS_SAMPLE_PAGES specifies the number of index pages to sample when estimating cardinality and other statistics for an indexed column, such as those calculated by ANALYZE TABLE.
  • STATS_SAMPLE_PAGES指定估计索引列的基数和其他统计信息(例如由ANALYZE TABLE计算的值)时要采样的索引页数。
    All three clauses are specified in the following CREATE TABLE example:
    所有三个子句在以下CREATE TABLE示例中指定:
    CREATE TABLE t1 (
    id int(8) NOT NULL auto_increment,
    data varchar(255),
    date datetime,
    PRIMARY KEY (id),
    INDEX DATE_IX (date)
    ) ENGINE=InnoDB,
    STATS_PERSISTENT=1,
    STATS_AUTO_RECALC=1,
    STATS_SAMPLE_PAGES=25;

14.6.12.1.3 Configuring the Number of Sampled Pages for InnoDB Optimizer Statistics
为InnoDB优化器统计配置采样页数
The MySQL query optimizer uses estimated statistics about key distributions to choose the indexes for an execution plan, based on the relative selectivity of the index. Operations such as ANALYZE TABLE cause InnoDB to sample random pages from each index on a table to estimate the cardinality of the index. (This technique is known as random dives.)
MySQL查询优化器使用关于密钥分配的估计统计信息来根据索引的相对选择性来选择执行计划的索引。诸如ANALYZE TABLE之类的操作会使InnoDB从表中的每个索引中抽取随机页面来估计索引的基数。 (这种技术被称为random dives)
To give you control over the quality of the statistics estimate (and thus better information for the query optimizer), you can change the number of sampled pages using the parameter innodb_stats_persistent_sample_pages, which can be set at runtime.
为了让您控制统计数据的质量(从而更好地为查询优化器提供信息),您可以使用参数innodb_stats_persistent_sample_pages来更改抽样页面的数量,该参数可以在运行时设置。
innodb_stats_persistent_sample_pages has a default value of 20. As a general guideline, consider modifying this parameter when encountering the following issues:
innodb_stats_persistent_sample_pages的默认值为20.作为一般准则,请在遇到以下问题时考虑修改此参数:
1:Statistics are not accurate enough and the optimizer chooses suboptimal plans, as shown by EXPLAIN output. The accuracy of statistics can be checked by comparing the actual cardinality of an index (as returned by running SELECT DISTINCT on the index columns) with the estimates provided in the mysql.innodb_index_stats persistent statistics table.
If it is determined that statistics are not accurate enough, the value of innodb_stats_persistent_sample_pagesshould be increased until the statistics estimates are sufficiently accurate. Increasinginnodb_stats_persistent_sample_pages too much, however, could cause ANALYZE TABLE to run slowly.
统计信息不够准确,优化程序选择次优计划,如EXPLAIN输出所示。 统计的准确性可以通过比较索引的实际基数(通过在索引列上运行SELECT DISTINCT返回)与在mysql.innodb_index_stats持久统计信息表中提供的估计值来检查。
如果确定统计数据不够准确,则应增加innodb_stats_persistent_sample_pagess的值,直到统计数据估计足够精确。 然而,增加innodb_stats_persistent_sample_pages太多会导致ANALYZE TABLE运行缓慢。
2:ANALYZE TABLE is too slow. In this case innodb_stats_persistent_sample_pages should be decreased until ANALYZE TABLE execution time is acceptable. Decreasing the value too much, however, could lead to the first problem of inaccurate statistics and suboptimal query execution plans.
ANALYZE TABLE太慢。 在这种情况下,应该减少innodb_stats_persistent_sample_pages,直到ANALYZE TABLE执行时间可以接受为止。 但是,如果将价值降低太多,可能会导致统计数据不准确和次优查询执行计划的第一个问题。
If a balance cannot be achieved between accurate statistics and ANALYZE TABLE execution time, consider decreasing the number of indexed columns in the table or limiting the number of partitions to reduce ANALYZE TABLE complexity. The number of columns in the table's primary key is also important to consider, as primary key columns are appended to each nonunique index.
For related information, see Section 14.6.12.3, “Estimating ANALYZE TABLE Complexity for InnoDB Tables”.
如果在准确的统计信息和ANALYZE TABLE执行时间之间无法实现平衡,请考虑减少表中索引列的数量或限制分区数以减少ANALYZE TABLE复杂性。 因为主键列被附加到每个非唯一索引,所以表的主键列数也很重要。更多有关信息,请看Section 14.6.12.3, “Estimating ANALYZE TABLE Complexity for InnoDB Tables”.

14.6.12.1.4 Including Delete-marked Records in Persistent Statistics Calculations

在持久统计计算中包括删除标记的记录
By default, InnoDB reads uncommitted data when calculating statistics. In the case of an uncommitted transaction that deletes rows from a table, InnoDB excludes records that are delete-marked when calculating row estimates and index statistics, which can lead to non-optimal execution plans for other transactions that are operating on the table concurrently using a transaction isolation level other than READ UNCOMMITTED. To avoid this scenario,innodb_stats_include_delete_marked can be enabled to ensure that InnoDB includes delete-marked records when calculating persistent optimizer statistics.
默认情况下,InnoDB在计算统计信息时读取未提交的数据。对于从表中删除行的未提交事务,InnoDB在计算行估计和索引统计数据时排除了删除标记的记录。这可能会导致使用除READ UNCOMMITTED以外的事务隔离级别同时在表上操作的其他事务的非最佳执行计划。为了避免这种情况,可以启用innodb_stats_include_delete_marked来确保InnoDB在计算持久化优化器统计信息时包含删除标记的记录。
When innodb_stats_include_delete_marked is enabled, ANALYZE TABLE considers delete-marked records when recalculating statistics.
当启用innodb_stats_include_delete_marked时,ANALYZE TABLE会在重新计算统计信息时考虑删除标记的记录。
innodb_stats_include_delete_marked is a global setting that affects all InnoDB tables, and it is only applicable to persistent optimizer statistics.
innodb_stats_include_delete_marked是一个影响所有InnoDB表的全局设置,它仅适用于持久化优化器统计信息。
innodb_stats_include_delete_marked was introduced in MySQL 5.7.16.
innodb_stats_include_delete_marked是在MySQL 5.7.16中引入的。

14.6.12.1.5 InnoDB Persistent Statistics Tables
InnoDB持久统计表
The persistent statistics feature relies on the internally managed tables in the mysql database, named innodb_table_stats and innodb_index_stats. These tables are set up automatically in all install, upgrade, and build-from-source procedures.
持久性统计特性依赖于mysql数据库中的内部管理表,命名为innodb_table_stats和innodb_index_stats。这些表在所有安装、升级和源代码生成过程中自动设置。

Table 14.3 Columns of innodb_table_stats

14.6.12.1 Configuring Persistent Optimizer Statist

Table 14.4 Columns of innodb_index_stats
14.6.12.1 Configuring Persistent Optimizer Statist

Both the innodb_table_stats and innodb_index_stats tables include a last_update column showing when InnoDBlast updated index statistics, as shown in the following example:
innodb_table_stats和innodb_index_stats表都包含last_update列,显示InnoDB上次更新索引统计信息的时间,如以下示例所示:

mysql> SELECT FROM innodb_table_stats \G
database_name: sakila
table_name: actor
last_update: 2014-05-28 16:16:44
n_rows: 200
clustered_index_size: 1
sum_of_other_index_sizes: 1
...
mysql> SELECT
FROM innodb_index_stats \G
database_name: sakila
table_name: actor
index_name: PRIMARY
last_update: 2014-05-28 16:16:44
stat_name: n_diff_pfx01
stat_value: 200
sample_size: 1
...
The innodb_table_stats and innodb_index_stats tables are ordinary tables and can be updated manually. The ability to update statistics manually makes it possible to force a specific query optimization plan or test alternative plans without modifying the database. If you manually update statistics, issue the FLUSH TABLE tbl_name command to make MySQL reload the updated statistics.
innodb_table_stats和innodb_index_stats表是普通表,可以手动更新。 通过手动更新统计信息的功能,可以强制执行特定的查询优化计划或测试备选计划,而无需修改数据库。如果手动更新统计信息,请发出FLUSH TABLE tbl_name命令以使MySQL重新加载更新的统计信息。
Persistent statistics are considered local information, because they relate to the server instance. The innodb_table_statsand innodb_index_stats tables are therefore not replicated when automatic statistics recalculation takes place. If you run ANALYZE TABLE to initiate a synchronous recalculation of statistics, this statement is replicated (unless you suppressed logging for it), and recalculation takes place on the replication slaves.
持久性统计信息被视为本地信息,因为它们与服务器实例相关。因此innodb_table_stats和innodb_index_stats表在重新计算自动统计信息时不会被复制。如果运行ANALYZE TABLE以启动统计信息的同步重新计算,则会复制此语句(除非您禁止对其进行日志记录),并且在复制slave上进行重新计算

14.6.12.1.6 InnoDB Persistent Statistics Tables Example

InnoDB持久统计表示例
The innodb_table_stats table contains one row per table. The data collected is demonstrated in the following example.
innodb_table_stats表每行包含一个表。下面的示例演示了所收集的数据。
Table t1 contains a primary index (columns a, b) secondary index (columns c, d), and unique index (columns e, f)
表t1包含主索引(列a,b)次索引(列c,d)和唯一索引(列e,f):
CREATE TABLE t1 (
a INT, b INT, c INT, d INT, e INT, f INT,
PRIMARY KEY (a, b), KEY i1 (c, d), UNIQUE KEY i2uniq (e, f)
) ENGINE=INNODB;
After inserting five rows of sample data, the table appears as follows:
插入五行样本数据后,表格如下所示:
mysql> SELECT FROM t1;
+---+---+------+------+------+------+
| a | b | c | d | e | f |
+---+---+------+------+------+------+
| 1 | 1 | 10 | 11 | 100 | 101 |
| 1 | 2 | 10 | 11 | 200 | 102 |
| 1 | 3 | 10 | 11 | 100 | 103 |
| 1 | 4 | 10 | 12 | 200 | 104 |
| 1 | 5 | 10 | 12 | 100 | 105 |
+---+---+------+------+------+------+
To immediately update statistics, run ANALYZE TABLE (if innodb_stats_auto_recalc is enabled, statistics are updated automatically within a few seconds assuming that the 10% threshold for changed table rows is reached):
要立即更新统计信息,请运行ANALYZE TABLE(如果启用了innodb_stats_auto_recalc,则假设已达到更改的表行的10%阈值,则会在几秒内自动更新统计信息):
mysql> ANALYZE TABLE t1;
+---------+---------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+---------+---------+----------+----------+
| test.t1 | analyze | status | OK |
+---------+---------+----------+----------+
Table statistics for table t1 show the last time InnoDB updated the table statistics (2014-03-14 14:36:34), the number of rows in the table (5), the clustered index size (1 page), and the combined size of the other indexes (2 pages).
表t1的表统计数据显示InnoDB上次更新表统计信息(2014-03-14 14:36:34),表中的行数(5),聚簇索引大小(1页)和组合 其他指标的大小(2页)。
mysql> SELECT
FROM mysql.innodb_table_stats WHERE table_name like 't1'\G
database_name: test
table_name: t1
last_update: 2014-03-14 14:36:34
n_rows: 5
clustered_index_size: 1
sum_of_other_index_sizes: 2
The innodb_index_stats table contains multiple rows for each index. Each row in the innodb_index_stats table provides data related to a particular index statistic which is named in the stat_name column and described in thestat_description column. For example:
innodb_index_stats表包含每个索引的多行。 innodb_index_stats表中的每一行都提供与特定索引统计信息相关的数据,该统计信息在stat_name列中指定,并在stat_description列中进行了描述。 例如:
mysql> SELECT index_name, stat_name, stat_value, stat_description
FROM mysql.innodb_index_stats WHERE table_name like 't1';
+------------+--------------+------------+-----------------------------------+
| index_name | stat_name | stat_value | stat_description |
+------------+--------------+------------+-----------------------------------+
| PRIMARY | n_diff_pfx01 | 1 | a |
| PRIMARY | n_diff_pfx02 | 5 | a,b |
| PRIMARY | n_leaf_pages | 1 | Number of leaf pages in the index |
| PRIMARY | size | 1 | Number of pages in the index |
| i1 | n_diff_pfx01 | 1 | c |
| i1 | n_diff_pfx02 | 2 | c,d |
| i1 | n_diff_pfx03 | 2 | c,d,a |
| i1 | n_diff_pfx04 | 5 | c,d,a,b |
| i1 | n_leaf_pages | 1 | Number of leaf pages in the index |
| i1 | size | 1 | Number of pages in the index |
| i2uniq | n_diff_pfx01 | 2 | e |
| i2uniq | n_diff_pfx02 | 5 | e,f |
| i2uniq | n_leaf_pages | 1 | Number of leaf pages in the index |
| i2uniq | size | 1 | Number of pages in the index |
+------------+--------------+------------+-----------------------------------+
The stat_name column shows the following types of statistics:
stat_name列显示以下类型的统计信息

  • size: Where stat_name=size, the stat_value column displays the total number of pages in the index.
    size:其中stat_name = size,stat_value列显示索引中的总页数。
  • n_leaf_pages: Where stat_name=n_leaf_pages, the stat_value column displays the number of leaf pages in the index.
    n_leaf_pages:其中stat_name = n_leaf_pages,stat_value列显示索引中叶子页的数量。
  • n_diff_pfxNN: Where stat_name=n_diff_pfx01, the stat_value column displays the number of distinct values in the first column of the index. Where stat_name=n_diff_pfx02, the stat_value column displays the number of distinct values in the first two columns of the index, and so on. Additionally, where stat_name=n_diff_pfxNN, the stat_description column shows a comma separated list of the index columns that are counted.
    n_diff_pfxNN:其中stat_name = n_diff_pfx01,stat_value列显示索引第一列中不同值的数量。其中stat_name = n_diff_pfx02,stat_value列显示索引前两列中不同值的数量,依此类推。此外,在stat_name = n_diff_pfxNN的情况下,stat_description列显示一个以逗号分隔的计算索引列的列表。

To further illustrate the n_diff_pfxNN statistic, which provides cardinality data, consider once again the t1 table example that was introduced previously. As shown below, the t1 table is created with a primary index (columns a, b), a secondary index (columns c, d), and a unique index (columns e, f)
为了进一步说明提供基数数据的n_diff_pfxNN统计量,再次考虑先前引入的t1表格示例。 如下所示,使用主索引(列a,b),辅助索引(列c,d)和唯一索引(列e,f)创建t1表:
CREATE TABLE t1 (
a INT, b INT, c INT, d INT, e INT, f INT,
PRIMARY KEY (a, b), KEY i1 (c, d), UNIQUE KEY i2uniq (e, f)
) ENGINE=INNODB;
After inserting five rows of sample data, the table appears as follows:
插入5行样本数据后,表如下:
mysql> SELECT * FROM t1;
+---+---+------+------+------+------+
| a | b | c | d | e | f |
+---+---+------+------+------+------+
| 1 | 1 | 10 | 11 | 100 | 101 |
| 1 | 2 | 10 | 11 | 200 | 102 |
| 1 | 3 | 10 | 11 | 100 | 103 |
| 1 | 4 | 10 | 12 | 200 | 104 |
| 1 | 5 | 10 | 12 | 100 | 105 |
+---+---+------+------+------+------+
When you query the index_name, stat_name, stat_value, and stat_description where stat_name LIKE 'n_diff%', the following result set is returned:
当查询index_name,stat_nam,stat_value和stat_description(其中stat_name如'n_diff%')时,返回以下结果集:
mysql> SELECT index_name, stat_name, stat_value, stat_description
FROM mysql.innodb_index_stats
WHERE table_name like 't1' AND stat_name LIKE 'n_diff%';
+------------+--------------+------------+------------------+
| index_name | stat_name | stat_value | stat_description |
+------------+--------------+------------+------------------+
| PRIMARY | n_diff_pfx01 | 1 | a |
| PRIMARY | n_diff_pfx02 | 5 | a,b |
| i1 | n_diff_pfx01 | 1 | c |
| i1 | n_diff_pfx02 | 2 | c,d |
| i1 | n_diff_pfx03 | 2 | c,d,a |
| i1 | n_diff_pfx04 | 5 | c,d,a,b |
| i2uniq | n_diff_pfx01 | 2 | e |
| i2uniq | n_diff_pfx02 | 5 | e,f |
+------------+--------------+------------+------------------+
For the PRIMARY index, there are two n_diff% rows. The number of rows is equal to the number of columns in the index.
对于PRIMARY索引,有两个n_diff%行。行数等于索引中的列数。
Note
For nonunique indexes, InnoDB appends the columns of the primary key.
对于非唯一索引,InnoDB追加主键列。

  • Where index_name=PRIMARY and stat_name=n_diff_pfx01, the stat_value is 1, which indicates that there is a single distinct value in the first column of the index (column a). The number of distinct values in column a is confirmed by viewing the data in column a in table t1, in which there is a single distinct value (1). The counted column (a) is shown in the stat_description column of the result set.
    在index_name=PRIMARY和stat_name=n_diff_pfx01中,stat_value为1,这表明在索引的第一列(a列)中有一个单独的值。通过查看表t1中a列中的数据,确定列a中不同值的数量,其中只有一个不同值(1)..计数列(a)显示在结果集的stat_description列中。
  • Where index_name=PRIMARY and stat_name=n_diff_pfx02, the stat_value is 5, which indicates that there are five distinct values in the two columns of the index (a,b). The number of distinct values in columns a and b is confirmed by viewing the data in columns a and b in table t1, in which there are five distinct values: (1,1), (1,2), (1,3), (1,4) and (1,5). The counted columns (a,b) are shown in the stat_description column of the result set.
    其中index_name = PRIMARY和stat_name = n_diff_pfx02,stat_value为5,表示索引的两列中有五个不同的值(a,b),通过查看表t1中a和b列中的数据,确定a和b列中不同值的数量,其中有五个不同值:(1,1)、(1,2)、(1,3)、(1,4)和(1,5)。计数的列(a,b)显示在结果集的stat_description列中。
    For the secondary index (i1), there are four n_diff% rows. Only two columns are defined for the secondary index (c,d) but there are four n_diff% rows for the secondary index because InnoDB suffixes all nonunique indexes with the primary key. As a result, there are four n_diff% rows instead of two to account for the both the secondary index columns (c,d) and the primary key columns (a,b).
    对于辅助索引(i1),有4行n_diff%。二级索引(c,d)只定义了两列,但二级索引有4行n_diff%,因为InnoDB为所有非唯一索引添加了主键后缀。.因此,有4个n_diff%行,而不是2行,用于表示辅助索引列(c,d)和主键列(a,b)。
  • Where index_name=i1 and stat_name=n_diff_pfx01, the stat_value is 1, which indicates that there is a single distinct value in the first column of the index (column c). The number of distinct values in column c is confirmed by viewing the data in column c in table t1, in which there is a single distinct value: (10). The counted column (c) is shown in the stat_description column of the result set.
    其中index_name = i1和stat_name = n_diff_pfx01,stat_value为1,表示索引第一列(列c)中存在单个不同的值。在列c中的不同值的数量通过查看表t1中的列c中的数据来确认,其中存在单个不同的值:(10)。 计数的列(c)显示在结果集的stat_description列中。
  • Where index_name=i1 and stat_name=n_diff_pfx02, the stat_value is 2, which indicates that there are two distinct values in the first two columns of the index (c,d). The number of distinct values in columns c an d is confirmed by viewing the data in columns c and d in table t1, in which there are two distinct values: (10,11) and (10,12). The counted columns (c,d) are shown in the stat_description column of the result set.
    其中index_name = i1和stat_name = n_diff_pfx02,stat_value为2,表示索引(c,d)的前两列中有两个不同的值。 通过查看表t1中的列c和d中的数据来确认列c和d中的不同值的数量,其中存在两个不同的值:(10,11)和(10,12)。 计数的列(c,d)显示在结果集的stat_description列中。
  • Where index_name=i1 and stat_name=n_diff_pfx03, the stat_value is 2, which indicates that there are two distinct values in the first three columns of the index (c,d,a). The number of distinct values in columns c, d, and a is confirmed by viewing the data in column c, d, and a in table t1, in which there are two distinct values: (10,11,1) and (10,12,1). The counted columns (c,d,a) are shown in the stat_description column of the result set.
    其中index_name = i1和stat_name = n_diff_pfx03,stat_value为2,表示索引(c,d,a)的前三列中有两个不同的值。 通过查看表t1中的列c,d和a中的数据来确认列c,d和a中的不同值的数目,其中存在两个不同的值:(10,11,1)和(10,12,1)。 计数的列(c,d,a)显示在结果集的stat_description列中。
  • Where index_name=i1 and stat_name=n_diff_pfx04, the stat_value is 5, which indicates that there are five distinct values in the four columns of the index (c,d,a,b). The number of distinct values in columns c, d, a and b is confirmed by viewing the data in columns c, d, a, and b in table t1, in which there are five distinct values: (10,11,1,1), (10,11,1,2), (10,11,1,3), (10,12,1,4) and (10,12,1,5). The counted columns (c,d,a,b) are shown in thestat_description column of the result set.
    其中index_name = i1和stat_name = n_diff_pfx04,stat_value为5,表示索引(c,d,a,b)的四列中有五个不同的值。 通过查看表t1中列c,d,a和b中的数据来确认列c,d,a和b中的不同值的数目,其中存在五个不同的值:(10,11,1,1 ),(10,11,1,2),(10,11,1,3),(10,12,1,4)和(10,12,1,5)。 计数的列(c,d,a,b)显示在结果集的stat_description列中。
    For the unique index (i2uniq), there are two n_diff% rows.
    对于唯一索引(i2uniq),有两个n_diff%行。
  • Where index_name=i2uniq and stat_name=n_diff_pfx01, the stat_value is 2, which indicates that there are two distinct values in the first column of the index (column e). The number of distinct values in column e is confirmed by viewing the data in column e in table t1, in which there are two distinct values: (100) and (200). The counted column (e) is shown in the stat_description column of the result set.
    其中,index_name = i2uniq和stat_name = n_diff_pfx01,stat_value为2,表示索引第一列(列e)中有两个不同的值。 列e中的不同值的数量通过查看表t1中的列e中的数据来确认,其中存在两个不同的值:(100)和(200)。 计数的列(e)显示在结果集的stat_description列中。
  • Where index_name=i2uniq and stat_name=n_diff_pfx02, the stat_value is 5, which indicates that there are five distinct values in the two columns of the index (e,f). The number of distinct values in columns e and f is confirmed by viewing the data in columns e and f in table t1, in which there are five distinct values: (100,101), (200,102), (100,103), (200,104) and (100,105). The counted columns (e,f) are shown in the stat_description column of the result set.
    其中index_name = i2uniq和stat_name = n_diff_pfx02,stat_value为5,表示索引(e,f)的两列中有五个不同的值。 列e和f中的不同值的数量通过查看表t1中的列e和f中的数据来确认,其中存在五个不同的值:(100,101),(200,102),(100,103),(200,104)和(100105)。 计数的列(e,f)显示在结果集的stat_description列中。

14.6.12.1.7 Retrieving Index Size Using the innodb_index_stats Table

使用innodb_index_stats表检索索引大小。
The size of indexes for tables, partitions, or subpartitions can be retrieved using the innodb_index_stats table. In the following example, index sizes are retrieved for table t1. For a definition of table t1 and corresponding index statistics, see Section 14.6.12.1.6, “InnoDB Persistent Statistics Tables Example”.
表,分区或子分区的索引大小可以使用innodb_index_stats表来检索。 在以下示例中,为表t1检索索引大小。 有关表t1和相应索引统计的定义,更多信息请看Section 14.6.12.1.6, “InnoDB Persistent Statistics Tables Example”.
mysql> SELECT SUM(stat_value) pages, index_name,
SUM(stat_value)@@innodb_page_size size
FROM mysql.innodb_index_stats WHERE table_name='t1'
AND stat_name = 'size' GROUP BY index_name;
+-------+------------+-------+
| pages | index_name | size |
+-------+------------+-------+
| 1 | PRIMARY | 16384 |
| 1 | i1 | 16384 |
| 1 | i2uniq | 16384 |
+-------+------------+-------+
For partitions or subpartitions, the same query with a modified WHERE clause can be used to retrieve index sizes. For example, the following query retrieves index sizes for partitions of table t1:
对于分区或子分区,可以使用具有修改的WHERE子句的相同查询来检索索引大小。 例如,以下查询将检索表t1分区的索引大小:
mysql> SELECT SUM(stat_value) pages, index_name,
SUM(stat_value)
@@innodb_page_size size
FROM mysql.innodb_index_stats WHERE table_name like 't1#P%'
AND stat_name = 'size' GROUP BY index_name;

PREV: 14.6.12 Configuring Optimizer Statistics for InnoDb https://blog.51cto.com/itzhoujun/2359332
NEXT: 14.6.12.2 Configuring Non-Persistent Optimizer Statistics Parameters https://blog.51cto.com/itzhoujun/2359336

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章