当前位置:网站首页>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
边栏推荐
- Deep learning: multi-layer perceptron and XOR problem (pytoch Implementation)
- Thunderbolt Chrome extension caused the data returned by the server JS parsing page data exception
- Yiwen takes you to know ZigBee
- 从输入URL到页面展示这中间发生了什么?
- Practice of traffic recording and playback in vivo
- PHP constructor with parameters - PHP constructor with a parameter
- Force deduction ----- the minimum path cost in the grid
- I2C 子系统(三):I2C Driver
- Cron表达式介绍
- Check log4j problems using stain analysis
猜你喜欢

vfork执行时出现Segmentation fault

Deep learning: multi-layer perceptron and XOR problem (pytoch Implementation)

A2L file parsing based on CAN bus (2)

Add automatic model generation function to hade

Joking about Domain Driven Design (III) -- Dilemma

Three.js本地环境搭建

Left connection, inner connection
![[principles of multithreading and high concurrency: 1_cpu multi-level cache model]](/img/c7/6b5ab4ff7379bfccff7cdbb358ff8f.jpg)
[principles of multithreading and high concurrency: 1_cpu multi-level cache model]

HTB-Devel
![ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc](/img/cb/145937a27ef08050a370d5a255215a.jpg)
ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc
随机推荐
How to limit the size of the dictionary- How to limit the size of a dictionary?
yii2 中andWhere多个or查询 orm条件
MySQL practice 45 [SQL query and update execution process]
[Fuhan 6630 encodes and stores videos, and uses RTSP server and timestamp synchronization to realize VLC viewing videos]
C # general interface call
tensor中的append应该如何实现
JS finds all the parent nodes or child nodes under a node according to the tree structure
Sous - système I2C (IV): débogage I2C
MySql实战45讲【索引】
C language beginner level - pointer explanation - paoding jieniu chapter
Didi programmers are despised by relatives: an annual salary of 800000 is not as good as two teachers
Deep learning: multi-layer perceptron and XOR problem (pytoch Implementation)
Super easy to use logzero
45 lectures on MySQL [index]
Unity3d human skin real time rendering real simulated human skin real time rendering "suggestions collection"
Getting started | jetpack hilt dependency injection framework
Deep learning: multi-layer perceptron and XOR problem (pytoch Implementation)
Pytest (6) -fixture (Firmware)
Add automatic model generation function to hade
docker安装redis