当前位置:网站首页>Learn about MySQL transaction isolation level
Learn about MySQL transaction isolation level
2022-07-05 17:14:00 【1024 questions】
Preface
1. Four transaction isolation levels
1.1 READ UNCOMMITTED
1.2 READ COMMITTED
1.3 REPEATABLE READ
1.4 SERIALIZABLE
1.5 Summary
2. Problems in concurrent transactions
2.1 Dirty reading
2.2 It can't be read repeatedly
2.3 Fantasy reading
3. Isolation level actual combat
3.1 Query transaction isolation level
3.2 Set the transaction isolation level
3.3 Dirty reading problem
3.4 Unrepeatable read problem
3.5 The problem of unreal reading
summary
PrefaceMySQL The transaction isolation level is used to solve the problem that concurrent transactions interfere with each other ,MySQL The total transaction isolation levels are as follows 4 Kind of :
READ UNCOMMITTED: Read uncommitted .
READ COMMITTED: Read submitted .
REPEATABLE READ: Repeatable .
SERIALIZABLE: serialize .
1. Four transaction isolation levels 1.1 READ UNCOMMITTEDRead uncommitted , Also known as uncommitted reads , Transactions at this isolation level can see uncommitted data in other transactions . This isolation level is because uncommitted data in other transactions can be read , Uncommitted data may be rolled back , Therefore, we call the data read at this level dirty data , Call this problem dirty reading .
1.2 READ COMMITTEDRead submitted , Also called submit read , Transactions at this isolation level can read the data of committed transactions , So it won't have dirty reading problems . However, the results submitted by other transactions can be read during the execution of transactions , So the same at different times SQL Querying , It's possible to get different results , This phenomenon is called unrepeatable reading .
1.3 REPEATABLE READRepeatable ,MySQL Default transaction isolation level . Repeatable reading can solve “ It can't be read repeatedly ” The problem of , But there is still the problem of unreal reading . The so-called unreal reading refers to , Use the same... At different times in the same transaction SQL When inquiring , It will produce different results . for example , One SELECT It was executed twice , But the second time it returns a line that was not returned the first time , So this line is a “ Visions ” That's ok .
1.4 SERIALIZABLEBe careful : The emphasis of unreal reading and unrepeatable reading is different , Non repeatable reading focuses on data modification , The same row of data read twice is different ; Unreal reading focuses on adding or deleting , The number of data rows returned by the two queries is different .
serialize , The highest level of transaction isolation , It forces the transaction to be sorted , So that there will be no conflict , Thus, the problem of dirty reading is solved 、 Non repetition and unreal reading , But because the execution efficiency is low , So there are not many real scenarios .
1.5 SummaryJust to summarize ,MySQL The transaction isolation level in is to solve dirty reads 、 Non repeatable reading and unreal reading , this 4 The isolation level is similar to this 3 The corresponding relationship between the questions is as follows :
Transaction isolation level Dirty reading It can't be read repeatedly Fantasy reading| Read uncommitted (READ UNCOMMITTED) | √ | √ | √ |
| Read submitted (READ COMMITTED) | × | √ | √ |
| Repeatable (REPEATABLE READ) | × | × | √ |
| Serialization (SERIALIZABLE) | × | × | × |
The following exist in concurrent transactions 3 A question .
2.1 Dirty readingOne transaction reads the data saved by another transaction for commit , Then the transaction is rolled back , This causes the first transaction to read a dirty data that does not exist .
2.2 It can't be read repeatedlyIn the same transaction , The same query gets different results at different times . For example, the transaction is in T1 Read a row of data , stay T2 Time to reread this line , The data in this line has been modified , So when I read it again, I get an and T1 Different results when querying .
2.3 Fantasy readingMySQL The definition of unreal reading is as follows :
The so-called phantom problem occurs within a transaction when the same query produces different sets of rows at different times. For example, if a SELECT is executed twice, but returns a row the second time that was not returned the first time, the row is a “phantom” row.
Official documents : Translated into Chinese is : The same query gets different results at different times , This is the problem of unreal reading in transactions . for example , One SELECT It was executed twice , But the second time it returns a line that was not returned the first time , So this line is a “ Visions ” That's ok .
3. Isolation level actual combat 3.1 Query transaction isolation levelLook at the big picture MySQL Of the transaction isolation level and the transaction isolation level of the current session SQL as follows :
select @@global.tx_isolation,@@tx_isolation; above SQL The execution result is shown in the figure below :

Each is connected to MySQL The client of can set the isolation level of transactions separately ,MySQL You can use the following SQL Set current connection ( client ) Transaction isolation level for :
set session transaction isolation level Transaction isolation level ;The transaction isolation levels are 4 It's worth :
READ UNCOMMITTED
READ COMMITTED
REPEATABLE READ
SERIALIZABLE
3.3 Dirty reading problemOne transaction reads the data saved by another transaction for commit , Then the transaction is rolled back , This causes the first transaction to read a dirty data that does not exist . Next , We use SQL To demonstrate the dirty reading problem .
Before the official start , First create a test table :
-- Create a city table drop table if exists city;create table city( id int primary key auto_increment, name varchar(250) not null);The dirty reads are executed in the following order :

