You get to know one article MySQL transaction isolation level

Foreword

This interview will be asked of it ... although in practice I basically kind have not had practical applications, but still devoted some time to learn to learn MySQL, and do a little recording here.

(I want to learn programming from a small partner search circle T community , more and more industry-related industry information about free video tutorials. Oh, absolutely free!)

Concurrency and locking

Before learning level transaction isolation level, we first need to know why there must be a transaction isolation level isolation level is to make concurrency control.

When multiple queries simultaneously when the concurrency problem arises, for most of the data related to the application, the operating there are two, one is "read", is the "write" (will generating a modified data). then when a read operation and a write operations occur simultaneously, what the outcome will happen? uncertain outcome, is likely to cause abnormal exit an operation, it is also possible operations have been completed but the results It is wrong, such as read the modified data and other abnormalities.

To solve the general problem of concurrency concurrency control, is to establish a common lock, when an operation to manipulate the data, the system locking operations, the lock is released after the completion of the operation, other operations can not be obtained because the lock can not be performed successfully during this period .

Lock types: read-write lock

If any data operations will be completely locked, so concurrent processing capability of the system will be very weak, so the locking system of MySQL, there are two locks: read and write locks.

Read locks : the lock is a shared read lock, operation of the plurality of application to obtain read locks may be obtained simultaneously at the same time.

Write lock : write lock is an exclusive lock that locks the exclusion of other write read and write locks.

Why this design? Think about it, when multiple operations are read operations, at the same time they do not cause confusion or failure of the operation of the data, it can be carried out simultaneously. The multiple write operation or a read operation and a write operation occur simultaneously, the result will not be controlled, and therefore the lock write operation needs to be exclusive lock.

Lock granularity

After designing the type of lock, but it is still not high degree of concurrency, every time an operator must have all of the data locked it? If you modify the data and the data to be read has nothing to do, it will not cause conflict, need to wait for the write operation to complete it? this time it divided the lock granularity.

Table lock : Table lock one kind of spending a small lock operation, he will operate the design data table lock.
Row-level locking : all data line operations designed to be locked, large overhead.

Deadlock

There will lock deadlock, when the two operations each hold our own resources does not release the cut, and then continue to request resources held by the other party, it will deadlock. Once the deadlock, there is no external intervention is not broken , just like the prisoner's dilemma.

The deadlock is difficult to completely avoid, so MySQL provides a more adequate policy deadlock detection, when the deadlock is detected, Innodb will hold a minimum of row-level exclusive lock transaction rollback, in order to to break the deadlock. this is a simple strategy, there are other, more complex algorithm to solve the deadlock, do not do research here.

summary

Can see more exclusive lock on the role of concurrent performance improvements, but he can not solve the problem of writing, table-level locking overhead is small but concurrent performance is not good enough to lock up more data, which may include a lot of useless data, thus in the actual application process, requires access on demand.

Affairs

Understand what matters most classic is the transfer of chestnuts, I believe we all know, there is not to say aside.
A transaction is a series of operations, they have to comply with the ACID properties of the most common understanding it is: either all operations in the transaction succeed or fail. but this is just not enough.

A = Atomicity
Atomicity is said above, either all succeed or all fail. Impossible to carry out only part of the operation.
C = Consistency
system (database) is always transferred from a consistent state to another consistent state, there will be no intermediate state.
the I = isolation
isolation: generally speaking: a transaction before it is fully committed, other transactions are not visible to the attention of the front of the general, the addition of red, meaning that there are exceptions.
D = Durability Rev
persistence, once the transaction is committed, then it will always be like this, even if the system crashes will not affect the outcome of the transaction.

Wanted to upgrade the lock system will bring the same system overhead, the full realization of the transaction is also a gradual increase or decrease in consumption of the system, and MySQL provides us with a variety of options for more than four kinds of characteristics, allows us according to their own the actual situation, choose a more suitable strategy in order to improve performance, which is four kinds of transaction isolation levels provided by MySQL.

Transaction isolation level

MySQL four isolation levels are as follows:

Uncommitted read (READ UNCOMMITTED)

This is known as exceptions to the above, this isolation level, you can see part of other transactions modify this transaction is not committed. Therefore cause problems dirty read (read some of the other uncommitted transactions, but after the transaction It was rolled back).

This level of performance is not big enough advantage, but there are a lot of problems, and therefore rarely used.

Read Committed (READ COMMITTED)

Other transactions can only read part of this transaction has been submitted. This isolation level there is a problem of non-repeatable read, read within the same transaction twice, to get the results turned out to be different, because another transaction data It has been modified.

REPEATABLE READ (repeatable read)

Repeatable Read isolation level to solve the problem (see also know the name), but there are still a new issue of the above non-repeatable read, phantom read is that when you read the id> 10 rows of data, all rows involved plus on a read lock, then insert a new exception to a transaction data a id = 11, because it is newly inserted, so the above exclusion will not trigger lock, then carry out this transaction for the next will find that there is a query id = 11 data, while the last query did not get to, and then insert will have a primary key conflict.

This isolation level is the default storage engine Innodb isolation level.

SERIALIZABLE (serializable)

This is the highest level of isolation can solve indexing problems mentioned above, it will be because he forced the operation of the serial execution, which can lead to the concurrent decline in performance speed, and therefore not very common.

Mysql is implemented automatically submitted, that a statement is not a default transaction, of course, you can turn off the automatic submission by setting the AUTOCOMMIT variable, it can also explicitly open a transaction begin.

MVCC

Locking is a concurrency control way, but after all, is a lock operation resource consuming, so MySQL also implements MVCC (Multi-Version Concurrency Control), the core idea is that not every piece of data plus two version numbers, one is the current data version, a version number is to delete the data through version control, to a certain extent still avoid locking concurrency control can also be achieved.

In MySQL, MVCC works roughly as follows:

select

Query command to obtain the data subject to the following two conditions:

  1. The version number is equal to the current transaction data is less than the version number, which can be found to ensure that the data is either existed before, either this transaction operations.
  2. Delete the version number of the data is either empty or larger than the current version number of the transaction. This ensures Prior to this transaction, the bank data is not deleted.

insert
insert data current version number is equal to the current transaction version number.

delete
to delete the version number is set to delete the line for the current version number of the transaction.

update
the original operation data are deleted, and then insert the new data, the above operation is equivalent to two collection.

summary

Although I really have not found the need to use the MySQL transaction isolation level knowledge of the scene, probably because MySQL do great, right. But we should knowledge of this part of this problem in MySQL because concurrency issues arise, we will not completely helpless.

Reference article

Books <High Performance MySQL>

play. . . .

Guess you like

Origin blog.csdn.net/wanghao112956/article/details/91363642