当前位置:网站首页>MySQL transaction isolation level
MySQL transaction isolation level
2022-06-23 07:16:00 【summer_ west_ fish】
One 、ACID attribute
- Atomicity (Atomicity): A transaction is an atomic operation , Or all of them can be executed successfully , Or the execution fails together
- Uniformity (Consistent): The beginning and end of a transaction , The data must be consistent
- Isolation, (Isolation): In the process of transaction processing, it is necessary to ensure mutual isolation 、 Independent
- persistence (Durale): When the business is over , Modifications to the data are permanently saved
Two 、 Problems caused by concurrent transaction processing
Update missing (Lost Update) or Dirty write : Multiple transactions without knowing that other transactions exist , At the same time, modify the data , Dirty writing will appear .
The last update covers the updates made by other firms
Dirty reading (Dirty Reads): When a transaction reads a record and modifies others , But it hasn't been submitted yet , At this point, another transaction also reads , If not controlled , The data read by the second transaction is dirty data .
Business A Read to transaction B Data that has been modified but not yet submitted
Non repeatability (Non-Repeatable Reads): Some time after a transaction reads some data , Read the previously read data again , But found that the data it read out has changed .
Business A The results of the same internal query statement read at different times are inconsistent , Nonconformance isolation
Fantasy reading (Phantom Reads): A transaction reads previously retrieved data according to the same query criteria , However, it is found that other transactions have inserted new data satisfying their query criteria .
Business A Read the business B New data submitted , Nonconformance isolation
3、 ... and 、 Isolation level
Dirty reading 、 It can't be read repeatedly 、 Fantasy reading , In fact, it's all about database read consistency , The database must provide some transaction isolation mechanism to solve , and MySQL The default is repeatable reading .
| Isolation level | Dirty reading | It can't be read repeatedly | Fantasy reading |
| Read uncommitted | YES | YES | YES |
| Read submitted | NO | YES | YES |
| Repeatable | NO | NO | YES |
| Serialization | NO | NO | NO |
Read uncommitted : Business A Updated data ( Has not been submitted ), And the business B But you can find the transaction A Updated data
Read submitted : Business A Updated data ( Has not been submitted ), Business B Cannot query transaction A Updated data ( Solved the problem of dirty reading )
Repeatable degrees : Business A Checked the records ( If 400), Business B Changed data (400 - 50 = 350, then commit), Business A The value read again before committing the transaction is still 400, Until transaction A The latest updated data can only be read after the transaction is committed ( It ensures the isolation between transactions )
Serialization : Data with row lock , Other transactions cannot be added, deleted, modified or queried , It's like an exclusive lock
Four 、 Locking mechanism
A lock is a computer that coordinates multiple processes / The mechanism by which threads access a resource concurrently , Lock classification :
From the performance of the database
- Optimism lock ( Version number comparison )
- Pessimistic locking
From the type of database operation
- Read the lock ( Shared lock , Multiple read operations can be performed simultaneously without affecting each other )
- Write lock ( Exclusive lock , Before the operation is completed , Nothing else can be done , Ensure serial connection )
Granularity of database operations from heap
- Table locks
- Row lock
Table locks
Lock the whole table with each operation , Low overhead , Locked fast , A deadlock will not occur , Big lock size , The probability of lock conflict is higher , Concurrent reads are the lowest , Generally used in data migration scenarios .
Row lock
Lock one row of data per operation , Spending big , Lock the slow , A deadlock occurs , The lock granularity is the smallest , The lowest probability of lock collisions ,InnoDB Support transactions and row locks , however MylSAM I won't support it .
One session Enable transaction updates without committing , Another one session Updating the same record will block , Updating different records will not block .
Clearance lock
Lock is the gap between two values .
Gap lock can solve the unreal reading problem in some cases
The gap lock will only take effect at the repeatable read isolation level
update account set name = '100' where id > 8 and id < 18;
other Session It is impossible to lock all the row records contained in this range and the gap row records within this range
Insert or modify any data in the gap of the original table . namely id stay (3,20] Data cannot be modified in any interval , Be careful
The last one 20 Also included .for example SQL: : There is a gap id by (3,10),(10,20),(20, It's just infinite ) These three intervals
Temporary lock
Combination of row lock and clearance lock
The key lock is mainly female in order to avoid unreal reading , If the transaction is downgraded to read committed , The keyless lock will fail .
for example : Example of clearance lock above SQL, Gap is (3,20] The whole range
Lockless pilot lock will be upgraded to table lock
The lock is mainly added to the index , If the non lock field is updated , Row locks may become table locks
for example : If now there will be no index char Change the type field to another varchar type , Because there is no index , The full table will be scanned , Finally upgrade to ' Table locks '
Lock optimization suggestions
- Try to make all data retrieval complete through index , Avoid upgrading non indexed row locks to table locks
- Design index reasonably , Minimize the scope of the lock
- Minimize the range of search conditions , Avoid gap locks
- Try to control the transaction size , Reduce the amount of locked resources and the length of time , Involving transaction locking SQL Try to put it at the end of the transaction
- As low level transaction isolation as possible
边栏推荐
猜你喜欢

C language learning summary

Analysis of personalized learning progress in maker Education

407 stack and queue (232. implementing queue with stack, 225. implementing stack with queue)

407-栈与队列(232.用栈实现队列、225. 用队列实现栈)

Nacos适配oracle11g-修改Nacos源码

How to migrate virtual machines from VirtualBox to hype-v

Initialization layer implementation

MySQL Niuke brush questions

JUnit unit test reports an error org junit. runners. model. InvalidTestClassError: Invalid test class ‘xxx‘ . No runnable meth
![[system] right click the desktop icon. After turning around, the Explorer will crash and the desktop will be refreshed](/img/aa/0189beb065fa0d4b625390793cb79b.png)
[system] right click the desktop icon. After turning around, the Explorer will crash and the desktop will be refreshed
随机推荐
NTU-RGBD数据集下载及数据格式解析
How to verify date format in PHP (regular)
Tp6 installation expansion
In depth learning series 47:stylegan summary
闫氏DP分析法
npm下载报错npm ERR code ERESOLVE
303. 区域和检索 - 数组不可变
Redis设置密码
322. 零钱兑换
318. maximum word length product
对二进制的某一位操作
901. stock price span
Spock-sub打桩
Deeplab V3 code structure diagram
codeforce 158B Taxi
产品-Axure9(英文版),原型设计 制作下拉二级菜单
MySQL(四) — MySQL存储引擎
MySQL mvcc multi version concurrency control
【博弈论】基础知识
Regular expression graph and text ultra detailed summary without rote memorization (Part 1)