当前位置:网站首页>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
边栏推荐
猜你喜欢

GINet

20220621 Three Conjugates of Dual Quaternions

Configuration and compilation of mingw-w64, msys and ffmpeg

excel高级绘图技巧100讲(八)-Excel绘制WIFI图

Quartz调度框架的学习使用

Pagoda forgot password

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

Flannel 工作原理

初始化层实现

GIS实战应用案例100篇(七十九)-多规整合底图的制作要点
随机推荐
306. Addenda
MySQL mvcc multi version concurrency control
Deep learning series 47: Super sub model real esrgan
100 GIS practical application cases (79) - key points of making multi plan integrated base map
313. super ugly number
【***数组***】
897. 递增顺序搜索树
【AI实战】xgb.XGBRegressor之多回归MultiOutputRegressor调参1
306. 累加数
20220621 Dual Quaternion
295. median data flow
In depth learning series 47:stylegan summary
Xshell7 Download
Vs2013 ffmpeg environment configuration and common error handling
[AI practice] xgb Xgbregression multioutputregressor parameter 1
滚动播报效果的实现
数据库原理实验测试题,关于图书分类表
ldconfig 命令
[system] right click the desktop icon. After turning around, the Explorer will crash and the desktop will be refreshed
Spock约束-调用频率/目标/方法参数