当前位置:网站首页>Review the past and know the new MySQL isolation level
Review the past and know the new MySQL isolation level
2022-07-28 08:57: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 .
*
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
边栏推荐
- Vrrp+mstp configuration details [Huawei ENSP experiment]
- 博客搭建九:hugo添加搜索功能
- Why setting application.targetframerate doesn't work
- Completion report of communication software development and Application
- SQL注入 ----前置基础
- How CI framework integrates Smarty templates
- Shell programming specifications and variables
- Blog Building 9: add search function to Hugo
- After summarizing more than 800 kubectl aliases, I'm no longer afraid that I can't remember commands!
- Chapter 2-14 sum integer segments
猜你喜欢
![Chapter 2-2 calculation of piecewise function [1]](/img/40/cad6bf92849624199af0fd1ba1d433.jpg)
Chapter 2-2 calculation of piecewise function [1]

Solution: indexerror: index 13 is out of bounds for dimension 0 with size 13

HCIP第八天
![Detailed explanation of MSTP protocol for layer 3 switch configuration [Huawei ENSP experiment]](/img/97/6c3662ef36b02bc42eec95abaa6bc5.png)
Detailed explanation of MSTP protocol for layer 3 switch configuration [Huawei ENSP experiment]

Uniapp ---- detailed steps to obtain the longitude and latitude of the current position and other information (including applet)

Smart software completed round C financing, making Bi truly "inclusive"
![Vrrp+mstp configuration details [Huawei ENSP experiment]](/img/44/987e9ff87f5c8e2a2ab493e947f884.png)
Vrrp+mstp configuration details [Huawei ENSP experiment]

ciou损失

我来教你如何组装一个注册中心?

博客搭建九:hugo添加搜索功能
随机推荐
看完这12个面试问题,新媒体运营岗位就是你的了
Basic syntax of jquey
象棋机器人夹伤7岁男孩手指,软件测试工程师的锅?我笑了。。。
Redis basic knowledge, let's review it
Alibaba internal interview materials
阿里技术四面+交叉面+HR面,成功拿到offer,双非本科进不了大厂?
Wechat applet - wechat applet browsing PDF files
思迈特软件完成C轮融资,让BI真正实现“普惠化”
Pyspark writes data to iceberg
解决:IndexError: index 13 is out of bounds for dimension 0 with size 13
Competition: diabetes genetic risk detection challenge (iFLYTEK)
How to import and export Youxuan database
博客搭建九:hugo添加搜索功能
After reading these 12 interview questions, the new media operation post is yours
NDK 系列(6):说一下注册 JNI 函数的方式和时机
阿里巴巴内部面试资料
I am a 27 year old technical manager, whose income is too high, and my heart is in a panic
Vrrp+mstp configuration details [Huawei ENSP experiment]
PostgreSQL: cannot change the type of column used by a view or rule
You're not still using xshell, are you? This open source terminal tool is yyds!