当前位置:网站首页>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
边栏推荐
- Change vs theme and set background picture
- Wei Pai: the product is applauded, but why is the sales volume still frustrated
- String abc = new String(“abc“),到底创建了几个对象
- 2.C语言矩阵乘法
- There is always one of the eight computer operations that you can't learn programming
- JS interview questions (I)
- CorelDRAW plug-in -- GMS plug-in development -- Introduction to VBA -- GMS plug-in installation -- Security -- macro Manager -- CDR plug-in (I)
- 【手撕代码】单例模式及生产者/消费者模式
- Arduino+ds18b20 temperature sensor (buzzer alarm) +lcd1602 display (IIC drive)
- View UI plus released version 1.3.1 to enhance the experience of typescript
猜你喜欢
3. C language uses algebraic cofactor to calculate determinant
C language to achieve mine sweeping game (full version)
7.数组、指针和数组的关系
仿牛客技术博客项目常见问题及解答(一)
Quickly generate illustrations
C language Getting Started Guide
Conceptual model design of the 2022 database of tyut Taiyuan University of Technology
Cookie和Session的区别
(原创)制作一个采用 LCD1602 显示的电子钟,在 LCD 上显示当前的时间。显示格式为“时时:分分:秒秒”。设有 4 个功能键k1~k4,功能如下:(1)k1——进入时间修改。
2.C语言初阶练习题(2)
随机推荐
Pit avoidance Guide: Thirteen characteristics of garbage NFT project
4.分支语句和循环语句
仿牛客技术博客项目常见问题及解答(二)
抽象类和接口的区别
透彻理解LRU算法——详解力扣146题及Redis中LRU缓存淘汰
杂谈0516
5. Function recursion exercise
[the Nine Yang Manual] 2020 Fudan University Applied Statistics real problem + analysis
The latest tank battle 2022 full development notes-1
[while your roommate plays games, let's see a problem]
View UI Plus 发布 1.3.0 版本,新增 Space、$ImagePreview 组件
Arduino+ds18b20 temperature sensor (buzzer alarm) +lcd1602 display (IIC drive)
Common method signatures and meanings of Iterable, collection and list
8.C语言——位操作符与位移操作符
IPv6 experiment
最新坦克大战2022-全程开发笔记-1
vector
2. C language matrix multiplication
3. Number guessing game
fianl、finally、finalize三者的区别