当前位置:网站首页>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
边栏推荐
- MySQL practice 45 [SQL query and update execution process]
- 45 lectures on MySQL [index]
- 超好用的日志库 logzero
- MySql实战45讲【全局锁和表锁】
- TCP handshake three times and wave four times. Why does TCP need handshake three times and wave four times? TCP connection establishes a failure processing mechanism
- open file in 'w' mode: IOError: [Errno 2] No such file or directory
- yii2 中andWhere多个or查询 orm条件
- Yiwen takes you to know ZigBee
- js根据树结构查找某个节点的下面的所有父节点或者子节点
- Source code analysis | layout file loading process
猜你喜欢
TCP handshake three times and wave four times. Why does TCP need handshake three times and wave four times? TCP connection establishes a failure processing mechanism
Deep learning: multi-layer perceptron and XOR problem (pytoch Implementation)
I2C subsystem (III): I2C driver
Sous - système I2C (IV): débogage I2C
Add automatic model generation function to hade
Spark on yarn资源优化思路笔记
Can netstat still play like this?
A2L file parsing based on CAN bus (2)
docker安装redis
HW initial preparation
随机推荐
Deep Reinforcement Learning for Intelligent Transportation Systems: A Survey 论文阅读笔记
Reset or clear NET MemoryStream - Reset or Clear . NET MemoryStream
Thunderbolt Chrome extension caused the data returned by the server JS parsing page data exception
MySql实战45讲【行锁】
Deep learning: multi-layer perceptron and XOR problem (pytoch Implementation)
tensorflow转pytorch笔记;tf.gather_nd(x,y)转pytorch
Introduction to cron expression
MySql实战45讲【SQL查询和更新执行流程】
Destroy the session and empty the specified attributes
Distributed transaction
How to use asp Net MVC identity 2 change password authentication- How To Change Password Validation in ASP. Net MVC Identity 2?
C语言初阶-指针详解-庖丁解牛篇
Two dimensional format array format index subscript continuity problem leads to return JSON format problem
Deep learning: multi-layer perceptron and XOR problem (pytoch Implementation)
The left value and the right finger explain better
[Fuhan 6630 encodes and stores videos, and uses RTSP server and timestamp synchronization to realize VLC viewing videos]
MySQL practice 45 [global lock and table lock]
How to implement append in tensor
你真的懂继电器吗?
Edit and preview in the back pipe to get the value writing method of the form