当前位置:网站首页>MySQL lock summary (comprehensive and concise + graphic explanation)
MySQL lock summary (comprehensive and concise + graphic explanation)
2022-07-06 13:36:00 【Li bohuan】
Catalog
Classification of locks
According to the range of locking ,MySQL The locks inside can be roughly divided into global locks 、 Table level lock and row level lock , As shown in the figure :
Specific subdivisions of table level locks : Specific subdivision of row level lock :
One , Global lock
The cost and locking time are between table level lock and row level lock ; A deadlock occurs ; Locking granularity is between table level lock and row level lock , The concurrency is average
seeing the name of a thing one thinks of its function , A global lock is a lock on the whole Database instance locking .MySQL It provides a way to add global read lock , The order is Flush tables with read lock (FTWRL). When you need to make the entire library read-only , You can use this command , After that, the following statements of other threads will be blocked : Data update statement ( Data addition, deletion and modification )、 Data definition statement ( Include Build table 、 Modify table structure, etc ) Commit statements for and update class transactions .
A typical use scenario for global locks is , do Full library logical backup . That is to say, every table in the whole library select Come out and save it as text .
Two , Table lock
Low overhead , Locked fast ; A deadlock will not occur ; Large locking size , The highest probability of lock collisions , Lowest degree of concurrency
1. Table locks
Watch locks can be divided into the following two categories : The syntax of table lock is lock tables …read/write. And FTWRL similar , It can be used unlock tables Active release lock , It can also be released automatically when the client is disconnected . We need to pay attention to ,lock tables The syntax will restrict the reading and writing of other threads , It also defines the next operation objects of this thread .
1.1 Table share read lock
Once a client adds a shared read lock to the table , Then it can only read but not write , It will not block the read operation of other clients, but the write operation of other clients , As shown in the figure below :
1.2 Table Write Lock
Once a client adds a write lock to the table , that It can read and write the table itself , But it will block Read and write operations of other clients , As shown in the figure below :
2. Metadata lock
MDL(metadata lock) Metadata lock , So-called Metadata is data that describes data , Here it can be understood as the structure of the table . It does not require explicit use , When accessing a table, it will be automatically added .MDL The role of is , Ensure the correctness of reading and writing . You can imagine , If a query is traversing a Table data , During execution, another thread changes the table structure , Delete a list , Then the result obtained by the query thread does not match the table structure , Surely not .
therefore , stay MySQL 5.5 The version introduces MDL, When adding, deleting, modifying and querying a table , Add MDL Read the lock ( Shared lock ); When you want to make structural changes to a table , Add MDL Write lock ( Exclusive lock ). Read locks are compatible , Write lock, read lock and write lock are mutually exclusive . As shown in the figure below , The write lock is only applied when the table structure needs to be changed (EXCLUSIVE).
3. Intent locks
To allow row locks and table locks to coexist , Implement multi granularity lock mechanism ,InnoDB There are also two internal intention locks . Intention lock is a kind of table lock , The granularity of locking is the whole table , Divided into intention sharing lock (IS) And intent exclusive lock (IX) Two types of . When a transaction adds a shared lock or exclusive lock to the data row of the table , At the same time, an identifier will be set for the table , It means that there is already a row lock , When other transactions want to lock the table , There is no need to judge row by row whether row locks may conflict with table locks , Directly reading this sign can determine whether you should lock your watch , This sign is the intention lock . The main purpose is to improve the efficiency of adding table lock .
3.1 Intention sharing lock (IS)
Before applying a shared lock to the entire table , You need to obtain the intention sharing lock first . By statement select...lock in share mode add to .
3.2 Intention exclusive lock (IX)
Before locking the whole watch , You need to get the intention exclusive lock first . from insert,update,delete,select...for update add to .
The compatibility and mutual exclusion of intentional locks and other locks are shown in the following figure , In a nutshell , Intent locks are compatible , Intentional exclusive locks and locks other than intentional locks are mutually exclusive , Exclusive lock (X) And any lock are mutually exclusive .
3、 ... and , Row-level locks
Spending big , Lock the slow ; A deadlock occurs ; Locking granularity minimum , The lowest probability of lock collisions , The highest degree of concurrency . Row level locking is achieved by locking the index items on the index .
1. Row lock (record lock)
Row locks are divided into shared locks (S) And exclusive locks (X). Row lock is a lock that locks a single record , Prevent other matters from affecting it update,delete The operation of , stay RR and RC All isolation levels support . As shown in the figure :
1.1 Shared lock
Shared lock (S) It's also called read lock , When a thread acquires a read lock , It will block other users' access to this row of data Write operations ( That is, the exclusive lock that prevents other transactions ), But it will not prevent other users from Read operations ( Shared lock )
1.2 Exclusive lock
Exclusive lock (X) It's also called write lock , When a thread acquires a write lock , It will block other users' access to this row of data Reading and writing (update,delete, operation , That is, other transactions cannot obtain the data of this row Shared lock and exclusive lock ( Only after obtaining the lock can the data be operated )
You may have a good understanding of shared locks , It means that multiple transactions can only read data and cannot change data . An exclusive lock refers to a transaction after adding an exclusive lock to a row of data , No other locks can be added to other transactions ( Exclusivity is reflected here ).mysql InnoDB The default data modification statement of the engine :update,delete,insert Will automatically add an exclusive lock to the data involved ,select Statement will not add any lock type by default , If an exclusive lock is added, it can be used select …for update sentence , Add shared lock to use select … lock in share mode sentence . Therefore, data rows with exclusive locks cannot be modified in other transactions , It can't pass for update and lock in share mode Query data by lock , But you can go straight through select …from… Query data , Because normal queries don't have any locking mechanism .
InnoDB The row lock of is achieved by locking the index , If you don't retrieve data by index criteria , that InnoDB Lock all records in the table , At this point, it will be upgraded to Table locks .
2. Clearance lock (Gap lock)
Gap lock is to lock the gap between index records , Prevent other transactions from going on at a certain gap insert The operation of , It produces unreal reading . stay RR All isolation levels support . As shown in the figure :
3. Temporary key lock (Next-key lock)
Key lock is a combination of row lock and clearance lock , At the same time, lock the gap between data and data , stay RR In the isolation level of . As shown in the figure :
By default ,InnoDB stay RR The transaction isolation level of ,InnoDB Will use next-key lock Lock for search and index scan , To prevent unreal reading .
- Equivalent query on Index ( unique index ), When locking records that do not exist , Optimized for clearance lock .
- Equivalent query on Index ( General index ), When traversing to the right, the last value does not meet the query requirements ,next-key lock Degenerate to clearance lock .
- Range query on Index ( unique index )-- Access to the first value that does not satisfy the condition .
The first point I believe is easy to understand . The second point to note is the general index , Ordinary indexes are not unique , Therefore, it is possible to insert records with the same index value . As shown in the figure below , If I insert another index as 18 The data of , Then it will be right at this time 18-29 This gap and 16-18 This gap is locked (gap lock).
Be careful : The only purpose of a gap lock is to prevent other transactions from inserting gaps . Gap locks can coexist , A gap lock adopted by one transaction will not prevent another transaction from adopting a gap lock on the same gap .
If it helps you , You can give bloggers some praise and attention ~~
Reference material : Black horse programmer MySQL Database entry to mastery , from mysql The installation to mysql senior 、mysql Optimize all inclusive _ Bili, Bili _bilibili
边栏推荐
- 【九阳神功】2021复旦大学应用统计真题+解析
- Voir ui plus version 1.3.1 pour améliorer l'expérience Typescript
- Questions and answers of "basic experiment" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
- 4.分支语句和循环语句
- 5.函数递归练习
- View UI plus released version 1.3.0, adding space and $imagepreview components
- 2. Preliminary exercises of C language (2)
- View UI Plus 发布 1.1.0 版本,支持 SSR、支持 Nuxt、增加 TS 声明文件
- 9. Pointer (upper)
- 为什么要使用Redis
猜你喜欢
Questions and answers of "Fundamentals of RF circuits" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
Summary of multiple choice questions in the 2022 database of tyut Taiyuan University of Technology
Tyut Taiyuan University of technology 2022 "Mao Gai" must be recited
C language Getting Started Guide
hashCode()与equals()之间的关系
7. Relationship between array, pointer and array
Smart classroom solution and mobile teaching concept description
2.C语言矩阵乘法
5.函数递归练习
5.MSDN的下载和使用
随机推荐
[the Nine Yang Manual] 2019 Fudan University Applied Statistics real problem + analysis
E-R graph to relational model of the 2022 database of tyut Taiyuan University of Technology
凡人修仙学指针-2
Arduino+ water level sensor +led display + buzzer alarm
Implement queue with stack
13 power map
Common method signatures and meanings of Iterable, collection and list
Aurora system model of learning database
4.二分查找
杂谈0516
View UI Plus 发布 1.1.0 版本,支持 SSR、支持 Nuxt、增加 TS 声明文件
Rich Shenzhen people and renting Shenzhen people
1.C语言矩阵加减法
(超详细二)onenet数据可视化详解,如何用截取数据流绘图
Alibaba cloud microservices (II) distributed service configuration center and Nacos usage scenarios and implementation introduction
5.函数递归练习
FileInputStream和BufferedInputStream的比较
稻 城 亚 丁
仿牛客技术博客项目常见问题及解答(三)
Redis实现分布式锁原理详解