2020-11-20 事务

ACID模型

Atomicity

The atomicity aspect of the ACID model mainly involves InnoDB transactions. Related MySQL features include:

  • Autocommit setting.

  • COMMIT statement.

  • ROLLBACK statement.

  • Operational data from the INFORMATION_SCHEMA tables.

Consistency

The consistency aspect of the ACID model mainly involves internal InnoDB processing to protect data from crashes. Related MySQL features include:

  • InnoDB doublewrite buffer.

  • InnoDB crash recovery.

Isolation

The isolation aspect of the ACID model mainly involves InnoDB transactions, in particular the isolation level that applies to each transaction. Related MySQL features include:

  • Autocommit setting.

  • SET ISOLATION LEVEL statement.

  • The low-level details of InnoDB locking. During performance tuning, you see these details through INFORMATION_SCHEMA tables.

Durability

The durability aspect of the ACID model involves MySQL software features interacting with your particular hardware configuration. Because of the many possibilities depending on the capabilities of your CPU, network, and storage devices, this aspect is the most complicated to provide concrete guidelines for. (And those guidelines might take the form of buy “new hardware”.) Related MySQL features include:

  • InnoDB doublewrite buffer, turned on and off by the innodb_doublewrite configuration option.

  • Configuration option innodb_flush_log_at_trx_commit.

  • Configuration option sync_binlog.

  • Configuration option innodb_file_per_table.

ACID具体特性

由于mysql数据库的存储引擎innodb遵照了ACID模型规范,所以才有如下ACID特性。

  • a原子性 (一个事务中 多个语句是一个整体要么都成功 要么都还原)
  • c一致性 (要么所有数据都是Commit/Rollback前的状态,要么都是commit/roolback的状态,不存在有事务前的,也有事务后的数据掺杂在一起)
  • i隔离性(多个事务的隔离程度是可控的,最终呈现的结果是可串行化的)
  • d持久性(即Commit/Rollback成功后,数据则持久化了)

事务并发导致的读取问题

假设X Y两个事务,同时操作一个表,原则上某事务开启后,多次读到的同一行的数据应该相同,即同一行数据是可重复读的,其次对于范围行(多行)的读取,也应该多次读取是一致的;但实际会发生如下几类读问题(现象)。

  • 脏读(解决难度:容易):首先 X开启事务 读了c行,其次Y开启事务 更新了c行,再者X又读了c行,发现X两次读到c行数据不一样,即X读到了Y更新仍未提交的脏数据。

  • 不可重复读(解决难度:中等):首先 X开启事务 读了c行;其次Y开启事务 更新(删除)了c行;再者X又读了c行,发现X两次读到c行数据是不一样的;此时Y提交了其更新(删除)的事务;最后X又读了c行,发现后两次和第一次读到c行数据不相同,后两次读到的相同都是更新(删除)后的,由于在一个事务中对同一行重复读取多次时,有可能读到别人在间隔中所做更新(删除)造成的影响,所以叫做不可重复读。

  • 幻读(解决难度:困难):幻影读”是不可重复读的一种特殊场景,特殊在其读取的是一个范围, 首先 X开启事务 读了id大于c的多行,读到m个行;其次Y开启事务 插入了一行数据;此时X又读了id大于c的多行,读到了m+1,再者Y又插入一行数据,并提交了事务,最后,X又读了id大于c的多行,读到了m+2行。就像出现幻觉一样,读到了预期之外的新行。看参考https://michaeljswart.com/2010/03/transaction-phenomena-part-3-the-phantom-read/说幻读的定义仅适用于插入行时,这里还是有些疑惑为何删除行不算呢(后来看了这篇文章也没说清ISO/ANSI standard标准为何成立,但是我个人理解:删除其实分两种,如果读取单行时对于单行删除当然属于不可重复读,但是如果读取的是范围行,删除单或多行应该也属于幻读,当然也属于不可重复读,因为幻读的概念是由于不对多行加锁产生的)。和不可重读的区别是,不可重复读更多强调一行,如果多次读一行时,而其他事务在其间隔删除或更新该行,会导致某行数据不可重复读了。

  • The Non-Repeatable Read is a phenomena specific to a read of a single row. When data has changed in this row, and the row is read again, the changed data is returned. This is a non-repeatable read.
    So what about a change that affects multiple rows? This certainly seems like a phenomenon that applies to more than one row. But actually, I think of this as just a set of non-repeatable reads where each non-repeatable read still only affects one row.

  • The Phantom Read is a phenomenon that deals with queries that return sets. The thing that’s changing in a phantom read is not the data in the rows; it’s the set membership that has changed. *

  • For rows that have been deleted, if a transaction reads them (or rather fails to read them) it would seem that this is both a non-repeatable read and a phantom read. But for the purposes of the ISO/ANSI standard it is in fact considered a non-repeatable read.

隔离级别(定义)

  • 读未提交 不同事务之间,具有读取到其他事务未提交的更新(删除)某行数据权限,已提交的当然就更能读到了。
  • 读已提交 不同事务之间,具有读取到其他事务已提交的更新(删除)某行数据权限,但不具有读取到其他事务未提交的更新(删除)某行数据权限。
  • 可重复读,不同事务之间,不具有读取到其他事务已提交的更新(删除)某行数据的权限,未提交的当然更读不到了。但是对于新插入的某行,不同数据库实际定义有差异,对于mysql innodb,新插入的某行数据(不论是否提交),都不会被其他事务读取到,即innodb实现了完全的可重复读(不仅更、删,插入也支持),不过其他的数据库就不一定了。
  • 可串行化
    不同事务之间,不可以并发执行任何操作,即每个事务都得等待正执行的事务COMMIT/ROLLBACK后,方可操作。如果用锁语言讲的话,就是不同事务的更删查都会上互斥锁。

不同隔离级别解决的问题

是否解决 读未提交 读已提交 可重复读 串行化
脏读(容易解决) 未解决 解决 解决 解决
不可重复读(一般) 未解决 未解决 解决 解决
幻读(解决困难) 未解决 未解决 目前innodb(5.6及以后)解决了 解决
image.png

隔离级别(选择)

隔离级别比较:可串行化>可重复读>读已提交>读未提交

隔离级别对性能的影响比较:可串行化>可重复读>读已提交>读未提交

由此看出,隔离级别越高,所需要消耗的MySQL性能越大(如事务并发严重性),为了平衡二者,一般建议设置的隔离级别为可重复读,MySQL默认的隔离级别也是可重复读。

你可能感兴趣的:(2020-11-20 事务)