当前位置:网站首页>When does MySQL use table locks and row locks?
When does MySQL use table locks and row locks?
2022-07-24 07:46:00 【51CTO】
Hello everyone , I'm brother Shu .
MySQL Innodb The lock of can be said to be the concurrency basis of the execution engine , Only with locks can the consistency of data be guaranteed . as everyone knows , We all know Innodb There's a global lock 、 Table lock 、 There are three types of row level locks , But you know when to use the watch lock , When will I use the lock ? Although the MySQL The knowledge points of are quite familiar , But at first I saw this problem , Brother Shu is also a little confused , I really haven't thought about it from this perspective . You can do it for a while 1 Think about the answer in minutes , Later, I will show you how to clarify this problem .
For this question , I can only think of some fragments roughly , for example :
- For table level locks , When executed DDL Statement to modify the table structure , Can use watch level lock .
- For row level locks , Generally, row level locks are used by default , It seems that you need an index to match .
The above is my rough answer , I wonder if the answer you think is the same as me ? Now let me take you to review MySQL Your lock !
For databases , The lock range can be divided into :
- Global lock
- Table lock
- Row-level locks
Global lock
Global lock is to lock the whole database instance . 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 ( Including building tables 、 Modify table structure, etc ) Commit statements for and update class transactions . You can understand it as , The global lock basically locks all the change statements of the data .
The typical application scenario of global lock is full database logical backup , That is, every table in the whole library select Come out and save . As mentioned above, global lock will lock all change statements , But it's just for MyISAM In terms of storage engines . about Innodb for , It can take advantage of MVCC Realize the consistent view of data , Thus, the data backup of the whole library can be realized without locking the whole library .
Table lock
Table level locks can be divided into : Table locks 、 Metadata lock 、 There are three kinds of intentional locks .
Table locks
Table locks , As the name suggests, it is to lock a table .
When will the watch lock be used ?
Generally, the corresponding storage engine does not have row level locks ( for example :MyIASM), Or corresponding SQL Statement does not match index .
In the first case , Because the corresponding storage engine does not support row locks , Therefore, it can only be achieved by using coarser grained locks , It's easy to understand .
In the second case , If the storage engine supports row locks , But the corresponding SQL No index is used , At this time, the whole table will also be scanned , Then the table lock will also be used at this time . For example, the following statement does not specify a query column , Or the query column is specified but the index is not used , Then the whole table will be locked directly .
The index mentioned above , It can be said that it is the key to judge whether row level locks will be used . But I have a question : If the index is used in the query or update , But there are a lot of data to query or update , Of the whole table 80% Even more , At this time, I can use a watch lock , Or a lock ? It's an interesting question , Interested friends get a test table to verify , We'll talk about this later when we have a chance .
Metadata lock
Metadata , It refers to our table structure and these metadata . Metadata lock (Metadata Lock) Naturally, execution DDL Table structure change statement , We put a lock on the watch .
When will metadata lock, a table level lock, be used ?
When we add, delete, modify, and query a table , Will add MDL Read the lock ; When we want to make changes to the table structure , Will add MDL Write lock .
Intent locks
Intent locks , In essence, it is the product of space for time , It is a thing to improve the efficiency of row lock .
stay InnoDB in , When we lock a record , To improve concurrency , Usually just lock this line of records , Instead of locking the entire table . And when we need to add X When it's locked , We need to traverse the records of the whole table , If every record is not locked , Before you can add X lock . And this traversal process is very time-consuming , At this time, there is the birth of intention lock .
Intention lock is actually to mark whether the table is locked , If a record is locked , Then you must obtain the intention lock of the table . So when we need to judge whether the records of this table are locked , Just judge the intention lock directly , Reduced traversal time , Improved efficiency , It is a typical practice of trading space for time .
So when will intention locks be used ?
It's simple , When locking row records in the table , Will use intention lock .
Row-level locks
time and again , Finally came to the row lock .
What you need to know is , Row level locks are storage engine level locks , Storage engine support is required to be effective . at present MyISAM The storage engine does not support row level locks , and Innodb The storage engine supports row level locking . And global lock 、 Table lock , It is MySQL The level supports the lock .
When will row level locks be used ?
When the addition, deletion, modification and query match the index ,Innodb Can use row level lock .
If there is no match, the index cannot be found , Then table level locks will be used directly .
summary
At the end of the article , Let's review the questions raised at the beginning :Innodb When to lock the watch , When to use row lock ?
Table level locks include : Table locks 、 Metadata lock 、 Intent locks .
For watch locks , When the storage engine does not support row level locking , Use the watch lock .SQL When the statement does not match the index , Use the watch lock .
For metadata locks , When adding, deleting, modifying and checking the table , Will add MDL Read the lock . When you make changes to the table structure , Will add MDL Write lock .
For intention lock , When locking row records in a table , Will use intention lock .
For row level locks , When the addition, deletion, modification and query match the index , Can use row level lock .
Last , Attached is the mind map of this article , It is convenient for you to review the content of this article ~
If you like today's sharing , Remember to support me with one button three times !

边栏推荐
- numpy.concatenate
- DOM operation of JS -- style operation
- Math。 Round, numeric rounding, underlying code parsing
- Hcip day 10 notes
- Arduino在不同主频下稳定支持的TTL串口速率
- Source code analysis of Nacos configuration center
- Vertex buffer and shader (the cherno + leranopongl) notes
- Simple Gateway - intranet server safely obtains external network data
- 【sklearn】tree.DecisionTreeClassifier
- Advanced part of Nacos
猜你喜欢

无法自动装配,未找到“RedisTemplate类型的Bean

Hcip day 8 notes

OpenGL camera and periodic review

Requests crawler multi page crawling to KFC restaurant location

Advanced part of C language IV. detailed explanation of user-defined types

Arduino在不同主频下稳定支持的TTL串口速率

2021-06-03pip error valueerror: unable to find resource t64.exe in package pip_ vendor.distlib

Oauth2==sso three protocols. Oauth2 four modes

Debug NO2 check for errors according to the process

Problems encountered in inserting large quantities of data into the database in the project
随机推荐
Detailed explanation of VAO
Influxdb unauthorized access & CouchDB permission bypass
基于VSCode聊聊编译器那些事儿
Debug No4 use renderdoc to troubleshoot bugs
Selenium basic knowledge multi window processing
Jersey2.25.1 integration freemaker
Hcip 13th day notes
[cloud native] MySQL index analysis and query optimization
Debug NO2 check for errors according to the process
Introduction to C language v First understanding pointer VI. first understanding structure
C language to achieve mine sweeping game
【云原生】MySql索引分析及查询优化
Example of dictionary
Appium use
Advanced part of C language VI. file operation
【sklearn】PCA
Jetson AgX Orin source change
Harbor2.2 quick check of user role permissions
The difference between session and cookie
剑指offer专项突击版第8天