当前位置:网站首页>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
边栏推荐
- Linked list: orderly circular linked list
- Ijkplayer source code - setting option 2
- Prometheus node_ Exporter installs and registers as a service
- Available types in C #_ Unavailable type_ C double question mark_ C question mark point_ C null is not equal to
- nn. Conv2d and nn Convtranspose2d differences
- Image classification system based on support vector machine (Matlab GUI interface version)
- Mp4 playback
- Linked list: delete the penultimate node of the linked list
- Es and kibana deployment and setup
- Find the number of permutations
猜你喜欢

專業的數據庫管理軟件:Valentina Studio Pro for Mac

Detailed installation tutorial of MATLAB r2019 B-mode ultrasound (complete installation files are attached)

Introduction and download of common data sets for in-depth learning (with network disk link)
![[life science] DNA extraction of basic biological experiments](/img/84/c1968c2c08feab44b14a529420eea9.jpg)
[life science] DNA extraction of basic biological experiments
![[data analysis and visualization] key points of data drawing 12- importance of chart notes](/img/9c/c610c6f9d08952aece97f788ae35a7.jpg)
[data analysis and visualization] key points of data drawing 12- importance of chart notes

Scala implements workcount
![PCR validation of basic biological experiments in [life sciences]](/img/92/1cecb7cb4728937bd18b336ba4e606.jpg)
PCR validation of basic biological experiments in [life sciences]

Es and kibana deployment and setup

Detailed explanation of UCI datasets and their data processing (with 148 datasets and processing codes attached)

Vant框架中关于IndexBar索引栏的CDN单页面引用,无法正常展示
随机推荐
Word splitting problem
Implementing fillet in custom dialog
Ijkplayer source code - setting option 2
Ijkplayer source code - audio playback
Linked list: adding numbers in the linked list
PCR validation of basic biological experiments in [life sciences]
My practice of SOA architecture project based on WCF
Code d'initialisation de l'arbre binaire
The latest Matlab r2020 B ultrasonic detailed installation tutorial (with complete installation files)
Vs Code modify default terminal_ Modify the default terminal opened by vs Code
Delphi implements adding a column of serial number to the CXGRID list
How to select fund products? What kind of fund is a good fund?
Available types in C #_ Unavailable type_ C double question mark_ C question mark point_ C null is not equal to
Linked list: palindrome linked list
【 enregistrement pytorch】 paramètre et tampon des variables pytorch. Self. Register Buffer (), self. Register Paramètre ()
2019 - sorting out the latest and most comprehensive IOS test questions (including framework and algorithm questions)
Using linked list to find set union
CDN single page reference of indexbar index column in vant framework cannot be displayed normally
Wechat applet coordinate location interface usage (II) map interface
mysql索引