1 Problems encountered in transaction concurrency1.1 Dirty ReadWhen a transaction reads data that has been modified but not committed by another transaction, it is called a dirty read. 1.2 Non-repeatable readWhen the same record is retrieved twice within a transaction and the results obtained twice are different, this phenomenon is called a non-repeatable read. 1.3 Phantom ReadsWhen a transaction queries twice (or multiple times) with the same query condition and the number of entries found is inconsistent, it is called phantom read. 2 Isolation LevelsWe have introduced several problems that may be encountered during the execution of concurrent transactions. These problems also have different priorities. We rank these problems according to their severity: Dirty read > Non-repeatable read > Phantom read The SQL standard stipulates that for different isolation levels, concurrent transactions can have different degrees of severity. The specific situation is as follows:
The SQL standard stipulates that for different isolation levels, concurrent transactions can have problems of varying severity, as follows:
3 Version ChainWe know that for a table using the InnoDB storage engine, its clustered index record contains two necessary hidden columns (row_id is not necessary, and the row_id column will not be included when the table we create has a primary key or a non-NULL UNIQUE key): Assuming that the transaction ID of the record inserted is 80, the schematic diagram of the record at this moment is as follows: Assume that two transactions with transaction IDs 100 and 200 perform UPDATE operations on this record. The operation flow is as follows: TRX 100: UPDATE t_people SET name = 'Guan Yu' WHERE number = 1; UPDATE t_people SET name = '张飞' WHERE number = 1; TRX 200: UPDATE t_people SET name = 'Zhao Yun' WHERE number = 1; UPDATE t_people SET name = 'Zhuge Liang' WHERE number = 1; Every time a record is modified, an undo log is recorded. Each undo log also has a roll_pointer attribute (the undo log corresponding to the INSERT operation does not have this attribute because the record has no earlier version). These undo logs can be connected to form a linked list, so the current situation is like the following figure: After each update to the record, the old value will be put into an undo log, even if it is an old version of the record. As the number of updates increases, all versions will be connected into a linked list by the roll_pointer attribute. We call this linked list a version chain, and the head node of the version chain is the latest value of the current record. In addition, each version also contains the transaction ID corresponding to the generation of the version. The version chain of this record can then be used to control the behavior of concurrent transactions accessing the same record. This mechanism is called 4 ReadView4.1 ReadView DefinitionInnoDB proposes a concept of ReadView, which mainly includes four important contents:
4.2 Access ControlWith this ReadView, when accessing a record, you only need to follow the steps below to determine whether a version of the record is visible:
4.3 Revisiting IsolationFor transactions using the READ UNCOMMITTED isolation level, since you can read records modified by uncommitted transactions, you can just read the latest version of the records directly. For transactions using the SERIALIZABLE isolation level, InnoDB uses locking to access records. In MySQL, a very big difference between READ COMMITTED and REPEATABLE READ isolation levels is the timing of their generation of ReadView. 4.3.1 READ COMMITTEDRead Submitted, a ReadView is generated before each data read. Suppose now a transaction using the READ COMMITTED isolation level starts executing: Detailed query: #Transaction using READ COMMITTED isolation level #Transaction 100 and 200 are not committed, and the value of the column name is Liu BeiSELECT name FROM t_people WHERE number = 1; The execution process of this SELECET is as follows:
Non-repeatable read: When transactions 100 and 200 are started, the name read is Liu Bei. When transaction 100 is committed, a ReadView is created for each read because the transaction isolation level is read committed. When transaction 200 is read, the generated ReadView m_ids is [200]. At this time, the name read according to the read rule is Zhang Fei. # Use transaction BEGIN with READ COMMITTED isolation level; # SELECE1: Transaction 100 and 200 are not submitted, and the name value is Liu Bei SELECT name FROM t_people WHERE number = 1; # SELECE2: Transaction 100 is committed, Transaction 200 is not committed #Transaction 200 transaction query, the name value is Zhang Fei, and a non-repeatable read occurs. SELECT name FROM teacher WHERE number = 1; 4.3.2 REPEATABLE READRe-readable, generates a ReadView when reading data for the first time. Solve the problem of non-repeatable read: 100 transactions and 200 transactions are enabled, and ReadView is created. m_ids is [100,200], and the name read is Liu Bei. When transaction 100 is committed, due to the re-read transaction isolation level, only one ReadView is created, and m_ids is still [100,200]. At this time, the name read according to the read rule is still Liu Bei. 5 Phantom ReadsWhen a transaction queries twice (or multiple times) with the same query condition and the number of entries found is inconsistent, it is called phantom read. Transaction T1 at the REPEATABLE READ isolation level first reads multiple records based on a search condition, then transaction T2 inserts a record that meets the corresponding search condition and commits it, and then transaction T1 executes a query based on the same search condition. What will be the result? According to the comparison rules in ReadView, regardless of whether transaction T2 is started before transaction T1, transaction T1 cannot see the commit of T2. However, MVCC in InnoDB under the REPEATABLE READ isolation level can largely avoid phantom reads instead of completely prohibiting phantom reads. #SELECT: Snapshot read. update: currently read. REPEATABLE READ can solve the snapshot read phantom read problem. The current phantom read problem cannot be solved. Examples: 1 Execute begin and execute select *. 2 Open another window to execute insert, select, and commit. 3 Return to the original window to execute the query 4 Execute update and submit 5 Search 6 ConclusionFrom the above description, we can see that the so-called MVCC (Multi-Version Concurrency Control) refers to the process of accessing the version chain of records when executing ordinary SELECT operations using transactions at the two isolation levels of READ COMMITTD and REPEATABLE READ. This allows read-write and write-read operations of different transactions to be executed concurrently, thereby improving system performance. A big difference between the two isolation levels READ COMMITTD and REPEATABLE READ is the timing of generating ReadView. READ COMMITTD generates a ReadView before each normal SELECT operation, while REPEATABLE READ only generates a ReadView before the first normal SELECT operation, and subsequent query operations reuse this ReadView, which can basically avoid phantom reads. The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Detailed explanation of various HTTP return status codes
>>: Code for implementing simple arrow icon using div+CSS in HTML
HTML implements 2-column layout, with fixed width...
Regarding the nginx panic problem, we first need ...
I haven’t updated my blog for several days. I jus...
Table of contents Preface Reference Comparison Ma...
Passive Check With passive health checks, NGINX a...
As the demand for front-end pages continues to in...
1. There are many Python version management tools...
MySQL software installation and database basics a...
I will be learning MySQL next semester. I didn...
The GtkTreeView component is an advanced componen...
The <marquee> tag is a tag that appears in ...
This article mainly introduces the detailed proce...
This article example shares the specific code of ...
If the words in the sql statement conflict with t...
1. Set firewall rules Example 1: Expose port 8080...