当前位置:网站首页>Review the past and know the new MySQL isolation level
Review the past and know the new MySQL isolation level
2022-07-26 17:06:00 【Sommery】
A few days ago, I found a giant cow's AI learning website , Easy to understand , Humor and wit humor , I can't help but share it with you . Click to jump to the website .
List of articles
To understand isolation levels , First of all, we need to understand the affairs , Isolation level only works under transaction operation .
Business
The business is made up of a group of SQL A logical processing unit made up of statements , These operations are either all successful , All or nothing .
Transaction features
Atomicity (Atomicity): A transaction is an atomic unit of operation , Its modification of data , Or do it all , Or none of them .
Uniformity (Consistency): At the beginning and end of the transaction , Data must be consistent . This means that all relevant data rules must be applied to the modification of the transaction , To maintain the integrity of the data .
Isolation, (Isolation): The database system provides a certain isolation mechanism , Ensure that transactions are not affected by external concurrent operations “ Independent ” Environmental execution . This means that the intermediate state in the transaction process is not visible to the outside , vice versa .
persistence (Durability): After the transaction completes , Its modification of data is permanent , Even if there is a system failure, it can keep .
Problems caused by concurrent transaction processing
Update missing ( Dirty write ): When multiple transactions modify the same record , The last operation overwrites the previous transaction operation , As a result, the previous operation update was lost . This phenomenon is called Update missing .
Dirty reading : A transaction is modifying a record , Not submitted , When not locked, another transaction reads the uncommitted record and operates on it , This leads to inconsistent data . This phenomenon is called Dirty reading .
A friend of yours is transferring 10000 yuan to you , The transfer transaction is not over yet , You go to check the account balance and find that there is an extra 10000 , At this time, reading that the account balance is more than 10000 is dirty reading . If you spend this money , And this transaction is rolled back , There will be data inconsistencies .Fantasy reading : A transaction re reads the previously retrieved data according to the same query criteria , However, it is found that other transactions have inserted new data satisfying their query criteria , This phenomenon is called Fantasy reading .
Check the account balance , You went to check the bill again this month , At this time, you are in the transaction of checking the bill . When I checked it for the first time, I saw that there was only one income this month , Inexplicably, you checked this month's bill again and found an extra income of onemillion , The extra record of onemillion is unreal reading .Don't reread : A time after a transaction reads some data , Read the previously read data again , It is found that the read data has changed or some records have been deleted . This phenomenon is called It can't be read repeatedly .
Check the details of this onemillion bill , At this time, you are in the transaction of checking the bill details , I don't know who transferred it to you . At this time, the old man found that the transfer was wrong , Just call the bank to cancel the transfer . At the same time, when you want to check more information about this bill , It turns out that you don't have this bill , The bank withdrew . When you read the same record in the transaction of checking the details of the bill and get different results, you cannot read it repeatedly .
The loss of updates is a writing problem , Dirty reading 、 Fantasy reading 、 Unrepeatable reading is the problem of reading .
Isolation level
The isolation level is when multiple transactions make changes and execute queries at the same time , Fine tuning performance and reliability of results 、 Setting of a balance between consistency and reproducibility .
The isolation level consistency and protection are from low to high :
- Read uncommitted (Read Uncommitted)
- Read submitted (Read committed)
- Repeatable (Repeatable Read)
- Serialization (SERIALIZABLE)
Read uncommitted (Read Uncommitted)
Read uncommitted is a transaction that modifies the data , But did not submit , Other transactions can read the uncommitted data , This time there will be dirty reading . This level is rarely used .

Business B Read to transaction A Uncommitted data , This causes dirty reading .
Read submitted (Read committed)
A transaction reads data that has been modified and committed by other transactions , Whenever other transactions commit modified data , Can be read by the current transaction . Read has been submitted to solve the dirty read problem , Phantom reading can occur 、 It can't be read repeatedly .

Business A After submission , Business B To find out a Value
Repeatable (Repeatable Read)
MySQL InnoDB The default isolation level of the engine . Business T1 When a record is first read , Even if other transactions have modified the record , Business T1 What I read is still the record I read for the first time , Instead of reading different values every time , This is repeatable reading . Repeatable reading solves dirty reading 、 It can't be read repeatedly , There will still be unreal reading ,MySQL adopt MVCC( Multi version concurrency control ) To some extent, it solves the problem of unreal reading , of MVCC The next article will introduce .
Repeatable read will generate a snapshot when the same transaction is queried for the first time , When the same condition is queried again in this transaction , The data queried is actually the data in the snapshot , In order to achieve repeatable reading .

