


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, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

MySQL and phpMyAdmin can be effectively managed through the following steps: 1. Create and delete database: Just click in phpMyAdmin to complete. 2. Manage tables: You can create tables, modify structures, and add indexes. 3. Data operation: Supports inserting, updating, deleting data and executing SQL queries. 4. Import and export data: Supports SQL, CSV, XML and other formats. 5. Optimization and monitoring: Use the OPTIMIZETABLE command to optimize tables and use query analyzers and monitoring tools to solve performance problems.

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

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.

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.

HTML5 brings five key improvements: 1. Semantic tags improve code clarity and SEO effects; 2. Multimedia support simplifies video and audio embedding; 3. Form enhancement simplifies verification; 4. Offline and local storage improves user experience; 5. Canvas and graphics functions enhance the visualization of web pages.

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.
