


An article briefly analyzing how MySQL solves the phantom reading problem
How does MySQL solve the phantom read problem? The following article will let you talk about this issue. Let’s read the article with questions!
Transaction characteristics (ACID): Atomicity (
Atomicity),
Isolation(
Isolation),
Consistency(
Consistency) and
Persistence
Isolation Level: READ UNCOMMITTED(
READ UNCOMMITTED),
READ COMMITTED(
READ COMMITTED),
Repeatable Read (
REPEATABLE READ),
Serializable (
SERIALIZABLE)
- READ UNCOMMITTED
Under the isolation level,
dirty reads,
non-repeatable readsand ## may occur. #phantom read
problem READ COMMITTED - Under the isolation level,
non-repeatable read
andphantom read
problems may occur, but they are not possibleDirty read
problem REPEATABLE READ - Under the isolation level,
phantom read
problem may occur, butdirty read# cannot occur ## and
Non-repeatable readproblems
SERIALIZABLE
Under the isolation level, various problems cannot occur -
For MySQL InnoDB The default isolation level supported by the storage engine is
REPEATABLE-READ (repeatable)
REPEATABLE-READ (repeatable) It is impossible to prevent phantom reads, but we all know that the MySQL InnoDB storage engine solves the problem of phantom reads, so how does it solve it?
1. Row format
Before entering the topic, we first have a general understanding of what the row format is. This will help us understand the following MVCC. The row format is The way row records in the table are stored on disk, Innodb
storage engine has a total of 4 different types of row formats:compact,
redundant,
dynamic ,
compress; Although there are many line formats, they are basically the same in principle, as follows, the
compact line format:
As can be seen from the figure, A complete record can actually be divided into two parts: recorded additional information
and recorded real data
.
recorded additional information are respectively
changes. Long field length list ,
NULL value list and
record header information , and
recorded real data In addition to our own defined columns, MySQL will Add some default columns to each record. These default columns are also called
hidden columns. The specific columns are as follows:
Description | ||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Row ID, uniquely identifies one Record | transaction_id | |||||||||||||||||||||||||||||||||||||||||||||
Transaction ID | roll_pointer | |||||||||||||||||||||||||||||||||||||||||||||
Rollback pointer |
Time number | trx 100 | trx 200 | |
---|---|---|---|
① | BEGIN; | ||
| BEGIN;BEGIN; | ||
##update person set grade =40 where id =1; |
|||
##⑥ |
|||
##COMMIT; | ⑦ |
||
##update person set grade =70 where id =1; |
⑧ |
SELECT * FROM person WHERE id = 1; | |
COMMIT; |
? |
COMMIT; | |
trx 100 the transaction was executed Submit, the version chain recorded in the id=1 line is as follows: |
, the transaction commit was executed, the version chain recorded in the id=1 line As follows:
At time ⑤, transaction
trx 100
will first generate a
select statement. #, the content of the
list of ReadView
is [100, 200]
, min_trx_id
is 100
,max_trx_id
is 201
, creator_trx_id
is 0
, at this time, select the visible record from the version chain, and traverse the version chain from top to bottom: Because grade=40, the value of trx_id
is 100
, which is in m_ids
, so the record is not visible. Similarly, the record with grade=20 is also invisible. Continue traversing down, grade=20, trx_id
value is 80
, which is less than min_trx_id
value 100# in
ReadView ##, so this version meets the requirements, and records with level 10 are returned to the user.
In time ⑧, if the isolation level of the transaction is
READ COMMITTED, a separate
ReadView will be generated, the ## of the
ReadView The content of the #m_ids
list is
, min_trx_id
is 200
, max_trx_id
is 201
, creator_trx_id
is 0
. At this time, select the visible record from the version chain, and the version chain is traversed from top to bottom: because grade=70, the value of trx_id
is 200
, in m_ids
, so the record is not visible, continue to traverse, grade=40, trx_id
value is 100
, It is less than the min_trx_id
value 200
in ReadView
, so this version meets the requirements, and a record with level 40 is returned to the user. In time ⑧, if the isolation level of the transaction is
REPEATABLE READ
, in time ⑧, a ReadView
will not be generated separately, but the one of time 5 will be used. ReadView
, so the level returned to the user is 10. The result of the two selects is the same. This is the meaning of repeatable reading
.
3. Summary
By analyzing the detailed explanation of MVCC, it can be concluded that based on MVCC, under the RR isolation level, it is very easy to solve
phantom reading
Problem, but we know that select for update
generates current reads and is no longer snapshot reads. In this case, how does MySQL solve the
problem? Based on time issues (it does take a lot of time to organize and draw pictures), I will give the conclusion first, and then analyze how MySQL solves the phantom reading problem under the current reading situation:
Current reading
: Use Next-Key Lock (gap lock) to lock to ensure that phantom reading does not occur
How gap lock is used in the current reading situation If you want to solve the problem of phantom reading, interested friends can add a follow and like
[Related recommendations:
- ]
The above is the detailed content of An article briefly analyzing how MySQL solves the phantom reading problem. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.

To safely and thoroughly uninstall MySQL and clean all residual files, follow the following steps: 1. Stop MySQL service; 2. Uninstall MySQL packages; 3. Clean configuration files and data directories; 4. Verify that the uninstallation is thorough.

Efficient methods for batch inserting data in MySQL include: 1. Using INSERTINTO...VALUES syntax, 2. Using LOADDATAINFILE command, 3. Using transaction processing, 4. Adjust batch size, 5. Disable indexing, 6. Using INSERTIGNORE or INSERT...ONDUPLICATEKEYUPDATE, these methods can significantly improve database operation efficiency.

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

Methods for configuring character sets and collations in MySQL include: 1. Setting the character sets and collations at the server level: SETNAMES'utf8'; SETCHARACTERSETutf8; SETCOLLATION_CONNECTION='utf8_general_ci'; 2. Create a database that uses specific character sets and collations: CREATEDATABASEexample_dbCHARACTERSETutf8COLLATEutf8_general_ci; 3. Specify character sets and collations when creating a table: CREATETABLEexample_table(idINT

Use the EXPLAIN command to analyze the execution plan of MySQL queries. 1. The EXPLAIN command displays the execution plan of the query to help find performance bottlenecks. 2. The execution plan includes fields such as id, select_type, table, type, possible_keys, key, key_len, ref, rows and Extra. 3. According to the execution plan, you can optimize queries by adding indexes, avoiding full table scans, optimizing JOIN operations, and using overlay indexes.

Installing MySQL on macOS can be achieved through the following steps: 1. Install Homebrew, using the command /bin/bash-c"$(curl-fsSLhttps://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)". 2. Update Homebrew and use brewupdate. 3. Install MySQL and use brewinstallmysql. 4. Start MySQL service and use brewservicesstartmysql. After installation, you can use mysql-u

The reasons why MySQL is widely used in various projects include: 1. High performance and scalability, supporting multiple storage engines; 2. Easy to use and maintain, simple configuration and rich tools; 3. Rich ecosystem, attracting a large number of community and third-party tool support; 4. Cross-platform support, suitable for multiple operating systems.
