当前位置:网站首页>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
边栏推荐
- Image classification system based on support vector machine (Matlab GUI interface version)
- Control scanner in Delphi
- The weight of the input and textarea components of the applet is higher than that of the fixed Z-index
- 遍历数组,删除某元素,直到删除为止
- In my ten years, every bit has become a landscape?
- MySQL index
- Implementing fillet in custom dialog
- 【 enregistrement pytorch】 paramètre et tampon des variables pytorch. Self. Register Buffer (), self. Register Paramètre ()
- Beginner development process_ Project development FAQs
- Wechat applet obtains the current location (startlocationupdate, onlocationchange, offlocationchange)
猜你喜欢
Vs Code modify default terminal_ Modify the default terminal opened by vs Code
Radio design and implementation in IVI system
JMeter quick start
Detailed explanation of UCI datasets and their data processing (with 148 datasets and processing codes attached)
Wechat applet coordinate location interface usage (II) map interface
Data processing in detailed machine learning (II) -- Feature Normalization
Vscode liveserver use_ Liveserver startup debugging
Detailed installation tutorial of MATLAB r2019 B-mode ultrasound (complete installation files are attached)
Flutter reports an error type 'Int' is not a subtype of type 'string' wonderful experience
[data analysis and visualization] key points of data drawing 11- precautions for radar chart
随机推荐
Linked list: delete the penultimate node of the linked list
專業的數據庫管理軟件:Valentina Studio Pro for Mac
Detailed explanation of handwritten numeral recognition based on support vector machine (Matlab GUI code, providing handwriting pad)
Linked list: orderly circular linked list
Wechat applet obtains the current location (startlocationupdate, onlocationchange, offlocationchange)
[data analysis and visualization] key points of data drawing 10- construction of legend
In my ten years, every bit has become a landscape?
【 enregistrement pytorch】 paramètre et tampon des variables pytorch. Self. Register Buffer (), self. Register Paramètre ()
Spark UDF instance details
Pycharm installation pyqt5 and its tools (QT designer, pyuic, pyrcc) detailed tutorial
Introduction to Kestrel_ Introduction to kestrel web server
Hash table: least recently used cache
IOS interview · full bat interview record of an IOS programmer (including the true interview questions of Baidu + Netease + Alibaba)
【pytorch 记录】pytorch的变量parameter、buffer。self.register_buffer()、self.register_parameter()
Ijkplayer source code - setting options
Six special GPU products for domestic aircraft passed the appraisal and review
IOS development interview knowledge sorting - OC Foundation (II)
[data analysis and visualization] key points of data drawing 3- spaghetti map
Using binary heap to implement priority queue
Vs Code modify default terminal_ Modify the default terminal opened by vs Code