天天看點

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列。

繼續閱讀