当前位置:网站首页>MySQL transactions and locks (V)
MySQL transactions and locks (V)
2022-06-13 03:01:00 【summer_ forty-five】
Database notes
- Mysql Index bottom layer ( One )
- Explain Tools and index optimization ( Two )
- SQL stay MySQL Execution process in ( 3、 ... and )
- MySQL Index optimization ( Four )
- MySQL Transactions and locks ( 5、 ... and )
- MVCC and BufferPool( 6、 ... and )
Transactions and locks
Business
Definition
The business is made up of a group of SQL A logical processing unit made up of statements , have ACID Four properties
attribute
A Atomicity : In business all operation Agreement
C Uniformity : Operations within a transaction == As a result of Data modification Should agree ==
I Isolation, : During transaction processing ** The intermediate state is invisible to the outside ** Of ; External state changes are also invisible to transactions .
D persistence : After the transaction completes , Yes == The modification of data is permanent .==
Problems caused by concurrent transaction processing
Dirty write —— Last update covers Updates made by other firms
Dirty reading —— Business A Read the business B Modified but not submitted The data of 【 Does not satisfy the isolation 】
Don't reread —— Business A Internal same query SQL Inconsistent results 【 Does not satisfy the isolation 】
Fantasy reading —— Business A Read transaction B New data submitted 【 Does not satisfy the isolation 】
Transaction isolation level
Definition
Databases have different isolation levels , The default isolation level is Repeatable
View the transaction isolation level of the current database :
show variables like 'tx_isolation';Set the transaction isolation level :
set tx_isolation='REPEATABLE-READ';
classification
- Read uncommitted
- Read submitted
- Repeatable
- Serializable
Read uncommitted
set tx_isolation='read-uncommitted';
technological process :
- client A With client B Each setting is Read uncommitted
- client B** Update table account,** but Do not commit transaction
- client A Query table account, Read B Modified data
- If at this time ,B Roll back ,A Read dirty data
Possible problems :
Dirty reading 、 Don't reread 、 Fantasy reading
Read submitted
set tx_isolation = 'read-committed';
technological process :
- client A With client B Each setting is Read submitted
- client B** Update table account,** Commit transaction
- client A Query table account, Read B Modified data
Possible problems :
Don't reread 、 Fantasy reading
Repeatable
set tx_isolation = 'repeatable-read';
adopt undo Log files for version control , Achieve isolation
technological process :
- client A With client B Each setting is Repeatable
- client B** To the table account Add a new one id by 100 The data of ,** Commit transaction
- client A Query table account, You can go to id by 100 The data of
Possible problems :
Fantasy reading
Serialization
set tx_isolation = 'serializable';
adopt Lock To achieve serialization
technological process :
Case one
- client A With client B Each setting is Serializable
- client A perform Inquire about id by 1 The sentence of 【 add Row lock 、 Read the lock 、 Clearance lock 】
- client B here to update id by 1 The statement of will Block waiting , to update id by 2 The statement can be executed normally
The second case
- client A With client B Each setting is Serializable
- client B here Insert id by 1 The sentence of 【 add Row lock 、 Write lock 】
- client A perform Inquire about id by 1 The statement of will Block waiting
Lock details
Definition :
Locks are used to control Critical section resources For who
The following lock categories , The database implements Row lock and table lock
Lock classification
performance —— Optimism lock and Pessimistic locking
Operation type —— Read the lock and Write lock
Lock granularity —— Row lock and Table locks and Clearance lock
Various types of locks can be crossed , Not necessarily mutually exclusive
Optimism lock
Definition :
Each thread uses the version number comparison to achieve the goal Critical section resources
Pessimistic locking
Definition :
Each thread vies for the lock Critical section resources
Read the lock
Definition :
Shared lock S
For the same data , If ** Threads A Get the read lock **
Multiple thread read operations can be performed simultaneously , Except for threads A Other thread writes will be blocked .
It's a pessimistic lock
Write lock
Definition :
Exclusive lock X
For the same data , If ** Threads A Get the write lock **
Only the thread A Be able to read and write data , The read and write operations of other threads will be blocked .
Row lock
Definition :
Lock one row of data per operation , Smaller particle size .
classification :
Row lock + Read the lock —— Lock the data in this row , Other threads' writes to this row are blocked
select * from test_innodb_lock where a = 2 lock in share mode;Row lock + Write lock —— Lock the data in this row , Other threads' read and write operations to this row are blocked
select * from test_innodb_lock where a = 2 for update;
expenses :
because The row lock should be positioned to a certain row , So it locks slowly , The cost will increase , And there will be a deadlock .
Lock escalation :
InnoDB The row lock of is the lock added for the index , Instead of recording the lock added . If Update the non indexed field or the index becomes invalid , Row locks may be upgraded to table locks .
Line lock analysis :
# Inquire about InnoDB Row lock contention
show status like 'innodb_row_lock%';
Innodb_row_lock_time_avg ( Average waiting time )
Innodb_row_lock_waits ( Total waiting times )
Innodb_row_lock_time( The total waiting time )
Optimize according to the situation
Table locks
Definition :
Lock one row of data per operation , The particle size is large , Applicable to whole table data migration .
classification :
Table locks + Read the lock —— Lock the table data , Other threads' writes to the table are blocked
lock table The name of the table read;Table locks + Write lock —— Lock the table data , Other threads' read and write operations to the table are blocked
lock table The name of the table write;
Clearance lock
Definition :
Clearance lock , Is to lock the gap between two lines 【 Section 】 Only in Repeatable To take effect
Example :

