当前位置:网站首页>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
边栏推荐
- Can SQL execution be written in tidb dashboard
- Sm2246en+ SanDisk 15131
- What does it mean to open an account online? Is it safe to open an account online?
- IFLYTEK active competition summary! (12)
- Lombok
- Wordpress blog uses volcano engine veimagex for static resource CDN acceleration (free)
- 五分钟搞懂探索式测试
- Design e-commerce seckill system
- 5g smart building solution 2021
- MySQL index test
猜你喜欢

Basic knowledge of Embedded Network - introduction of mqtt

Software supply chain security risk pointing North for enterprise digitalization and it executives

Analysis of 8253a register

How does the VR cloud exhibition hall bring vitality to offline entities? What are the functions?

Redis - understand the master-slave replication mechanism

Redis - how to understand publishing and subscribing

2022-2028 global public address fire alarm system industry research and trend analysis report

CentOS installation starts redis

2022-2028 global PTFE lined valve industry research and trend analysis report

ABAQUS 2022 software installation package and installation tutorial
随机推荐
8253A寄存器浅析
女朋友说:你要搞懂了MySQL三大日志,我就让你嘿嘿嘿!
Rhai - rust's embedded scripting engine
Vulnerability discovery - App application vulnerability probe type utilization and repair
Never use redis expired monitoring to implement scheduled tasks!
leetcode 474. Ones and zeroes (medium)
Detailed explanation of conv2d -- use in arrays and images
2022-2028 global public address fire alarm system industry research and trend analysis report
CentOS install MySQL
2022-2028 global ultra high purity electrolytic iron powder industry research and trend analysis report
2022-2028 global PTFE lined valve industry research and trend analysis report
Bridge emqx cloud data to AWS IOT through the public network
20220215-ctf-misc-buuctf-ningen--binwalk analysis --dd command separation --archpr brute force cracking
Netease cloud sign in lottery? That year I could sign in for 365 days. No? Look.
Solving the weird problem that the query conditions affect the value of query fields in MySQL query
Understand target detection in one article: r-cnn, fast r-cnn, fast r-cnn, Yolo, SSD "suggestions collection"
The college entrance examination in 2022 is over. Does anyone really think programmers don't need to study after work?
Vmware16 installing win11 virtual machine (the most complete step + stepping on the pit)
1175. prime number arrangement / Sword finger offer II 104 Number of permutations
五分钟搞懂探索式测试