当前位置:网站首页>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
边栏推荐
- C # general interface call
- Practice of traffic recording and playback in vivo
- As a leader, how to control the code version and demand development when the epidemic comes| Community essay solicitation
- 超好用的日志库 logzero
- Cron表达式介绍
- Docker install MySQL
- [principles of multithreading and high concurrency: 1_cpu multi-level cache model]
- Thunderbolt Chrome extension caused the data returned by the server JS parsing page data exception
- 一文带你了解 ZigBee
- Privatization lightweight continuous integration deployment scheme -- 01 environment configuration (Part 2)
猜你喜欢

I2C subsystem (IV): I2C debug

I2C 子系统(一):I2C spec

MySql实战45讲【行锁】

idea 加载不了应用市场解决办法(亲测)
![[error record] the parameter 'can't have a value of' null 'because of its type, but the im](/img/1c/46d951e2d0193999f35f14d18a2de0.jpg)
[error record] the parameter 'can't have a value of' null 'because of its type, but the im

Three.js本地环境搭建

Kubernetes cluster log and efk architecture log scheme
![[flutter] example of asynchronous programming code between future and futurebuilder (futurebuilder constructor setting | handling flutter Chinese garbled | complete code example)](/img/04/88ce45d370a2e6052c2fce558aa531.jpg)
[flutter] example of asynchronous programming code between future and futurebuilder (futurebuilder constructor setting | handling flutter Chinese garbled | complete code example)

Three. JS local environment setup

Creation and destruction of function stack frame
随机推荐
Nasvit: neural architecture search of efficient visual converter with gradient conflict perception hypernetwork training
C # general interface call
JMeter performance test JDBC request (query database to obtain database data) use "suggestions collection"
当lambda没有输入时,是何含义?
MySql实战45讲【索引】
Practice of traffic recording and playback in vivo
用docker 连接mysql的过程
Concrete CMS vulnerability
MySql实战45讲【全局锁和表锁】
Can I use read-only to automatically implement properties- Is read-only auto-implemented property possible?
左值右指解释的比较好的
Thunderbolt Chrome extension caused the data returned by the server JS parsing page data exception
Source code analysis | layout file loading process
About HTTP cache control
The solution of "the required function is not supported" in win10 remote desktop connection is to modify the Registry [easy to understand]
Distributed transaction
用docker 連接mysql的過程
Unity3d human skin real time rendering real simulated human skin real time rendering "suggestions collection"
左连接,内连接
ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc