Lock in InnoDB - Notes on MySQL 8.0 official documents (1)

d1zzyboy 2020-11-08 00:44:11
lock innodb notes mysql official


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 UPDATE

Monitor 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 to READ 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 ”.

版权声明
本文为[d1zzyboy]所创,转载请带上原文链接,感谢

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云