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

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