MySQL Tuning Series (8) - Transaction and Lock Classification

1. What is the business

A transaction is a logical set of operations that either are performed or none are performed.
The sql statement is as follows:

# 开启事务
START TRANSACTION;
# 多条 SQL 语句
SQL1,SQL2...
## 提交事务
COMMIT;

Second, the characteristics of the transaction (ACID)

Atomicity: A transaction is the smallest unit of execution and cannot be split. The atomicity of the transaction ensures that the action is either completely completed or does not work at all; ( execution completion )
Consistency: before and after the execution of the transaction, the data remains consistent. For example, in the transfer business, no matter whether the transaction is successful or not, the total amount of the transferor and the payee should remain unchanged; (data consistency) Isolation: a user’s transaction is not interfered by other transactions, and the database between concurrent transactions is independent; (mutually independent) Durability: after a
transaction is committed
. Its changes to the data in the database are persistent. ( persistent changes )

3. Problems caused by concurrent transactions

Dirty write : For the same row of data, the update operation of a transaction on the row overrides the update operation of other transactions on the row of data. The essence of update loss is the conflict of write operations . The solution is to let each transaction execute in a serial manner, and write operations in a certain order.

Dirty read : One transaction reads the uncommitted data of another transaction , and the subsequent transaction is rolled back, so the dirty data is found; dirty read is essentially a conflict between read and write operations , and the solution is to write first and then read, that is, read after writing.

Non-repeatable read : In the same transaction, using the same query statement, the result data read at different times is inconsistent. The essence of non-repeatability is also the conflict of read and write operations . The solution is to read first and then write, that is, write after reading.

Phantom reading : The number of data result sets read twice by a transaction is different. Phantom reading is essentially a conflict between read and write operations. The solution is to read first and then write, that is, read and then write.
Non-repeatable reads focus on updates and deletions, while phantom reads focus on insert operations.

4. Transaction isolation level

READ-UNCOMMITTED (read uncommitted) : The lowest isolation level that allows reading uncommitted data changes, which may cause dirty reads, phantom reads, or non-repeatable reads.
READ-COMMITTED (read committed) : Allows to read data that has been committed by concurrent transactions, which can prevent dirty reads, but phantom reads or non-repeatable reads may still occur.
REPEATABLE-READ (repeatable read) : The results of multiple reads of the same field are consistent, unless the data is modified by the transaction itself, which can prevent dirty reads and non-repeatable reads, but phantom reads may still occur.
SERIALIZABLE (serializable) : The highest isolation level, fully compliant with the ACID isolation level. All transactions are executed one by one, so that there is no possibility of interference between transactions, that is, this level can prevent dirty reads, non-repeatable reads, and phantom reads.
To sum up:
insert image description here
methods to solve phantom reads:
(1) Serialization
(2) Snapshot read : In case of repeatable read, use MVCC mechanism (multi-version concurrency control)
(3) Current read : In case of repeatable read, use Next-Key Lock for locking. Next-Key Lock is a combination of row lock (Record Lock) and gap lock (Gap Lock). Row lock can only lock existing rows.
(4) Table lock : At the repeatable read transaction level, add a table lock to the table operated by the transaction.

Five, lock classification

1. Deadlock

Four necessary conditions:
(1) Mutual exclusion , when a resource is used (occupied) by one thread, other threads cannot use it.
(2) Inalienable , resource requesters cannot forcibly seize resources from resource occupants, and resources can only be actively released by resource occupants.
(3) Request and hold , that is, when the resource requester requests other resources while maintaining the possession of the original resource.
(4) Circular waiting , that is, there is a waiting queue: P1 occupies the resources of P2, P2 occupies the resources of P3, and P3 occupies the resources of P1. This forms a loop waiting.

A欠B100块钱,B欠C一百块钱,C欠A一百块。A说C还我才能还B,B说A还我才能还C,C 说B还我才能还A。

Breaking any of the above conditions can remove the deadlock.

2. Mysql lock classification

As shown in the picture:
insert image description here

Due to the existence of MVCC, for general SELECT statements, InnoDB will not add any locks.

Guess you like

Origin blog.csdn.net/liwangcuihua/article/details/131379341