Existing interval id by (3,10) (10,20) (20,+∞)
stay session1 In the implementation of
# UPDATE statement update account set name = 'zhuge' where id > 8 and id <18; ## MySQL A clearance lock will be added Lock the (8,18) ## because 8 Fall to the original (3,10) The range of ,18 Fall in the (10,20) The range of , So the locked interval is (3,20]Lock according to clearance , Lock the (3,20]
Add a write lock to the interval , other session This interval cannot be inserted 、 Delete 、 Modify the operating
View the related data table of system library lock
## see INFORMATION_SCHEMA Data table related to system library lock
# View transactions
select * from INFORMATION_SCHEMA.INNODB_TRX;
# Check the lock
select * from INFORMATION_SCHEMA.INNODB_LOCKS;
# View lock wait
select * from INFORMATION_SCHEMA.INNODB_LOCK_WAITS;
# Release the lock ,trx_mysql_thread_id It can be downloaded from INNODB_TRX See in the table
kill trx_mysql_thread_id
# View lock wait details
show engine innodb status\G;
summary
- MyISAM stay DQL front , Will give == The tables involved are read locked .== stay DML It will be given before ** The tables involved are locked .** 【MyISAM Table locks 】
- InnoDB stay DQL front ,( Non serial isolation level ) No locks . stay DML It will be given before == The rows involved are read locked .==【InnoDB Row lock 】
- When performing updates, try to operate with original values
update Table name set Field 1 = Field 1 - 50 where Conditions - Data is retrieved by index , Avoid row lock escalation
- Minimize the range of search conditions , Avoid gap locks
- ** Control transaction size ,** It may be locked SQL Execute after the transaction
边栏推荐
- [data and Analysis Visualization] D3 introductory tutorial 1-d3 basic knowledge
- 2022 qianle micro cloud technology learning task plan
- Logiciel professionnel de gestion de base de données: Valentina Studio Pro pour Mac
- JVM class loading (I)
- Ijkplayer source code ---packetqueue
- Graduation project - campus old thing recycling system based on stm32
- [data analysis and visualization] key points of data drawing 11- precautions for radar chart
- js 解构赋值
- Vscode liveserver use_ Liveserver startup debugging
- [data and Analysis Visualization] data operation in D3 tutorial 3-d3
猜你喜欢

Keil removes annoying st link update tips

Professional database management software: Valentina Studio Pro for Mac
![[life science] DNA extraction of basic biological experiments](/img/84/c1968c2c08feab44b14a529420eea9.jpg)
[life science] DNA extraction of basic biological experiments

Es and kibana deployment and setup
![[data analysis and visualization] key points of data mapping 7- over mapping](/img/ae/d4e251b37ec4857c99f738ca981092.jpg)
[data analysis and visualization] key points of data mapping 7- over mapping

二叉樹初始化代碼

Data warehouse notes | 5 factors that need attention for customer dimension modeling

JVM class loading (I)
![[data analysis and visualization] key points of data drawing 3- spaghetti map](/img/3d/ea832e67d22c62b07dc46cf49e50ba.jpg)
[data analysis and visualization] key points of data drawing 3- spaghetti map

Detailed explanation of data processing in machine learning (I) -- missing value processing (complete code attached)
随机推荐
How did you spend your winter vacation perfectly?
数字IC设计——FIFO的设计
Radio design and implementation in IVI system
mysql索引
OneNote User Guide (1)
Ijkplayer source code -- Library loading and initialization
Redis server configuration
Techniques for collecting stringgrid
Vs Code modify default terminal_ Modify the default terminal opened by vs Code
My practice of SOA architecture project based on WCF
Find the number of permutations
PCR validation of basic biological experiments in [life sciences]
[data analysis and visualization] key points of data drawing 5- the problem of error line
. Net compact Framework2.0 wince intelligent device development project experience sharing Net drag space advanced
Introduction and download of common data sets for in-depth learning (with network disk link)
Prometheus install and register services
PK of dotnet architecture
[data analysis and visualization] key points of data drawing 8- use of circular bar chart
. New features in net 6.0 _ What's new in net 6.0
HEAP[xxx.exe]: Invalid address specified to RtlValidateHeap( 0xxxxxx, 0x000xx)