14.6.13 Configuring the Merge Threshold for Index

You can configure the MERGE_THRESHOLD value for index pages. If the “page-full” percentage for an index page falls below the MERGE_THRESHOLD value when a row is deleted or when a row is shortened by an UPDATE operation, InnoDB attempts to merge the index page with a neighboring index page. The default MERGE_THRESHOLD value is 50, which is the previously hardcoded value. The minimum MERGE_THRESHOLD value is 1 and the maximum value is 50.
您可以爲索引頁配置MERGE_THRESHOLD值。如果一個索引頁的“page-full”百分比低於MERGE_THRESHOLD值,當一行被刪除,或者當一行被更新操作縮短時,InnoDB嘗試將索引頁合併到一個相鄰的索引頁。默認的MERGE_THRESHOLD值爲50,這是之前硬編碼的值。最小的MERGE_THRESHOLD值爲1,最大值爲50。
When the “page-full” percentage for an index page falls below 50%, which is the default MERGE_THRESHOLD setting, InnoDBattempts to merge the index page with a neighboring page. If both pages are close to 50% full, a page split can occur soon after the pages are merged. If this merge-split behavior occurs frequently, it can have an adverse affect on performance. To avoid frequent merge-splits, you can lower the MERGE_THRESHOLD value so that InnoDB attempts page merges at a lower“page-full” percentage. Merging pages at a lower page-full percentage leaves more room in index pages and helps reduce merge-split behavior.
當索引頁的“page-full百分比低於50%(這是默認的MERGE_THRESHOLD設置)時,innodbtry試圖將索引頁與相鄰頁面合併。如果兩個頁面都接近50%滿,那麼在頁面合併後不久就會發生頁面分割。如果這種合併分割行爲頻繁發生,則會對性能產生不利影響。爲了避免頻繁的合併-分割,您可以降低MERGE_THRESHOLD值,以便InnoDB嘗試將頁面合併到一個更低的“page-full”百分比。以較低的page-full百分比合並頁面會在索引頁中留下更多空間,並有助於減少合併拆分行爲。
The MERGE_THRESHOLD for index pages can be defined for a table or for individual indexes. A MERGE_THRESHOLD value defined for an individual index takes priority over a MERGE_THRESHOLD value defined for the table. If undefined, the MERGE_THRESHOLD value defaults to 50.
可以爲表或單個索引定義索引頁的MERGE_THRESHOLD。爲單個索引定義的MERGE_THRESHOLD值優先於爲表定義的MERGE_THRESHOLD值。如果沒有定義,MERGE_THRESHOLD值默認爲50。

Setting MERGE_THRESHOLD for a Table
爲表設置MERGE_THRESHOLD
You can set the MERGE_THRESHOLD value for a table using the table_option COMMENT clause of the CREATE TABLEstatement. For example:
您可以使用CREATE table語句的table_option COMMENT子句爲表設置MERGE_THRESHOLD值。例如:
CREATE TABLE t1 (
id INT,
KEY id_index (id)
) COMMENT='MERGE_THRESHOLD=45';
You can also set the MERGE_THRESHOLD value for an existing table using the table_option COMMENT clause with ALTER TABLE:
您還可以使用ALTER table的表COMMENT子句設置現有表的MERGE_THRESHOLD值:
CREATE TABLE t1 (
id INT,
KEY id_index (id)
);

ALTER TABLE t1 COMMENT='MERGE_THRESHOLD=40';

Setting MERGE_THRESHOLD for Individual Indexes
爲單個索引設置MERGE_THRESHOLD
To set the MERGE_THRESHOLD value for an individual index, you can use the index_option COMMENT clause with CREATE TABLE, ALTER TABLE, or CREATE INDEX, as shown in the following examples:
要爲單個索引設置MERGE_THRESHOLD值,可以使用CREATE TABLE, ALTER TABLE, 或者 CREATE INDEX的所有選項COMMENT子句,如下面的示例所示:

  • Setting MERGE_THRESHOLD for an individual index using CREATE TABLE:
  • 使用CREATE TABLE爲單個索引設置MERGE_THRESHOLD:
    CREATE TABLE t1 (
    id INT,
    KEY id_index (id) COMMENT 'MERGE_THRESHOLD=40'
    );
  • Setting MERGE_THRESHOLD for an individual index using ALTER TABLE:
  • 使用ALTER TABLE爲單個索引設置MERGE_THRESHOLD:
    CREATE TABLE t1 (
    id INT,
    KEY id_index (id)
    );

ALTER TABLE t1 DROP KEY id_index;
ALTER TABLE t1 ADD KEY id_index (id) COMMENT 'MERGE_THRESHOLD=40';

  • Setting MERGE_THRESHOLD for an individual index using CREATE INDEX:
  • 使用CREATE index爲單個索引設置MERGE_THRESHOLD:
    CREATE TABLE t1 (id INT);
    CREATE INDEX id_index ON t1 (id) COMMENT 'MERGE_THRESHOLD=40';

