lihongjie0209 / myblog

4 stars 0 forks source link

MySQL: checkpoint #170

Open lihongjie0209 opened 3 years ago

lihongjie0209 commented 3 years ago

InnoDB implements a checkpoint mechanism known as fuzzy checkpointing. InnoDB flushes modified database pages from the buffer pool in small batches. There is no need to flush the buffer pool in one single batch, which would disrupt processing of user SQL statements during the checkpointing process.

During crash recovery, InnoDB looks for a checkpoint label written to the log files. It knows that all modifications to the database before the label are present in the disk image of the database. Then InnoDB scans the log files forward from the checkpoint, applying the logged modifications to the database.

原理:

  1. mysql定期把内存中的脏页落盘, 落盘之后把lsn写入到日志中. 恢复的时候只需要从最新的checkpoint开始就可以了.
lihongjie0209 commented 3 years ago

As you may know, a transactional engine writes transactional logs (name you may see: “redo logs”, write-ahead logs or WAL etc), to be able to perform a crash recovery in the case of database crash or server power outage. To maintain somehow the time for recovery (we all expect that database will start quick), the engine has to limit how many changes are in logs. For this a transactional engine performs a “checkpoint,” which basically synchronizes changes in memory with corresponding changes in logs – so old log records can be deleted. Often it results in writing changed database pages from memory to a permanent storage.

checkpoint的一个重要原因是 redolog 容量有限, 不可能一直记录下去, 把redolog中的记录的操作落盘到tablespace, 那么旧的redolog就可以删除了