Rough on MySQL transaction isolation levels and characteristics

For such online article already saturated, also wrote that the reason is very simple - as their understanding of the notes.

Foreword

This article, as you learn MySQLsome personal understanding, engines using InnoDb. First to talk about the transaction concept, in 《高性能MySQL》第三版in its affairs described like this:

The transaction is a set of atomic SQL query, or a stand-alone unit of work. If the database engine can successfully all statements that queries the database application, then execute the query. If any of these statements as a crash or other reason unenforceable, then all the statements will not be executed.

In other words, the transaction is a whole unit , which the SQLstatement is not executed alone, like some commodities in general, comprised of multiple components, but I definitely do not sell the components separately, to only buy the commodity, or not Sell.

After a brief understanding of the business, also need to know the purpose of the transaction is to ensure the accuracy and consistency of the data , then for this purpose it is born out of 4a characteristic (back then go into detail), and in order to achieve these four characteristics and requires many specific implementation, including for isolation produced four isolation levels , four isolation levels has produced three issues ( 脏读, 不可重复读and 幻读), and this is their approximate relationship, let's take a look these particular in the end is what.

1 four kinds of properties (ACID)

Speaking of the characteristics of a transaction, it is certainly mouth came ACID, however, in addition to ACIDoutside the point we need to say something else.

原子性(Atomicity): This means that a transaction should be treated as an indivisible smallest unit, the operation of the entire transaction either all succeed or all execution does not perform , like atoms as indivisible (Do not talk to me about Quark), perform here refers to the successful implementation If there is a failure to perform the operation then all is not executed, that is what we usually see a rollback.

一致性(Consistency): Meaning given in the book are matters always jump to another consistent state from a state of consistency . My understanding is involved in a range of data is conserved, that is to say, the whole of the data is unchanged, transfer the money to get everything for example, Aaccounts transferred to B``200a dollar, then made Aand Bthe composition of this data range He said data has not changed(-200+200=0) , but the composition of how the data is changed , it is from one consistent state -> another consistent state.

隔离性(Isolation): Generally speaking, the operation of a transaction for other transactions not visible, that is to say in general affairs are independent. But this isolation level with the database related, in addition to a (yes, you - read uncommitted students) outside the isolation level, the other is not visible, and this transaction levels seen rarely used, so it is 'generally speaking'.

持久性(Durability): Once the transaction is completed, the data change caused by the transaction will be permanent and will not change (unless another transaction changes). But this is actually mentioned in the book associated with the implementation of the strategy, but it seems a bit far away (yes, I do not know!).

These are the affairs of four kinds of characteristics , however, the realization of which is the isolation of isolation level depends on the database.

The database isolation level 2

In MySQLthe isolation level there are four kinds of different, each corresponding transaction isolation levels reflect the possible problems also are different.

未提交读(read uncommited): In this isolation level, the operation performed even if a transaction does not submit other matters can also be seen. At this level in a transaction may not read dirty data submitted by other transactions that may appear dirty read . As shown below, the number indicates the order of execution.

img

It can be seen in the interface 1of a transaction to the testtable inserted a data, this time not even submit the page 2can also see the data submitted by another transaction.

提交读(read commited): After a transaction is committed, other transactions can see the modified transaction . This isolation level may appear the same query is executed the same transaction but read different data, that is, non-repeatable read ( nonrepeatable read), another uncommitted read can also occur non-repeatable read. Examples are as follows

img

​​  可重复读(repeatable read)这是MySQL的默认隔离级别在事务开始的时候会保存此刻的一个快照(这里啰嗦一下,实际上是开启事务后执行第一条语句的时候准备的快照,准备快照的方法则是记录当前事务的版本号,没有进行数据的复制,不明白事务版本号或隐藏字段的可以看看MySQLMVCC),然后接下来这个事务的所有数据读取都是从这个快照读,所以不会出现不可重复读的情况,但是还是有可能出现幻读。意思就是读取的是快照表数据不会变化,但是进行写操作如更新的时候更新的数量可能会跟预期的不同。如图

img

​​  可以看到,在界面1插入一条记录并且提交之后,界面2还是没有读到这个提交的数据,因为他是从事务开始时的快照表读取的所以自然是读不到的,但是在进行更新操作的时候则是更新了意料之外的记录,这就是一种幻读的现象。

​​  可串行化(serializable):意思就是事务要一个一个来,如果在一个事务中进行读操作,那么其他事务在该事务完成前只能进行读操作;如果进行写操作,那么其他事务的操作都进入等待(直到当前事务提交)。这种级别就可以防范目前出现的脏读、不可重复读、幻读等现象。如图

img

上图演示的是事务读时,其他事务不可写,下图是写时不可操作。

img

3 三个问题—脏读、不可重复读、幻读。

​​  这是采取事务的不同隔离级别可能产生的几个问题,在上面隔离级别已经提及到了,但是为了避免混淆还是单独拿出来。

  • 脏读: Refers to read dirty data other matters not yet submitted a transaction took place in read uncommitted level.
  • 不可重复读: The same query may appear different results in a transaction took place in read uncommitted, read committed level . (Personally I feel no need to understand specifically what is called the call can not be repeated, confusingly)
  • 幻读: When a write operation in a transaction with a number of modifications of the number of different expectations, such as changes to the query out before the data.

Some long-winded 不可重复读and 幻读distinction: can be understood as 不可重复读是the piece of field values recorded changes, for example, idto 1record namethe two values are different; but 幻读it is different in quantity , for example, when I check the total 2records, but when a modify operation has updated the 3article.






Reference: "High Performance MySQL", http://www.zsythink.net/archives/1233/

Perhaps I simply want to be recognized.

Guess you like

Origin www.cnblogs.com/zhangweicheng/p/12273797.html