background
Recently MySQL There are a lot of official documents , Here we open a pit to translate some chapters , And attach some side notes , It is used to show the results of the actual operation , Or express personal understanding .
Document version :8.0
source :innodb-locking
This kind of form is a sidenote .
This paper mainly introduces InnoDB All kinds of locks in , The lock trigger conditions and application scenarios are not all mentioned in this article , We will explain it in a separate article later .
Shared lock & An exclusive lock
InnoDB Two types of standard row locks are implemented : share (S) Lock and exclusive (X) lock .( Hereinafter referred to as" S Lock and X lock )
- S A lock allows the transaction holding the lock to read a row of records
- X A lock allows the transaction holding the lock to update or delete a row of records
If the transaction T1 Holding bank R Of S lock , Another business T2 In line R Try to get the lock on , There will be the following scenarios :
- T2 request S lock , Directly available . here T1 and T2 All hold the line R Of S lock .
- T2 request X lock , You can't get it directly .
If T1 Holding bank R Of X lock , Another business T2 In line R Try to acquire any lock on , You can't get it directly .T2 Must wait T1 Let go R The lock on the .
What I'm talking about here S/X Locks are more likely to describe the model of locks : That is, how the lock is obtained 、 The interaction between resource control and locks .
The following types of locks are based on S/X Model to achieve , The difference is in the granularity 、 Strength, etc .
Intent locks
InnoDB Support Multi granularity lock : That is, row lock and table lock coexist . For example, statement LOCK TABLES ... WRITE
Get the X lock .InnoDB Use Intent locks Multiple granularity is implemented in
Lock it up . Intention lock is table lock , Used to indicate which type of row lock a transaction will acquire later (S or X). There are two types of intent locks :
- Sharing intention lock (IS): Indicates that the transaction is about to acquire a shared lock on the row
- Exclusive intent lock (IX): Indicates that the transaction is about to acquire an exclusive lock on the row
for example ,SELECT ... FOR SHARE
Got IS lock ,SELECT ... FOR UPDATE
Got IX lock .
Be careful 5.6\5.7 Version acquisition IS Lock statements are different :
SELECT ... LOCK IN SHARE MODE
The use principle of intention lock :
If a transaction wants to get the line of S lock , You must first get the table's IS Lock or a higher level lock .
If a transaction wants to get the line of X lock , You must first get the table's IX lock .
The compatibility between table locks is summarized as follows :
X | IX | S | IS | |
---|---|---|---|---|
X | Conflict | Conflict | Conflict | Conflict |
IX | Conflict | compatible | Conflict | compatible |
S | Conflict | Conflict | compatible | compatible |
IS | Conflict | compatible | compatible | compatible |
The lock requested by the transaction must be compatible with the lock currently generated , Otherwise, you can't get , Until the conflict lock is released . And the process of waiting for release if InnoDB Deadlock detected , An error will be thrown .
Intent locks do not block other lock requests , Except for the watch lock ( Such as LOCK TABLES ... WRITE
). Intent locks are used to indicate that a transaction is trying to acquire , Or about to acquire a row lock .
If you want to view intent locks in the current database , perform SHOW ENGINE INNODB STATUS
, InnoDB The monitor will output the following :
TABLE LOCK table `test`.`t` trx id 10080 lock mode IX
If you can't see lock information in the output log , You need to turn on the following parameters :
SET GLOBAL innodb_status_output_locks=ON;
see : innodb-enabling-monitors
Record locks
A record lock is used to lock an index value . For example, statement SELECT c1 FROM t WHERE c1 = 10 FOR UPDATE;
Will prevent other transactions from targeting t.c1=10 Add, delete and modify all the lines of the .
The record lock only locks the index value , Even if there is no index defined in the table . If there is no index ,InnoDB Will implicitly create a clustered index , For record lock .
If you want to view the record locks in the current database , perform SHOW ENGINE INNODB STATUS
, InnoDB The monitor will output the following :
RECORD LOCKS space id 58 page no 3 n bits 72 index `PRIMARY` of table `test`.`t`
trx id 10078 lock_mode X locks rec but not gap
Record lock, heap no 2 PHYSICAL RECORD: n_fields 3 ; compact format; info bits 0
0 : len 4 ; hex 8000000 a; asc ;;
1 : len 6 ; hex 00000000274 f; asc 'O;;
2 : len 7 ; hex b60000019d0110; asc ;;
trx id 10078 lock_mode X locks rec but not gap It means that the record lock only locks a single record without locking any gap , This is also the result of the usual primary key query , The concept of clearance is described in the following .
When the query condition does not cover the index , experiment :
1. Add auto increment primary key to a table , Insert several records
2. Delete the index of the primary key column
3. Execute with the value of the original primary key column as the query condition SELECT FOR UPDATEMonitor output :
RECORD LOCKS space id 3 page no 6 n bits 320 index GEN_CLUST_INDEX of table `test`.`t` trx id 2131 lock_mode X
In other words, it uses the implicitly generated cluster index .
In this case, even if the column in the query condition is unique in value , It also locks the full table record ( Because it took a full scan ).
Now open another transaction , Perform a primary key lock query on another record (SELECT FOR UPDATE), according to S/X Lock criteria will be blocked , It turns out to be true .
Clearance lock
Gap locks are used to lock gaps between index records , Or the gap between the two ends of a set of index values . For example, statements SELECT c1 FROM t WHERE c1 BETWEEN 10 and 20 FOR UPDATE;
It can prevent other things from happening t.c1 Insert... On the column 15 ( Because in 10-20 Between ), No matter what 15 Is the value already on the column , Because in BETWEEN The specified intervals are locked .
So called gaps can cover a value , Multiple values , Even 0 individual .
For the exact meaning of clearance, the official glossary is quoted here :
The gap Can be in InnoDB The position in an index data structure that can be inserted . For example SELECT ... FOR UPDATE When you lock up a batch of lines ,InnoDB The values on the index where the condition hits and the gap between them will be locked . For example, lock read all greater than 10 The value of , Gap locks prevent other transactions from inserting greater than 10 Value .
The clearance lock reflects to some extent MySQL The tradeoff between performance and concurrency , Gap locks are used at certain transaction isolation levels .
For statements that use unique index lookup , You won't use the gap lock ( Unless the search criteria contain only a few columns with a unique index of multiple columns ) For example, in the following sentence , If the column id There's a unique index , Only one is used id=100 The record lock of , And it doesn't prevent other conversations from inserting in the previous gap .
1 SELECT * FROM child WHERE id = 100 ;
But if id There is no index or has a non unique index , The sentence will lock the gap before .
Different transactions can hold conflicting locks on the same gap . for example , Business A A shared gap lock that holds a gap (gap S-lock), Simultaneous transaction B You can hold exclusive gap locks on the same gap (gap X-lock). Because if an index record is deleted , Gap locks held by different transactions for this record must be merged .
stay InnoDB in , The mutual exclusion of gap locks is largely suppressed , The only function of gap lock is to prevent transactions from inserting into the gap . Gap locks can coexist . The same transaction can hold different gaps at the same time . There is no difference between shared gap locks and exclusive gap locks . There is no conflict between them , And it has the same effect .
The gap lock can be explicitly disabled . By changing the transaction isolation level to READ COMMITTED
Or turn on the system variable innodb_locks_unsafe_for_binlog
( At present, it has been abandoned )
To disable . In these cases , Gap locks are no longer used for search and index scanning , It is only used for foreign key constraint checking and duplicate key checking .
There are also some of the above two settings “ side effect ”. stay MySQL To calculate the where After the condition , Record locks on mismatched rows will be released . about UPDATE sentence ,InnoDB Will execute a “ Semi consistency ” read , So that MySQL Return the latest submitted version number , Used to pick out a match WHERE The conditions are right .
It is mentioned in the last paragraph about
READ COMMITTED
Will release mismatched record locks for experiments .SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED; start TRANSACTION; -- id Not indexed SELECT * from test.t where id =1 for update;
Monitor output :
RECORD LOCKS space id 3 page no 6 n bits 320 index >GEN_CLUST_INDEX of table `test`.`t` trx id 2178 lock_mode X locks rec but not gap
You can see that although the cluster index goes , But the transaction eventually only holds the lock of the records that match the filter .
Isolation level changed back toREAD COMMITTED
, Execute the same statement .
Monitor output :3 lock struct(s), heap size 1136, 372 row lock(s) RECORD LOCKS space id 3 page no 6 n bits 320 index GEN_CLUST_INDEX of table `test`.`t` trx id 2179 lock_mode X
Hold the full table record lock .
Adjacent key lock
Adjacent key lock is a combination of record lock and gap lock on gap before index record .
InnoDB When searching or scanning the index , Set sharing for every index record encountered / An exclusive lock , In this way, the row lock is realized . therefore , The so-called row lock is actually a record lock . The adjacent key lock not only locks an index record , It will also affect what happened before the record “ The gap ”. In other words, the adjacent key lock can be expressed as a record lock plus a gap lock before recording . If a session holds records R Sharing of index on / Exclusive record lock , For other conversations , If the inserted value is less than the record R Upper index value ( Sort by index ), You can't insert it directly , You have to wait for the lock to release .
Imagine an index that contains values 10 , 11 , 13 , 20 . Then all possible adjacent key lock intervals are as follows , The parenthesis means not to contain , Square brackets stand for :
( Negative infinity , 10 ]
( 10 , 11 ]
( 11 , 13 ]
( 13 , 20 ]
( 20 , It's just infinite )
For the last interval , The adjacent key lock locks a gap greater than the maximum index value , And use a virtual record to represent the upper bound . This upper bound is not the actual index value , So in fact, the adjacent key lock does not carry a record lock , Only gap locks greater than the maximum of the current index .
InnoDB By default REPEATABLE READ
Isolation level . At this level ,InnoDB Use adjacent key locks when searching and scanning indexes , Used to avoid hallucinations .
If you want to view adjacent key locks in the current database , perform SHOW ENGINE INNODB STATUS
, InnoDB The monitor will output the following :
RECORD LOCKS space id 58 page no 3 n bits 72 index `PRIMARY` of table `test`.`t`
trx id 10080 lock_mode X
Record lock, heap no 1 PHYSICAL RECORD: n_fields 1 ; compact format; info bits 0
0 : len 8 ; hex 73757072656 d756d; asc supremum;;
Record lock, heap no 2 PHYSICAL RECORD: n_fields 3 ; compact format; info bits 0
0 : len 4 ; hex 8000000 a; asc ;;
1 : len 6 ; hex 00000000274 f; asc 'O;;
2 : len 7 ; hex b60000019d0110; asc ;;
Adjacent key lock is mainly used to solve the problem of magic line : The result of the other transaction insert caused by two inconsistent operations . The adjacent key lock solves this problem , It can help the application to realize the unique insert value ( Lock reading -> Get adjacent key lock -> Only this session is allowed to insert ).
Insert intention lock
Insert intent lock is a special kind of gap lock , Get... Before performing row insertion in an insert operation . Used to mark the intention of insertion , In this way, multiple transactions can be inserted in the same interval , If the other party is not in the same index value position, insert , You don't have to wait for each other . for example , The current index values are 4 and 7 . Two transactions are ready to insert 5 and 6 , Before acquiring the exclusive lock of the inserted row , They will get each other 4 to 7 The insert intention lock between the gaps , And they don't block each other , Because the insert values do not conflict .
Let's use an example to demonstrate that a transaction before acquiring an exclusive lock on a record , The process of obtaining the insert intent lock . Two clients are involved in the case , Namely A and B.
client A Create a table , Contains two index values ( 90 and 102), And then start a transaction , obtain ID Greater than 100 Exclusive lock for all records of . The exclusive lock will contain a id<102 The gap lock :
Adjacent key lock , Section =(100,102]
mysql> CREATE TABLE child (id int( 11 ) NOT NULL, PRIMARY KEY(id)) ENGINE=InnoDB;
mysql> INSERT INTO child (id) values ( 90 ),( 102 );
mysql> START TRANSACTION;
mysql> SELECT * FROM child WHERE id > 100 FOR UPDATE;
+-----+
| id |
+-----+
| 102 |
+-----+
client B Start a transaction , Execute the command to insert records in the gap . The transaction will first acquire an insert intent lock , Then wait for the exclusive lock to be acquired .
mysql> START TRANSACTION;
mysql> INSERT INTO child (id) VALUES ( 101 );
If you want to view the insert intent lock in the current database , perform SHOW ENGINE INNODB STATUS
, InnoDB The monitor will output the following :
1 RECORD LOCKS space id 31 page no 3 n bits 72 index `PRIMARY` of table `test`.`child`
2 trx id 8731 lock_mode X locks gap before rec insert intention waiting
3 Record lock, heap no 3 PHYSICAL RECORD: n_fields 3 ; compact format; info bits 0
4 0 : len 4 ; hex 80000066 ; asc f;;
5 1 : len 6 ; hex 000000002215 ; asc " ;;
6 2 : len 7 ; hex 9000000172011 c; asc r ;;...
Self increasing lock
Autoincrement lock is a special kind of table lock , When a transaction performs an insert in a table with an auto increment column, it acquires an autoincrement lock . In the simplest case , If a transaction is inserting data into a table , Other transactions must wait for their insertion to complete before executing their own inserts , To ensure that the primary key values are continuous .
Configuration item innodb_autoinc_lock_mode
The algorithm used to control the use of autoincrement locks , To help you balance the predictability of auto increment sequences with the concurrency of inserts .
Assertion locks for spatial indexes
InnoDB Supports indexing of spatial rows .
When dealing with operations involving spatial indexes , The adjacent key is locked in REPEATABLE READ
and SERIALIZABLE
Two isolation levels don't work well . Because there is no absolute collation for multidimensional data , So it's not clear who is “ Adjacent bond ”.