Files
rikako-note/mysql/mysql底层/Undo logging and History System.md
2025-08-05 00:00:06 +08:00

23 lines
1.7 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Undo logging and History System
innodb存储引擎实现了多版本并发控制MVCC故而不同用户在访问数据时会读取到数据不同版本的快照取决于事务的隔离级别
通过MVCC其为用户提供了一致的视图。undo logging和history system则是实现mvcc的底层机制。
## 当innodb修改时会保存备份
在innodb针对mvcc的实现中如果某一条记录发生了修改那么当前记录的`修改前版本`将会作为`undo record`被记录到undo log中。
通过undo log用户能够将修改后的数据还原到修改前的版本。
对于innodb中的记录每条记录都会包含其`most recent undo record`的引用,引用被称为`rollback pointer``ROLL_PTR`。并且每条undo record还包含指向其前一条undo record的引用init record insert除外
综上所述,每条记录都包含一个记录了`all versions of record`的undo record chain。故而可以通过undo record chain构建记录的所有先前版本数据。
## Global history and purge operations
每条记录都可以通过undo record chain还原到先前的版本除此之外在整个数据库层面还有一个全局的history view该history view被称之为`history list`。当每个事务被提交时,事务关联的`history`将会被链接到global history list中history顺序按照事务提交顺序。history list主要用用于数据的清理。
innodb会运行一个连续的`清理`process其主要负责如下内容
-`delete-marked records`进行`实际删除`
- 释放undo log page并且将其从history list中移除使得其可以被reuse