天天看点

MySQL 5.7.6: Make InnoDB fill factor settable

git show 3f4abb52fbfe9118fc94a4b60876c16e5aa2e0e0

在该worklog中,将原先的hard code的宏定义btr_cur_page_compress_limit变成可配置的。这个宏有什么作用呢 ? 从它的注释可以看的很清楚:

/** in the pessimistic delete, if the page data size drops below this limit, merging it to a neighbor is tried */

在做悲观删除时,当page内的数据量低于一定的比例时,会尝试合并左右兄弟节点,以节约空间。当前这个值是page的一半(默认8kb),这意味着即使合并成功了,也可能因为合并后的page太满,导致随后出现page分裂。我们可以调低这个百分比因子来减少page 合并,避免过多的分裂。用一定的空间损失来换取性能的提升。

在create/alter table时可以针对表级、索引级指定merge_threshold,例如:

create table t1 (a int , b int , c int , primary key (a) comment ‘merge_threshold=45′, key(b,c) comment ‘merge_threshold=48′) comment ‘merge_threshold=42′; alter table t1 add key (c); root@zwx 11:56:51>select name, merge_threshold from information_schema.innodb_sys_indexes where merge_threshold != 50 and table_id = 1275; +———+—————–+ | name    | merge_threshold | | primary |              45 | | b       |              48 | | c       |              42 |

使用comment来设定merge threshold, 新建的索引默认继承表级设置,除非显式指定。更具体的参考官方文档:

<a href="http://dev.mysql.com/doc/refman/5.7/en/index-page-merge-threshold.html">http://dev.mysql.com/doc/refman/5.7/en/index-page-merge-threshold.html</a>

1.定义及存储

因为定义merge threshold被包含到索引或表的comment中,语法本身未作改动,server层无需变更。

innodb在创建/alter时会去解析comment, 以创建表为例:

ha_create —&gt; ha_innobase::create —&gt;create_table_info_t::create_table_update_dict —&gt; innobase_parse_hint_from_comment

innobase_parse_hint_from_comment是新增的主函数,用于解析comment中的hint:

//解析表级comment及每个索引的comment,函数innobase_parse_merge_threshold,其实就是简单的strstr函数定位到merge_threshold=,再进行整型转换(atoi)

//更新sys_index系统表

////gen_clust_index(无唯一键或主键时)总是使用表级定义的merge threshold

////dict_index_set_merge_threshold:在sys_index中更新每个索引的merge threshold字段

////更新dict_index_t::merge_threshold

2.使用

将原先的hardcode替换成使用索引对象上的dict_index_t::merge_threshold

参考函数:

btr_cur_will_modify_tree,该函数用于决定修改记录是否可能导致索引树修改 btr_cur_compress_recommendation, //检查是否可以合并索引页 btr_cur_can_delete_without_compress  //删除记录时 page_delete_rec  //删除记录时

3.加载

在索引加入内内存时,会从sys_index系统表中读取索引信息,

参考函数:dict_load_index_low

展示:

修改i_s表innodb_sys_indexes,增加新列来展示索引上的threshold

兼容性

由于系统表sys_indexes表发生了变化,原地升级后的新版本如果不做upgrade,表的定义也需要向前兼容,因此容忍该表缺少merge threshold列。

继续阅读