change buffer (write buffer)

1.1 What is change buffer

  In MySQL, data is divided into two parts: memory and disk; hot data pages and index pages are cached in the buffer pool to reduce disk reads; change buffer is a means to ease disk writes.

  The change buffer is when the non-unique ordinary index page is not in the buffer pool, and the page is written, the record change is buffered first, and when the future data is read, the operation in the change buffer is merged to the original data Page technology. Before MySQL5.5, it was called insert buffer, which was optimized only for insert; now it is also effective for delete and update, called change buffer.

1.2 Principle of change buffer

  When a data page needs to be updated, it is directly updated if the data page is in memory. If the data page is not in memory. Under the premise of not affecting data consistency, InooDB will cache these update operations in the change buffer, so that the data page does not need to be read from the disk. When the next query needs to access the data page, the data page is read into the memory, and then the operations related to this page in the change buffer are executed. In this way, the correctness of the data logic can be guaranteed.
  Although the name is change buffer, it is actually persistent data. In other words, the change buffer is copied in memory and will also be written to disk (ibdata).
  The process of merging the operations in the change buffer into the original data page to obtain the latest result is called merge. The following situations will trigger merge:

  • Visit this data page;
  • The background master thread will merge regularly;
  • When the database buffer pool is not enough;
  • When the database is closed normally;
  • When the redo log is full;

1.3 Why is the change buffer for non-unique ordinary index pages?

Unique index

  • All update operations must first determine whether this operation violates the uniqueness constraint. And this must be determined by reading the data page into the memory.
  • If all have been read into the memory, it will be faster to update the memory directly, and there is no need to use the change buffer.

  Therefore, the change buffer cannot be used for the update of the unique index. In fact, only the ordinary index can be used.

Normal index

  • There is no need to judge the uniqueness, and use the change buffer to update normally.

1.4 Related variables

mysql> show variables like '%change_buffer%';
+-------------------------------+-------+
| Variable_name                 | Value |
+-------------------------------+-------+
| innodb_change_buffer_max_size | 25    |
| innodb_change_buffering       | all   |
+-------------------------------+-------+
2 rows in set (0.00 sec)
#innodb_change_buffering:默认是all支持所有DML操作
#innodb_change_buffer_max_size:默认是25,即缓冲池的1/4。最大可设置为50,采用默认即可

1.5 Monitoring indicators

-------------------------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
 #这行显示了关于size(size 1代表了已经合并记录页的数量)、 free list(代表了插入缓冲中空闲列表长度)和seg size大小(seg size  2显示了当前insert buffer的长度,大小为27572*16K=440M左右)的信 息。0 merges代表合并插入的次数
Ibuf: size 1, free list len 0, seg size 2, 0 merges
#这个标签下的一行信息insert,delete mark,delete 分别表示merge操作合并了多少个insert buffer,delete buffer,purge  buffer 
merged operations:
 insert 0, delete mark 0, delete 0
 #这个标签下的一行信息表示当change buffer发生 merge时表已经被删除了,就不需要再将记录合并到辅助索引中
discarded operations:
 insert 0, delete mark 0, delete 0
 
#因为没有update buffer,所以对一条记录进行update的操作可以分为两个过 程: # A:将记录标记为删除 # B:真正将记录删除 #因此,delete buffer对应update 操作的第一个过程,即将记录标记为删除, purge buffer对应update的第二个过程,即将记录真正地删除

Guess you like

Origin blog.csdn.net/qq_42979842/article/details/108031299