Preface In the previous article Detailed Explanation of MySQL Lock Mechanism, we explained in detail the lock mechanism of InnoDB. The lock mechanism is used to ensure the accuracy of data in concurrent situations. To ensure data accuracy, transactions are usually required. The MySQL storage engine InnoDB cleverly implements the four isolation levels in the isolation characteristics of transactions through the lock mechanism. Transaction ACID characteristics, where I stands for isolation. Isolation means that when concurrent transactions of multiple users access the same database, the transaction of one user should not be interfered with by the transactions of other users, and multiple concurrent transactions should be isolated from each other. We all know the properties of transactions. Consistency and isolation in the database are the basic ideas for implementing transactions. When the system has a large number of concurrent accesses, understanding and skillfully applying the database's own transaction isolation level plays a key role in writing robust code with strong concurrent processing capabilities. 1. How transactions interfere with each other How does one transaction interfere with other transactions? For example, there is the following table: create table lock_example(id smallint(10),name varchar(20),primary key id)engine=innodb; The table contains the following data:
demo1: Transaction A is executed first and is in an uncommitted state: insert into t values(4, 'zhaoliu'); Transaction B, executed later, is also not committed: select * from t; If transaction B can read the record (4, zhaoliu), it means that transaction A has an impact on transaction B. This impact is called "dirty read", that is, the record of uncommitted transaction operations is read. demo2: Transaction A, execute first: select * from t where id=1; The result set is
Transaction B is executed later and committed: update t set name=xxx where id=1; commit; Transaction A executes the same query again: select * from t where id=1; The result set is:
This time, the committed transaction B has an impact on transaction A. This impact is called "non-repeatable read", that is, the same query within a transaction produces different results. demo3: Transaction A, execute first: select * from t where id>3; The result set is:
Transaction B is executed later and committed: insert into t values(4, zhaoliu); commit; Transaction A first queries for id>3 and the result is NULL, so it wants to insert a record with id 4: insert into t values(4, xxoo); The result set is:
You might be thinking. . . Are you fucking kidding me? I checked and it was an empty set if id>3, but when I insert id=4 it told me there was a PK conflict? →_→ This time, the impact of the committed transaction B on transaction A is called "phantom read". As mentioned above, concurrent transactions may cause dirty reads, non-repeatable reads, and phantom reads in other transactions. In order to avoid the above situation, what efforts has InnoDB made? 2. What transaction isolation levels does InnoDB implement? InnoDB implements four different transaction isolation levels:
The isolation level of different transactions is actually a trade-off between consistency and concurrency. 3. How to implement the four transaction isolation levels in InnoDB? InnoDB uses different locking strategies to implement different isolation levels. a. Read Uncommitted At this transaction isolation level, the select statement does not lock and is not a snapshot read. SELECT statements are performed in a nonlocking fashion. At this time, inconsistent data may be read, which is called "dirty read". This is the isolation level with the highest concurrency and the worst consistency. b. Read Committed (RC)
At this time, the insertion of other transactions can still be executed, which may cause phantom records to be read. This level is the most commonly used. And if it is an unlocked select, non-repeatable read may occur. At this level, dirty reads are prevented through snapshot reads. Because snapshot reads at this level can always read the latest row data snapshot, of course, it must be written by a committed transaction, so non-repeatable reads may occur. c. Repeatable Read (RR) This is the default isolation level of InnoDB, under RR:
At this level
d. Serializable At this transaction isolation level, all select statements are implicitly converted to select ... in share mode, which means that a shared read lock (S lock) is set by default. Therefore, if transaction A executes the following SQL statement first, it will try to obtain the IS lock of the queried row (which is compatible with other IS and IX locks). At this time, other transactions can also obtain the IS lock or even the S lock of these rows. However, if transaction A updates or deletes some of the rows next, it will obtain the X lock. Other transactions will be blocked even if they execute ordinary select statements because they try to obtain the IS lock. However, the IS lock and the X lock are mutually exclusive. This avoids dirty reads, non-repeatable reads, and phantom reads, and all transactions can only be executed serially. select ... ; This is the most consistent, but least concurrent isolation level. In high-concurrency scenarios, the above two isolation levels a and d are rarely used. 4. Conclusion Mutual interference between concurrent transactions may cause problems such as dirty reads, non-repeatable reads, and phantom reads. InnoDB implements four isolation levels in the SQL92 standard:
The default isolation level of InnoDB is RR, and the most commonly used isolation level is RC Summarize The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM. You may also be interested in:
|
<<: JS implements jQuery's append function
>>: How to deal with time zone issues in Docker
Go is an open source programming language that ma...
<br />Every family has its own problems, and...
background When developing a feature similar to c...
The <tfoot> tag is used to define the style...
Add table fields alter table table1 add transacto...
1|0MySQL (MariaDB) 1|11. Description MariaDB data...
MySQL implements sequence function 1. Create a se...
Before talking about OO, design patterns, and the ...
Let’s start with a question Five years ago when I...
<br />This is from the content of Web front-...
This article mainly introduces the sample code of...
CSS combination selectors include various combina...
If the words in the sql statement conflict with t...
React native implements the monitoring gesture to...
A simple Linux guessing game source code Game rul...