关于innodb:InnoDB-的并发控制锁与-MVCC
原文:https://nullwy.me/2023/07/innodb-locking/如果感觉我的文章对你有用,请随便赞叹目前支流数据库事务的并发管制实现,如 MySQL InnoDB、PostgreSQL、Oracle,都应用两阶段封闭 2PL 与 MVCC 技术,但具体实现细节上存在差别。InnoDB 是在以封闭技术为主体的状况下,用 MVCC 技术辅助实现读-写、写-读操作的并发。PostgreSQL 的并发控制技术是以 MVCC 技术为主,封闭技术为辅。本文次要关注 InnoDB 事务的并发管制实现。 背景常识并发管制,是数据库系统的 ACID 个性中的隔离性(Isolation))的保障。所谓隔离性,就是事务的执行不应受到其余并发执行事务的烦扰,事务的执行看上去应与其余事务是隔离的。被隔离的执行,等价于事务的某种串行执行,或者说,它等价于一个没有并发的执行。保障串行性可能只容许极小的并发度,采纳较弱隔离性,能带来更高的并发度,是并发事务的正确性和性能之间的斗争。 晚期各大数据库厂商实现并发管制时多采纳基于封闭的并发控制技术,所以在基于封闭的技术背景下,才在 ANSI SQL-92 规范中提出了四种隔离级别:未提交读(Read Uncommitted)、己提交读(Read Committed)、可反复读(Repeatable Read)、可串行化(Serializable)(附注:为了书写简便本文将各个隔离级别顺次缩写为 RU、RC、RR、SER)。ANSI SQL-92 规范的四种隔离级别,是依据三种读异常现象(phenomena)定义的,隔离级别和异常现象的关系如下: 隔离级别P1 脏读P2 不可反复读P4 幻读Read Uncommitted可能可能可能Read Committed防止可能可能Repeatable Read防止防止可能Serializable防止防止防止ANSI SQL-92 规范文档对三种读异常现象的定义原文如下 [ref]: The isolation level specifies the kind of phenomena that can occur during the execution of concurrent SQL-transactions. The following phenomena are possible:1) P1 ("Dirty read"): SQL-transaction T1 modifies a row. SQL-transaction T2 then reads that row before T1 performs a COMMIT. If T1 then performs a ROLLBACK, T2 will have read a row that was never committed and that may thus be considered to have never existed.2) P2 ("Non-repeatable read"): SQL-transaction T1 reads a row. SQL-transaction T2 then modifies or deletes that row and performs a COMMIT. If T1 then attempts to reread the row, it may receive the modified value or discover that the row has been deleted.3) P3 ("Phantom"): SQL-transaction T1 reads the set of rows N that satisfy some <search condition>. SQL-transaction T2 then executes SQL-statements that generate one or more rows that satisfy the <search condition> used by SQL-transaction T1. If SQL-transaction T1 then repeats the initial read with the same <search condition>, it obtains a different collection of rows.除了脏读、不可反复读和幻读这 3 种读数据异样外,还有写数据异样,即脏写和失落更新。各个异样的含意如下: ...