innoDb快照读

快照读(Snapshot Read)

MySQL数据库,InnoDB存储引擎,为了提高并发,使用MVCC(多版本并发控制)机制,在并发事务时,通过读取数据行的历史数据版本,不加锁,来提高并发的一种不加锁一致性读(Consistent Nonlocking Read)。一致性读,又称为快照读

1
A consistent read means that InnoDB uses multi-versioning to present to a query a snapshot of the database at a point in time. The query sees the changes made by transactions that committed before that point of time, and no changes made by later or uncommitted transactions. The exception to this rule is that the query sees the changes made by earlier statements within the same transaction.

注意:本事务中修改的数据,即使未提交的数据也可以被本事务的后面部分读取到

快照实现

  • undo log :记录事务变更前的状态。操作数据之前,先将数据备份到undo log,然后进行数据修改(COW:写时备份),如果出现错误或用户执行了rollback语句,则系统就可以利用undo log中的备份数据恢复到事务开始之前的状态。
  • redo log: 记录事务变更后的状态。在事务提交前,只要将redo log持久化即可,数据在内存中变更。当系统崩溃时,虽然数据没有落盘,但是redo log已持久化,系统可以根据redo Log的内容,将所有数据恢复到最新的状态。

参考

一致性读

热评文章