Business A And transaction B All available a=1, Business A take a Is changed to 2, Then commit the transaction , Although a The value of is already 2 了 , But the business B Obtained before submission a Still 1, This is repeatable reading .
Serialization (SERIALIZABLE)
Highest isolation level , It forces transactions to execute serially . It is rarely used in practice , Only when there is a strong need to ensure data consistency and it is acceptable that there is no concurrency , Only consider .
When serializing transactions, other transactions are not allowed to execute concurrently . Read completely serially , No writing as long as there is reading , But at the same time , It eliminates unreal reading .
The higher the transaction isolation level , The integrity and consistency of data are also higher , But the less concurrency , The greater the impact on the access speed of the system . Different applications may have different requirements for the isolation level of transactions , If the application is not sensitive to unreal reading , Is to use the current MySQL The default isolation level is ok .
Check the isolation level
1、 Check the system isolation level
show variables like 'transaction_isolation';
perhaps
select @@global.tx_isolation;
2、 View session isolation level
select @@tx_isolation;
Set isolation level
1、 Global isolation level setting
set global transaction isolation level Isolation level ;
-- Set the isolation level to repeatable read
set global transaction isolation level REPEATABLE READ;
2、 Session isolation level setting
set session transaction isolation level Isolation level ;
set session transaction isolation level REPEATABLE READ;
Average ability , Level co., LTD. , If there is a mistake , Please point out more .
If the article is useful to you, pay attention to it and give it a compliment
More articles can be concerned about my WeChat official account. suncodernote
边栏推荐
- How to implement Devops with automation tools | including low code and Devops application practice
- 别用Xshell了,试试这个更现代的终端连接工具
- Difference between C event and delegation
- Current limiting comparison: how to choose sentinel vs hystrix?
- Stop using xshell and try this more modern terminal connection tool
- Win11 auto delete file setting method
- 【Express接收Get、Post、路由请求参数】
- 40个高质量信息管理专业毕设项目分享【源码+论文】(六)
- Tensorflow Lite source code analysis
- 限流对比:Sentinel vs Hystrix 到底怎么选?
猜你喜欢

Win11自动删除文件设置方法

Tcpdump命令详解

How emqx 5.0 under the new architecture of mria+rlog realizes 100million mqtt connections

2022 software testing skills postman+newman+jenkins continuous integration practical tutorial

Marxan model, reserve optimization and protection vacancy selection technology, application in invest ecosystem
![37. [categories of overloaded operators]](/img/67/b821270079589c53b9c38b0ca033ac.png)
37. [categories of overloaded operators]

Win11如何关闭共享文件夹

第一章概述-------第一节--1.3互联网的组成
![[fluent -- advanced] packaging](/img/aa/bd6ecad52cbe4a34db75f067aa4dfe.png)
[fluent -- advanced] packaging
![[express receives get, post, and route request parameters]](/img/6c/ac936a8dff50b803993bef5192723b.png)
[express receives get, post, and route request parameters]
随机推荐
Win11自动删除文件设置方法
Use verdaccio to build your own NPM private library
公安部发出暑期旅游客运交通安全预警:手握方向盘 绷紧安全弦
TCP 和 UDP 可以使用相同端口吗?
The Ministry of Public Security issued a traffic safety warning for summer tourism passenger transport: hold the steering wheel and tighten the safety string
Definition and relationship of derivative, differential, partial derivative, total derivative, directional derivative and gradient
movable-view 组件(可上下左右拖动 )
Marxan model, reserve optimization and protection vacancy selection technology, application in invest ecosystem
[Luogu cf643f] bears and juice (conclusion)
NUC 11 build esxi 7.0.3f install network card driver-v2 (upgraded version in July 2022)
Thoroughly uncover how epoll realizes IO multiplexing
操作系统迁移实战之在openEuler上部署MySQL数据库
C#读取本地文件夹中所有文件文本内容的方法
2022-2023 topic recommendation of information management graduation project
Quickly build a development platform for enterprise applications
Quickly learn to configure local and network sources of yum, and learn to use yum
PXE高效批量网络装机
【飞控开发基础教程2】疯壳·开源编队无人机-定时器(LED 航情灯、指示灯闪烁)
After Oracle creates a table partition, the partition is not given during the query, but the value specified for the partition field will be automatically queried according to the partition?
Digital intelligence transformation, management first | jnpf strives to build a "full life cycle management" platform