1 什么是 Insert Buffer
官网说:
The change buffer is a special data structure that caches changes to secondary index pages when those pages are not in the buffer pool. The buffered changes, which may result from INSERT
, UPDATE
, or DELETE
operations (DML), are merged later when the pages are loaded into the buffer pool by other read operations
翻译:
1、它是一种特殊的数据结构
作用:缓存不在缓冲池中的二级索引页的『
改变
』
2、它通常是由 insert、update、delete 导致
因为这些操作通常会改变二级索引的数据
3、当其他的 read 操作把 the pages 加载到 the buffer pool 中时,它和 the pages 进行合并
合并的结果是:在缓冲池中的脏的二级索引页随后被写入到索引文件中
白话文解释:
以插入操作为例,在进行插入操作的时候,待插入的二级索引数据不是排序好的但是在索引文件中我们又要求二级索引的数据是排序好的,怎么解决这个矛盾的问题?也就是随机读磁盘导致的效率低下问题!就是使用 Change Buffer 缓冲
在插入的时候如果二级索引页不在缓冲池中,那么我们在 the change buffer 中构建二级索引的数据结构而不是直接对磁盘随机读;等待随后其他读取操作把索引页加载到缓冲池中,此时进行 the change buffer 与 索引页的 merged 操作;merged 的结果最后被写入到索引文件中,通过 the change buffer 数据结构减少了随机读操作
那要这么说,如果插入数据到一张空表,就是直接在 the change buffer 中把二级索引的数据结构都构建好,最后直接写入索引文件中了,避免了大量的随机读写
2 为什么要用 Insert Buffer
官网说:
Unlike clustered indexes, secondary indexes are usually nonunique, and inserts into secondary indexes happen in a relatively random order. Similarly, deletes and updates may affect secondary index pages that are not adjacently located in an index tree. Merging cached changes at a later time, when affected pages are read into the buffer pool by other operations, avoids substantial random access I/O that would be required to read secondary index pages into the buffer pool from disk
解释:
跟聚簇索引不同的是,二级索引通常是非唯一的,插入到二级索引会发生随机顺序。类似的删除和更新操作也影响二级索引页。随后,当被影响的页被其它操作读取到缓冲池中,就会发生合并,避免了大量的(从二级缓冲页读取到缓冲池中)随机 I/O 访问
白话文解释:
避免了大量的(把二级缓冲页读取到缓冲池中的)随机 I/O 访问,改变了二级索引的插入效率!
3 什么是 Change Buffer
Change Buffer 是 Insert Buffer 的升级版,InnoDB 存储引擎可以对 DML 操作——INSERT、DELETE、UPDATE 都进行缓冲
4 对 Change Buffer 的配置
innodb_change_buffering
:可选值有all
、inserts
、deletes
等,默认是all
innodb_change_buffer_max_size
:默认值是 25,表示最多占用缓冲池的 25%
5 参考资料
change-buffer: https://dev.mysql.com/doc/refman/8.0/en/innodb-change-buffer.html
clustered indexes:聚簇索引(前提:必须知道)
secondary index:二级索引(前提:必须知道)
传送门: 保姆式Spring5源码解析
欢迎与作者一起交流技术和工作生活