{{ it.name }}
{{ it.text }}
场景二:
页 D 和页 E,两个页面记录占用都在 49%。那么页合并后,页 D 记录占用 98%,只剩下 2%。
页 F 和页 H,两个页面记录占用也都是 49%,那么合并后,页 F 记录占用 98%,也只剩下 2%。
1. 表的 MERGE_THRESHOLD
mysql> create table sample1(id int primary key,r1 int, r2 varchar(1000)) comment 'MERGE_THRESHOLD=40';Query OK, 0 rows affected (0.08 sec)
mysql> alter table t1 comment 'MERGE_THRESHOLD=40';Query OK, 0 rows affected (0.02 sec)Records: 0 Duplicates: 0 Warnings: 0
2. 索引的 MERGE_THRESHOLD
mysql> create table t1(id int, key idx_id(id) comment 'MERGE_THRESHOLD=40');Query OK, 0 rows affected (0.08 sec)
mysql> alter table t1 drop key idx_id, add key idx_id(id) comment 'MERGE_THRESHOLD=40';Query OK, 0 rows affected (0.02 sec)Records: 0 Duplicates: 0 Warnings: 0
mysql> create index idx_id on t1(id) comment 'MERGE_THRESHOLD=40';Query OK, 0 rows affected (0.05 sec)Records: 0 Duplicates: 0 Warnings: 0
3. 查看 MERGE_THRESHOLD
mysql> show create table sample1\G...) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='MERGE_THRESHOLD=40'1 row in set (0.00 sec)
mysql> show index from t1\G... Comment:Index_comment: MERGE_THRESHOLD=401 row in set (0.00 sec)
mysql> select a.name as tablename,b.name as index_name, b.MERGE_THRESHOLD from innodb_tables as a,innodb_indexes as b where a.table_id = b.table_id and a.name like 'ytt%';+-------------+-----------------+-----------------+| tablename | index_name | MERGE_THRESHOLD |+-------------+-----------------+-----------------+| ytt/sample1 | PRIMARY | 40 || ytt/t1 | GEN_CLUST_INDEX | 50 || ytt/t1 | idx_id | 40 |+-------------+-----------------+-----------------+3 rows in set (0.00 sec)
4. 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 |+-----------------------------+----------------------------------------+2 rows in set (0.00 sec)
mysql> set global innodb_monitor_enable='index_page_merge_attempts';Query OK, 0 rows affected (0.00 sec)mysql> set global innodb_monitor_enableQuery OK, 0 rows affected (0.00 sec)
mysql> create table t1_max(id int primary key,r1 int, key idx_r1 (r1));Query OK, 0 rows affected (0.08 sec)mysql> create table t1_min(id int, primary key (id) comment 'MERGE_THRESHOLDQuery OK, 0 rows affected (0.08 sec)
mysql> set global innodb_monitor_disable='index_page_merge_attempts';Query OK, 0 rows affected (0.00 sec)mysql> set global innodb_monitor_disableQuery OK, 0 rows affected (0.00 sec)mysql> set global innodb_monitor_reset_allQuery OK, 0 rows affected (0.00 sec)mysql> set global innodb_monitor_enableQuery OK, 0 rows affected (0.00 sec)
replace into t1_max select ceil(rand()*1000),ceil(rand()*100) ;...mysql> select count(*) from t1_max+----------+| count(*) |+----------+| 1000 |+----------+1 row in set (0.03 sec)
mysql> delete from t1_max limit 500;Query OK, 500 rows affected (0.05 sec)
mysql> SELECT name,count,max_count,avg_count FROM INFORMATION_SCHEMA.INNODB_METRICS WHERE NAME like '%index_page_merge%';+-----------------------------+-------+-----------+--------------------+| name | count | max_count | avg_count |+-----------------------------+-------+-----------+--------------------+|| index_page_merge_successful | 20 | 20 | 0.072992700729927 |+-----------------------------+-------+-----------+--------------------+2 rows in set (0.00 sec)
mysql> select count(*) from t1_min;+----------+| count(*) |+----------+| 500 |+----------+1 row in set (0.02 sec)mysql> delete from t1_min limit 500Query OK, 500 rows affected (0.02 sec)
mysql> SELECT name,count,max_count,avg_count FROM INFORMATION_SCHEMA.INNODB_METRICS WHERE NAME like '%index_page_merge%';+-----------------------------+-------+-----------+---------------------+| name | count | max_count | avg_count |+-----------------------------+-------+-----------+---------------------+|| index_page_merge_successful | 2 | 2 | 0.03333333333333333 |+-----------------------------+-------+-----------+---------------------+2 rows in set (0.00 sec)
mysql> SELECT name,count,max_count,avg_count FROM INFORMATION_SCHEMA.INNODB_METRICS WHERE NAME like '%index_page_merge%';+-----------------------------+-------+-----------+-----------+| name | count | max_count | avg_count |+-----------------------------+-------+-----------+-----------+|| index_page_merge_successful | 0 | NULL | 0 |+-----------------------------+-------+-----------+-----------+2 rows in set (0.00 sec)
这篇我介绍了 MySQL 数据库索引页合并临界值的概念以及如何在实际环境中评估这个值对索引页合并以及拆分的影响,有问题或者相关建议欢迎指正回复。