当前位置:网站首页>The principle and related problems of acid in MySQL
The principle and related problems of acid in MySQL
2022-07-01 00:22:00 【ordinaryBlog】
Business
The nature of transactions
Atomicity A
The whole database transaction is an inseparable work unit , Only when all database operations in the transaction are executed successfully , Business is a success
Uniformity C
Consistency refers to a transaction that moves a database from one state to the next , The transaction must be in a consistent state before and after execution
Isolation, I
Transaction isolation requires that the read / write objects of each transaction can be separated from the operation objects of other transactions , That is, it is invisible to other transactions before committing
persistence D
Once the transaction is committed, the result is permanent , Even in case of database downtime and other failures, the data can be recovered .
InnoDB How to achieve ACID
Atomicity
InnoDB utilize undo log( Rollback log ) To achieve atomicity of transactions , When the transaction is rolled back, all successfully executed SQL sentence .undo log It records the information before the data is modified, as well as the newly added and deleted data information .
such as :
- When deleting a piece of data , We need to record the information of this data , When rolling back , Add this adjustment information
- When adding a new piece of data , You need to record the primary key of this data , When rolling back , Delete information according to the primary key
- When modifying a piece of data , The old value of this data needs to be recorded , Perform modification during rollback
persistence
InnoDB Take advantage of redo log( Redo log ) To achieve transaction persistence
When a record is updated ,InnoDB Will first write the record to redo log Inside , And update memory ,InnoDB It will be brushed into the disk at the appropriate time . When the database is down and restarted , Will redo log The content in is restored to the database , According to undo log and bin log Content determines whether to roll back data or submit data .
Isolation,
MySQL Through locks and MVCC Mechanism to achieve isolation
Isolation level
Read uncommitted Allow read of uncommitted data changes , Can cause dirty reading 、 Phantom or unrepeatable reading . No lock will be added to the reader . And writing will add exclusive lock , And release after the transaction ends .
Read the submission Allow to read data submitted by concurrent transactions , Can prevent dirty reading , But phantom or unrepeatable reads can still occur . Will pass MVCC Get the current data newest snapshot , No locks , And ignore any locks
Repeatable Multiple reads of the same field are consistent , Unless the data is modified by the transaction itself , Can prevent dirty and unrepeatable read , But phantom reading can still happen . Only in a transaction for the first time ****select It's time to build the version , All subsequent queries are performed on this version , So that Repeatable .
Serialization All reads and writes to the same data become mutually exclusive .
Dirty reading : When a transaction is accessing data and accessing it Read uncommitted data
** It can't be read repeatedly :** Between two reads of data in the first transaction , Due to the modification of the second transaction, the data read by the first transaction is different .
** Fantasy reading :** Unreal reading is similar to nonrepeatable reading , The first transaction reads several rows of data , Then another transaction inserts some data , In the subsequent query, the first transaction will find some more records that do not exist , It's like an illusion , So it's called Unreal reading
The difference between nonrepeatable reading and unreal reading
The point of non repeatable reading is modify , For example, reading a record many times and finding that the values of some columns in it have been modified , The point of fantasy reading is Add or delete , For example, read a record many times and find that the number of records increases or decreases .
MVCC
MVCC It refers to multi version concurrency control ,MVCC When accessing the database concurrently , Through multi version control of data, the blocking of read operation caused by write lock can be avoided , So as to optimize the concurrent blocking problem .
adopt undo log And consistency view , adopt undo log To save multiple versions of data , Save the list of currently active transactions through the consistency view .
For example, a value 1 It is changed into 2、3、4
Transactions started at different times have different views , In view A、B、C in The recorded values can be 1,2,3, That is, a record can have multiple versions in the system , This is multi version concurrency control .
Long transaction hazard
Long transactions mean that there will be a very old transaction view in the system . Since these transactions may access any data in the database at any time , So before this transaction is committed , All possible rollback records in the database must be kept , This can lead to a lot of storage space
Uniformity
- From the database level Database through atomicity 、 Isolation, 、 persistence , To ensure consistency . Atomicity 、 Isolation, 、 Persistence is a means to achieve consistency , Consistency is the ultimate goal
- From the application level , Through the code to determine whether the database data is valid , Then decide whether to roll back or commit the data
边栏推荐
- Red Hat将在Project Atomic上运用容器负载服务器
- Sm2246en+ SanDisk 15131
- NATs cluster deployment
- Analysis of 8253a register
- 2022-2028 global ultra high purity electrolytic iron sheet industry research and trend analysis report
- Techo youth 2022 academic year college open class: behind the live broadcast of Lianmai, explore how to apply audio and video technology
- [PHP] self developed framework qphp, used by qphp framework
- Software engineering best practices - project requirements analysis
- Ybtoj exchange game [tree chain splitting, line segment tree merging]
- New trend of embedded software development: Devops
猜你喜欢

Bridge emqx cloud data to AWS IOT through the public network

SSM integration process (integration configuration, function module development, interface test)

2022-2028 global rampant travel industry research and trend analysis report

2022-2028 global capsule shell industry research and trend analysis report

5g smart building solution 2021

Ditto set global paste only text shortcuts

Wechat official account development (1) introduction to wechat official account

The full technology stack, full scene and full role cloud native series training was launched to help enterprises build a hard core cloud native technology team
![[NLP] [textcnn] text classification](/img/bb/c8fd9f1ed458a88a17b8d5c70d9ce2.png)
[NLP] [textcnn] text classification

ABAQUS 2022 latest edition - perfect realistic simulation solution
随机推荐
Redis - how to understand publishing and subscribing
Design e-commerce seckill system
CTF tool (1) -- archpr -- including installation / use process
2022-2028 global ultra high purity electrolytic iron sheet industry research and trend analysis report
2022-2028 global single travel industry research and trend analysis report
Analysis of 8253a register
2022-2028 global plant peptone industry research and trend analysis report
Yboj mesh sequence [Lagrange interpolation]
Rust controls Dajiang programmable UAV Tello
How to close an open DNS resolver
Is it safe to choose mobile phone for stock trading account opening in Hangzhou?
Query points in MATLAB Delaunay triangulation
CentOS 6.3 x64 PHP 5.2.6 扩展安装OpenSSL出错的解决方法
Maxpool2d explanation -- Application in arrays and images
CentOS installation starts redis
composer
Software engineering best practices - project requirements analysis
Excuse me, does Flink support synchronizing data to sqlserver
CentOS install MySQL
2022-2028 global electric yacht industry research and trend analysis report