Dirty read execution SQL The sequence of and execution is as follows :
client A:set session transaction isolation level read uncommitted;
client A:start transaction;
client B:start transaction;
client B:insert into city(name) values(' Xi'an ');
client A:select * from city;
client B:rollback;
client A:select * from city;
The corresponding execution results are shown in the following figure :

As can be seen from the above results , When the client A Set to read uncommitted transaction isolation level , client A You can read uncommitted data from other transactions , When other transactions are rolled back , client A The read data becomes dirty data , This is dirty reading , That is to say There is a dirty read problem in the uncommitted transaction isolation level .
3.4 Unrepeatable read problemIn the same transaction , The same query gets different results at different times . For example, the transaction is in T1 Read a row of data , stay T2 Time to reread this line , The data in this line has been modified , So when I read it again, I get an and T1 Different results when querying .
The non repeatable reads are executed in the following order :

Non repeatable read execution SQL The sequence of and execution is as follows :
client A:set session transaction isolation level read committed;
client A:start transaction;
client A:select * from city where id=1;
client B:start transaction;
client B:update city set name=' changan ' where id=1;
client B:commit;
client A:select * from city where id=1;
The corresponding execution results are shown in the following figure :

As can be seen from the above results , client A After the read committed transaction isolation level is set , Use the same SQL The same piece of data read twice , The content is different , This is unrepeatable reading . That is to say Read the committed transaction isolation level , There may be a problem of non repetition .
3.5 The problem of unreal readingThe same query gets different results at different times , This is the problem of unreal reading in transactions . for example , One SELECT It was executed twice , But the second time it returns a line that was not returned the first time , So this line is a “ Visions ” That's ok .
The execution sequence of unreal reading is as follows :

The execution of unreal reading SQL The sequence of and execution is as follows : client A:set session transaction isolation level repeatable read; client A:start transaction; client A:select * from city where id<5; -- Query out 1 Data client B:start transaction; client B:insert into city(id,name) values(2,' Beijing '); client B:commit; client A:update city set name=' Beijing ' where id=2; client A:select * from city where id<5; -- Query out 2 Data The corresponding execution results are shown in the following figure :

As can be seen from the above results , client A After the transaction isolation level that can be read repeatedly is set , Use the same SQL But the result is the same , A piece of data was found for the first time , The second time, two pieces of data were found , The extra row of data is called “ Visions ” That's ok , So we can get the result , There may be the problem of unreal reading in repeatable reading .
summaryMySQL There is 4 Transaction isolation level : Read uncommitted ( There is dirty reading / It can't be read repeatedly / The problem of unreal reading )、 Read submitted ( There is no repeatable reading / The problem of unreal reading )、 Repeatable ( There is the problem of unreal reading ) And serialization , Which can be read repeatedly is MySQL Default transaction isolation level . Dirty read refers to reading uncommitted data of other transactions , Unrepeatable reading refers to reading data modified by other transactions , Phantom reading is to read the new or deleted data of other transactions “ Visions ” Row data .
This article is about one article MySQL This is the end of the article on transaction isolation level , More about MySQL For the content of transaction isolation level, please search the previous articles of SDN or continue to browse the relevant articles below. I hope you will support SDN more in the future !
边栏推荐
- 【beanshell】数据写入本地多种方法
- ThoughtWorks global CTO: build the architecture according to needs, and excessive engineering will only "waste people and money"
- 【Web攻防】WAF检测技术图谱
- How does the outer disk futures platform distinguish formal security?
- Wsl2.0 installation
- WSL2.0安装
- Browser rendering principle and rearrangement and redrawing
- 【性能测试】jmeter+Grafana+influxdb部署实战
- flask解决CORS ERR 问题
- 2022 年 Q2 加密市场投融资报告:GameFi 成为投资关键词
猜你喜欢
随机推荐
Is it safe for qiniu business school to open a stock account? Is it reliable?
Embedded -arm (bare board development) -1
微信公众号网页授权登录实现起来如此简单
Three traversal methods of binary tree
【testlink】TestLink1.9.18常见问题解决方法
C language to get program running time
WSL2.0安装
Application of threshold homomorphic encryption in privacy Computing: Interpretation
Etcd 构建高可用Etcd集群
兰空图床苹果快捷指令
Function sub file writing
Jarvis OJ Telnet Protocol
Embedded UC (UNIX System Advanced Programming) -3
Is it safe to open an account for digging wealth stocks? How is it safe to open a stock account?
时间戳strtotime前一天或后一天的日期
It is forbidden to copy content JS code on the website page
Learnopongl notes (I)
WR | 西湖大学鞠峰组揭示微塑料污染对人工湿地菌群与脱氮功能的影响
【jmeter】jmeter脚本高级写法:接口自动化脚本内全部为变量,参数(参数可jenkins配置),函数等实现完整业务流测试
[Web attack and Defense] WAF detection technology map








![[729. My schedule I]](/img/e3/32914227d00cf7595ee850e60f2b72.png)