Note
You cannot modify the MERGE_THRESHOLD value at the index level for GEN_CLUST_INDEX, which is the clustered index created by InnoDB when an InnoDB table is created without a primary key or unique key index. You can only modify the MERGE_THRESHOLD value for GEN_CLUST_INDEX by setting MERGE_THRESHOLD for the table.
您不能修改GEN_CLUST_INDEX的索引級別上的MERGE_THRESHOLD值,這是InnoDB創建的聚集索引,當創建一個InnoDB表時,沒有主鍵或惟一鍵索引。您只能通過設置表的MERGE_THRESHOLD來修改GEN_CLUST_INDEX的MERGE_THRESHOLD值。

Querying the MERGE_THRESHOLD Value for an Index
查詢索引的MERGE_THRESHOLD值
The current MERGE_THRESHOLD value for an index can be obtained by querying the INNODB_SYS_INDEXES table. For example:
可以通過查詢INNODB_SYS_INDEXES表來獲得索引的當前MERGE_THRESHOLD值。例如:
mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_SYS_INDEXES WHERE NAME='id_index' \G
INDEX_ID: 91
NAME: id_index
TABLE_ID: 68
TYPE: 0
N_FIELDS: 1
PAGE_NO: 4
SPACE: 57
MERGE_THRESHOLD: 40
You can use SHOW CREATE TABLE to view the MERGE_THRESHOLD value for a table, if explicitly defined using thetable_option COMMENT clause:
如果使用表選項COMMENT子句顯式定義,您可以使用SHOW CREATE TABLE查看錶的MERGE_THRESHOLD值:
mysql> SHOW CREATE TABLE t2 \G
Table: t2
Create Table: CREATE TABLE t2 (
id int(11) DEFAULT NULL,
KEY id_index (id) COMMENT 'MERGE_THRESHOLD=40'
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Note
A MERGE_THRESHOLD value defined at the index level takes priority over a MERGE_THRESHOLD value defined for the table. If undefined, MERGE_THRESHOLD defaults to 50% (MERGE_THRESHOLD=50, which is the previously hardcoded value.
在索引級別定義的MERGE_THRESHOLD值優先於爲表定義的MERGE_THRESHOLD值。如果沒有定義,MERGE_THRESHOLD默認爲50% (MERGE_THRESHOLD=50,這是之前硬編碼的值。
Likewise, you can use SHOW INDEX to view the MERGE_THRESHOLD value for an index, if explicitly defined using theindex_option COMMENT clause:
同樣,如果使用索引選項COMMENT子句顯式定義,您可以使用SHOW INDEX查看索引的MERGE_THRESHOLD值:
mysql> SHOW INDEX FROM t2 \G
Table: t2
Non_unique: 1
Key_name: id_index
Seq_in_index: 1
Column_name: id
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment: MERGE_THRESHOLD=40

Measuring the Effect of MERGE_THRESHOLD Settings
測量MERGE_THRESHOLD設置的效果
The INNODB_METRICS table provides two counters that can be used to measure the effect of a MERGE_THRESHOLD setting on index page merges.
INNODB_METRICS表提供了兩個計數器,可用於度量MERGE_THRESHOLD設置對索引頁面合併的影響。
mysql> SELECT NAME, COMMENT FROM INFORMATION_SCHEMA.INNODB_METRICS
WHERE NAME like '%index_page_merge%';
+-----------------------------+----------------------------------------+
| NAME | COMMENT |
+-----------------------------+----------------------------------------+
| index_page_merge_attempts | Number of index page merge attempts |
| index_page_merge_successful | Number of successful index page merges |
+-----------------------------+----------------------------------------+
When lowering the MERGE_THRESHOLD value, the objectives are:
降低MERGE_THRESHOLD值時,目標爲

  • A smaller number of page merge attempts and successful page merges
  • 較少的頁面合併嘗試和成功的頁面合併
  • A similar number of page merge attempts and successful page merges
  • 類似數量的頁面合併嘗試和成功的頁面合併

A MERGE_THRESHOLD setting that is too small could result in large data files due to an excessive amount of empty page space.
一個太小的MERGE_THRESHOLD設置可能會由於過多的空頁空間而導致大數據文件。
For information about using INNODB_METRICS counters, see Section 14.15.6, “InnoDB INFORMATION_SCHEMA Metrics Table”.
關於INNODB_METRICS計算器的更多信息,請看Section 14.15.6, “InnoDB INFORMATION_SCHEMA Metrics Table”.

PREV: 14.6.12.3 Estimating ANALYZE TABLE Complexity for InnoDB Tables https://blog.51cto.com/itzhoujun/2359337
NEXT: 14.7 InnoDB Tablespaces. https://blog.51cto.com/itzhoujun/2359831

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