当前位置:网站首页>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
边栏推荐
- 2022-2028 global retro glass industry research and trend analysis report
- SSM integration process (integration configuration, function module development, interface test)
- [UML] UML class diagram
- Basic knowledge of Embedded Network - introduction of mqtt
- Shell multitasking to download video at the same time
- 深入理解 Jetpack Compose 内核:SlotTable 系统
- The programmer's girlfriend gave me a fatigue driving test
- 1175. 质数排列 / 剑指 Offer II 104. 排列的数目
- [PHP] self developed framework qphp, used by qphp framework
- 2022-2028 global mobile scanning radiology room industry survey and trend analysis report
猜你喜欢

Combining online and offline, VR panorama is a good way to transform furniture online!

2022-2028 global 3D printing ASA consumables industry research and trend analysis report

MySQL variables, stored procedures and functions

女朋友说:你要搞懂了MySQL三大日志,我就让你嘿嘿嘿!

The college entrance examination in 2022 is over. Does anyone really think programmers don't need to study after work?

20220215 CTF misc buuctf Xiaoming's safe binwalk analysis DD command separate rar file archpr brute force password cracking

Quick start of wechat applet -- project introduction

2022-2028 global ethylene oxide scrubber industry research and trend analysis report

Redis - understand the master-slave replication mechanism

2022-2028 global carbon fiber room scraper system industry research and trend analysis report
随机推荐
Explain kubernetes backup and recovery tools velero | learn more about carina series phase III
Query points in MATLAB Delaunay triangulation
[NLP] [textcnn] text classification
The programmer's girlfriend gave me a fatigue driving test
高等数学建模
Wechat official account development (1) introduction to wechat official account
MySQL variables, stored procedures and functions
[designmode] factory pattern
CTF tool (1) -- archpr -- including installation / use process
让企业数字化砸锅和IT主管背锅的软件供应链安全风险指北
2022-2028 global rotary transmission system industry research and trend analysis report
Software supply chain security risk pointing North for enterprise digitalization and it executives
如何关闭一个开放的DNS解析器
[leetcode] [SQL] notes
2022-2028 global ultra high purity electrolytic iron powder industry research and trend analysis report
leetcode 474. Ones and zeroes (medium)
To tell you the truth, ThreadLocal is really not an advanced thing
PS2 handle-1 "recommended collection"
What is SRM system and how to standardize the internal procurement process of the company
2022-2028 global elevator emergency communication system industry research and trend analysis report