MYSQL(二) 特性 change buffer(1)

什么是change 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

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

In memory, the change buffer occupies part of the buffer pool. On disk, the change buffer is part of the system tablespace, where index changes are buffered when the database server is shut down.

change buffering 是一种特殊的内存结构,修改一个二级索引块(secondary index)时的数据时,
索引块在buffter pool中不存在,修改信息就会被cache在change buffer中,当通过索引扫描把需要的索引块读取到buffer pool时,会和change buffer中修改信息合并,

当其他操作将受影响的页读入缓冲池时,合并缓存的更改,可以避免从磁盘将辅助索引页读入缓冲池所需的大量随机I/O访问

在内存是buffer pool的一部分,在磁盘是在系统表空间中。

原理

对页进行了写操作,并不会立刻将磁盘页加载到缓冲池,而仅仅记录缓冲变更(buffer changes),等未来数据被读取时,再将数据合并(merge)恢复到缓冲池中的技术

change buffer 总的来说就是减少磁盘的IO,把若干对同一页面的更新缓存起来做合并为一次性更新操作,转换随机IO为顺序IO,这样可以避免IO带来的性能损耗,提高数据库的写性能。

比如一个 insert 语句执行
如果没有写change buffer
1.如果buffer pool中没有这个索引页,从磁盘读到缓冲池(一次磁盘io)
2.在内存修改(一个内存io)
3.写入redo(一次顺序的磁盘io)

如果使用了change buffer ,则
1.在change buffer记录对应的操作(一个内存io)
2.写入redo(一次顺序的磁盘io)

有人会觉得,如果数据库崩溃没有写入到磁盘怎么办
这时候有
1.redo能从异常中恢复
2.写缓冲是一个内存结构,和其他的buffer pool一样也会写到磁盘(系统表空间内)

change buffer只作用于二级索引的叶子节点,并且无法处理可能导致索引分裂或合并的操作。
但是在现在ssd越来越便宜的时代,这个参数可能没那么重要,但是在机械盘的情况提升还是很大。

使用的场景

主要适用在
(1)服务器使用机械磁盘
(2)数据库写多读少,而且非唯一索引较多。

对应的参数:

mysql> show variables like ‘%change%’;
±------------------------------±------+
| Variable_name | Value |
±------------------------------±------+
| innodb_change_buffer_max_size | 25 |
| innodb_change_buffering | all |
±------------------------------±------+

innodb_change_buffering是否开启change buffer 默认是all
可配置的参数:
none、inserts、deletes、changes、purges、all

innodb_change_buffer_max_size:

Maximum size for the InnoDB change buffer, as a percentage of the total size of the buffer pool
change buffer 占用buffer pool的比例。默认25

change buffer的结构

更改缓冲区是btree ,能够保存任何二级索引的记录。InnoDB中只有一个更改缓冲区,它一直保存在系统表空间中.
存储在ibdata系统表空间中,根页为ibdata的第4个page(FSP_IBUF_TREE_ROOT_PAGE_NO),
结构如下:
在这里插入图片描述

change buffer的操作类型
`
typedef enum {
IBUF_OP_INSERT = 0,
IBUF_OP_DELETE_MARK = 1,
IBUF_OP_DELETE = 2,

    /* Number of different operation types. */
    IBUF_OP_COUNT = 3

} ibuf_op_t;
`

为了跟踪每个索引页的空闲空间,有了位图页(ibuf bitmap):
The change buffer bitmap page uses 4 bits (IBUF_BITS_PER_PAGE) to describe each page. It contains an array of such 4 bits describing each of the pages. This whole array is called the “ibuf bitmap” (insert/change buffer bitmap)

一共只有两位来标识索引页的可用信息:4个可能的值:0、1、2、3
在这里插入图片描述
这里可以判断是否有可用的空间,保证merge的时候不会导致业务分裂。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值