当前位置:网站首页>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
边栏推荐
- vfork执行时出现Segmentation fault
- 函数栈帧的创建与销毁
- The left value and the right finger explain better
- Nasvit: neural architecture search of efficient visual converter with gradient conflict perception hypernetwork training
- Serious security vulnerabilities reported by moxa mxview network management software
- 力扣------网格中的最小路径代价
- docker安装redis
- Deep reinforcement learning for intelligent transportation systems: a survey paper reading notes
- Sous - système I2C (IV): débogage I2C
- sql server数据库添加 mdf数据库文件,遇到的报错
猜你喜欢

MySql实战45讲【全局锁和表锁】

Add some hard dishes to the interview: how to improve throughput and timeliness in delayed task scenarios!

Practice of traffic recording and playback in vivo

Thunderbolt Chrome extension caused the data returned by the server JS parsing page data exception

你真的懂继电器吗?

Can netstat still play like this?

内存泄漏工具VLD安装及使用

Xiaodi notes

Your family must be very poor if you fight like this!

Sqlserver row to column pivot
随机推荐
The base value is too large (the error is marked as "08") [duplicate] - value too great for base (error token is'08') [duplicate]
模糊查询时报错Parameter index out of range (1 > number of parameters, which is 0)
Can netstat still play like this?
tensorflow转pytorch笔记;tf.gather_nd(x,y)转pytorch
C # general interface call
docker安装mysql
Unity3d human skin real time rendering real simulated human skin real time rendering "suggestions collection"
Basic information of Promethus (I)
Tensorflow to pytorch notes; tf. gather_ Nd (x, y) to pytorch
[C语言]给账号密码进行MD5加密
Reset or clear NET MemoryStream - Reset or Clear . NET MemoryStream
How to select the minimum and maximum values of columns in the data table- How to select min and max values of a column in a datatable?
Counter统计数量后,如何返回有序的key
Anhui University | small target tracking: large-scale data sets and baselines
I2C 子系统(四):I2C debug
What happens between entering the URL and displaying the page?
Your family must be very poor if you fight like this!
Agile certification (professional scrum Master) simulation exercise-2
从C到Capable-----利用指针作为函数参数求字符串是否为回文字符
Opengauss database development and debugging tool guide