当前位置:网站首页>Read mysql45 - a simple understanding of global locks and table locks
Read mysql45 - a simple understanding of global locks and table locks
2022-06-25 16:25:00 【Hurry to Friday】
Global lock
Global lock is to lock the entire database instance ,mysql A global read lock command is provided :Flush tables with read lock (FTWRL)
When you need to keep the entire library in a read-only state , You can use the entire command , When the entire command is executed , All write operations are 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 .
The background of global lock is that the database needs to be backed up , At this time FTWRL Command prevents other threads from writing to the database , At this time, the entire library is in a read-only state .
If the backup is not locked, the result is :
Back up before purchasing the course , But only backup A Of account, Will be A The account of 200 Backup , Then back up after purchasing the course course,
After that, if you use the backup data to restore the database , It will lead to A Of account still 200, however coures There will be one more course ; It's not right .
If you back up course, Purchase course , Backup again account, It will lead to course still null, however account Less .
So if the backup is not locked , This will cause problems in the data obtained from the backup data . If during the backup operation , Open transaction , At the end of the backup , Resubmission complete , To ensure that the backup data is correct .
The official logic backup tool is mysqldump. When mysqldump Using parameter –single-transaction When , guide A transaction is started before the data , To ensure a consistent view . And because the MVCC Support for , The data in this process is Can be updated normally .
For the storage engine MylSAM This engine does not support transactions , You need to use it FTWRL The command ensures that the entire library is read-only during backup .
set global readonly=true s You can also make the database read-only , There are two differences :
- , In some systems ,readonly The value of will be used to do other logic , For example, it is used to judge whether a database is the primary database or the standby database library . therefore , modify global The way the variables affect the surface is larger , Not recommended .
- There are differences in exception handling mechanisms . If you execute FTWRL After the command, due to the abnormal disconnection of the client , that MySQL This global lock will be released automatically , The whole library can be updated normally . Instead, set the entire library to readonly after , If the client has an exception , Then the database will remain readonly state , This will lead to the whole The library is not writable for a long time , High risk .
Table lock
There are two kinds of table level locks : One is watch lock ; The other is metadata lock (meta data lock,MDL).
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 Syntax will restrict the reading and writing of other threads Outside , It also defines the next operation objects of this thread . for instance , If in a thread A In the implementation of lock tables t1 read, t2 write; This statement , Other threads write t1、 read Write t2 All of the statements will be blocked . meanwhile , Threads A In execution unlock tables Before , It can only be read t1、 Reading and writing t2 Gymnastics do . Linking t1 Not allowed , Naturally, you can't access other tables .
Another type of table level lock is another MDL M (metadata lock) m .MDL You don't need to explicitly use , When accessing a table, it will be Automatically add .
When modifying the table structure , Add or modify fields , When deleting fields , Will add one to the watch MDL Write lock , Do not write data at this time ;
When modifying data , Add data or update data , When deleting data , Add a to the table MDL Read the lock , The data can be read only after the operation is completed .
- Read locks are not mutually exclusive , So you can have multiple threads to add, delete, modify and query a table at the same time .
- Between read lock and write lock 、 Write locks and write locks are mutually exclusive , To ensure the security of the operation to change the structure of the table . therefore , If there are two lines The program should add fields to a table at the same time , One of them can't be executed until the other has finished .
Add fields to a table , Or modify the fields , Or the Caucasian , Need to scan the data of the whole table .
sessionA Start... First , Query data , then sessionB start-up , Query data , next sessionC start-up , Modify table structure , It was blocked at this time , It will lead to sessionD Also blocked .
sessionC The blocking operation may be due to sessionA The read operation of is not over yet . Because read locks and write locks are mutually exclusive , So in sessionA Of MDL Before the read lock is released ,sessionC No MDL Write the lock .
If the query statement of this table is very high frequency , And the client has a retry mechanism , Is to create a new one after the query timeout session Query again , This will cause the thread pool of this library to be full . So when modifying the table structure or data , If there is a long transaction , And never submitted , We need to consider first kill Drop the transaction or suspend the modification operation .
边栏推荐
- MT60B1G16HC-48B:A美光内存颗粒FBGA代码D8BNK[通俗易懂]
- This article will help you understand the common concepts, advantages and disadvantages of JWT
- cmd。。。。。。
- Once the code was encrypted by the company's computer, the compilation failed
- What exactly is a handler
- Introduction to MgO 256gb NAND flash chip
- Deadlock, thread communication, singleton mode
- Leetcode topic [array]-34- find the first and last positions of elements in a sorted array
- Take you to the open source project of smart home: the preliminary configuration of zhiting home cloud and home assistant+ homebridge
- [problem solving] dialogfragment can not be attached to a container view
猜你喜欢
GridLayout evenly allocate space
Alvaria announces Jeff cotten, a veteran of the customer experience industry, as its new CEO
Don't underestimate the integral mall, its role can be great!
DOM event flow, event delegate
Gold three silver four, an article to solve the resume and interview
Introduction to database transactions
不要小看了积分商城,它的作用可以很大!
Prototype chain analysis
Nsurlsession learning notes (III) download task
数字经济时代文化消费新特征
随机推荐
[problem solving] dialogfragment can not be attached to a container view
XML usage and parsing of data storage and transmission files
Flutter assembly
普通人的2022春招总结(阿里、腾讯offer)
The textfield is encapsulated by the flutter itself, which causes the data display to be disordered when the data in the list is updated.
Describe your understanding of the evolution process and internal structure of the method area
Lifeifei's team applied vit to the robot, increased the maximum speed of planning reasoning by 512 times, and also cued hekaiming's MAE
2021, committed to better development
Inter thread synchronization semaphore control
_ 17 collection overview
Mixed density network (MDN) for multiple regression explanation and code example
Swift responsive programming
Summary of 2022 spring moves of ordinary people (Alibaba and Tencent offer)
Continuously improve the overall performance of adaoracle Oracle Oracle
What is the NFT digital collection?
How to reload the win10 app store?
什么是骨干网
What is backbone network
Shuttle pop-up returns to the upper level
The release of autok3s v0.5.0 continues to be simple and friendly