当前位置:网站首页>MySQL practice 45 lecture [transaction isolation]
MySQL practice 45 lecture [transaction isolation]
2022-07-03 03:06:00 【Little fish 2020】
List of articles
03 | The transaction isolation : Why do you change? I can't see ?
Transaction is to ensure a set of database operations , All or nothing , All or nothing .
Isolation and isolation level
ACID(Atomicity、Consistency、Isolation、Durability, Atomicity 、 Uniformity 、 Isolation, 、 persistence .
When there are multiple transactions in the database executing at the same time , There may be dirty reading (dirty read)、 It can't be read repeatedly (non-repeatable read)、 Fantasy reading (phantom read) The problem of , To solve these problems , And then there is “ Isolation level ” The concept of .
Dirty reading (dirty read)
Dirty read refers to reading uncommitted data from other transactions , Uncommitted means that the data may be rolled back , That is, it may not be stored in the database eventually , That is, data that doesn't exist . Read the data that must eventually exist , This is dirty reading .
Repeatable
Repeatable reading refers to within a transaction , The first read data is consistent with the same batch of data read at any time before the end of the transaction . Usually for data updates (UPDATE) operation .
It can't be read repeatedly (non-repeatable read)
Contrast can be read repeatedly , Nonrepeatable reading refers to within the same transaction , The same batch of data read at different times may be different , May be affected by other things , For example, other transactions change this batch of data and commit . Usually for data updates (UPDATE) operation .
Fantasy reading (phantom read)
Unreal reading is for data insertion (INSERT) In terms of operation . What if A Changes have been made to the contents of some lines , But it hasn't been submitted yet , At this point, the transaction B Insert with transaction A The record before the change is the same as the record line , And in business A Submitted before submitting , And then , In the transaction A Query in , You'll find that the change you just made doesn't work for some data , But it's business B Just inserted in , It makes users feel magical , I feel like I'm hallucinating , This is called Unreal reading .
Cannot be found in the same transaction , But it can't be inserted ( For example, primary key conflicts ), Because it was inserted and committed by another transaction
Isolation level There are four kinds. , Namely : Read uncommitted 、 Read submitted 、 Repeatable 、 serialize .
Read uncommitted : Read Uncommitted, seeing the name of a thing one thinks of its function , That is, one transaction can read the data of another uncommitted transaction . The lowest level , It exists 4 A FAQ ( Dirty reading 、 It can't be read repeatedly 、 Fantasy reading 、 Lost update ).
Read submitted : Read Committed, seeing the name of a thing one thinks of its function , It means that one transaction cannot read data until another transaction is committed . It solves the dirty reading problem , There is 3 A FAQ ( It can't be read repeatedly 、 Fantasy reading 、 Lost update ).
Repeatable : Repeatable Read, You're just starting to read the data ( The transaction open ) when , Modification operations are no longer allowed . It solves dirty reading and non repeatable reading , There is still 2 A FAQ ( Fantasy reading 、 Lost update ). The data that the transaction sees during execution must be consistent before and after
serialize : Serializable, serialize , Or serialization . Is to execute each transaction in a certain order , It will solve all the isolation problems , But this level of transaction isolation is inefficient , Compare database performance , Generally not used .
The default transaction isolation level for most databases is Read Committed
On the implementation , A view will be created in the database , When accessing, the logical result of the view shall prevail .
stay “ Repeatable ” Under isolation level , This view is created when the transaction starts , This view is used throughout the life of the transaction
stay “ Read the submission ” Under isolation level , This view is in each SQL Created when the statement begins execution .
“ Read uncommitted ” Directly return the latest value on the record under isolation level , There is no view concept .
Serialization ” Under isolation level, lock is used to avoid parallel access .
-- Check the data isolation sector
show variables like 'transaction_isolation';
The settable value is :
READ-UNCOMMITTED
READ-COMMITTED
REPEATABLE-READ
SERIALIZABLE
Implementation of transaction isolation
stay MySQL in , In fact, each record will record a rollback operation at the same time when it is updated . The latest value on the record , By rolling back operations , Can get the value of the previous state .
Suppose a value from 1 Has been changed in order to 2、3、4, In the rollback log, there will be records like the following .
The current value is 4, But when looking up this record , There will be different transactions started at different times read-view,
As you can see in the picture , In view A、B、C Inside , The values of this record are 1、2、4, There can be multiple versions of the same record in the system , It's multi version concurrency control of database (MVCC).
Long transactions mean that there will be a very old transaction view in the system . Since these transactions may access any data in the database at any time , So before this transaction is committed , All possible rollback records in the database must be kept , This can lead to a lot of storage space .
In addition to the impact on rollback segments , Long transactions also take up lock resources , It could also drag down the entire warehouse .
How to start a transaction
- Start the transaction statement explicitly , begin or start transaction. The accompanying commit statement is commit, The rollback statement is rollback
- set autocommit=0( Auto start ), This command will turn off the automatic submission of this thread . It means that if you only execute select sentence , This transaction starts , And it doesn't automatically commit . This business continues until you take the initiative to commit or rollback sentence , Or disconnect
- therefore , I would suggest that you always use set autocommit=1, Start the transaction through an explicit statement , stay autocommit by 1 Under the circumstances , use begin Explicitly started transactions , If you execute commit Then commit the transaction .information_schema Library innodb_trx Query long transactions in this table
select * from information_schema.innodb_trx
where TIME_TO_SEC(timediff(now(),trx_started))>60
边栏推荐
- Can I use read-only to automatically implement properties- Is read-only auto-implemented property possible?
- TCP 三次握手和四次挥手机制,TCP为什么要三次握手和四次挥手,TCP 连接建立失败处理机制
- Kubernetes family container housekeeper pod online Q & A?
- 从C到Capable-----利用指针作为函数参数求字符串是否为回文字符
- Parameter index out of range (1 > number of parameters, which is 0)
- Concrete CMS vulnerability
- C语言中左值和右值的区别
- Check log4j problems using stain analysis
- 当lambda没有输入时,是何含义?
- Thunderbolt Chrome extension caused the data returned by the server JS parsing page data exception
猜你喜欢
MySql實戰45講【SQL查詢和更新執行流程】
用docker 连接mysql的过程
I2C 子系统(一):I2C spec
TCP 三次握手和四次挥手机制,TCP为什么要三次握手和四次挥手,TCP 连接建立失败处理机制
你真的懂继电器吗?
Didi programmers are despised by relatives: an annual salary of 800000 is not as good as two teachers
内存泄漏工具VLD安装及使用
Spark on yarn资源优化思路笔记
Le processus de connexion mysql avec docker
I2C subsystem (II): I3C spec
随机推荐
How to implement append in tensor
Le processus de connexion mysql avec docker
Xiaodi notes
Use of check boxes: select all, deselect all, and select some
sql server 查詢指定錶的錶結構
力扣------网格中的最小路径代价
从输入URL到页面展示这中间发生了什么?
PHP constructor with parameters - PHP constructor with a parameter
Docker install redis
[C语言]给账号密码进行MD5加密
docker安装mysql
敏捷认证(Professional Scrum Master)模拟练习题-2
模糊查询时报错Parameter index out of range (1 > number of parameters, which is 0)
【富瀚6630编码存录像,用rtsp服务器及时间戳同步实现vlc观看录像】
迅雷chrome扩展插件造成服务器返回的数据js解析页面数据异常
The process of connecting MySQL with docker
Nasvit: neural architecture search of efficient visual converter with gradient conflict perception hypernetwork training
ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc
I2C 子系统(三):I2C Driver
疫情当头,作为Leader如何进行代码版本和需求开发管控?| 社区征文