当前位置:网站首页>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 .
边栏推荐
- 一文带你搞懂 JWT 常见概念 & 优缺点
- Analysis of the concept of metacosmic system
- GridLayout evenly allocate space
- Read AFN through - from the creation of manager to the completion of data parsing
- Read the configuration, explain the principle and read the interview questions. I can only help you here...
- Continuously improve the overall performance of adaoracle Oracle Oracle
- Record learning of hystrix knowledge --20210929
- Common APIs and exception mechanisms
- 什么是骨干网
- Lifeifei's team applied vit to the robot, increased the maximum speed of planning reasoning by 512 times, and also cued hekaiming's MAE
猜你喜欢

心楼:华为运动健康的七年筑造之旅

加密潮流:时尚向元宇宙的进阶

绕过技术聊'跨端'......
Take you to the open source project of smart home: the preliminary configuration of zhiting home cloud and home assistant+ homebridge

Interviewer: your resume says you are proficient in mysql, so you say cluster / Union / overlay index, table return, index push down

Detailed explanation of IVX low code platform series -- Overview (I)
Inter thread synchronization semaphore control

Read the configuration, explain the principle and read the interview questions. I can only help you here...

mysql整体架构和语句的执行流程

Navicat premium 15 for MAC (database development tool) Chinese version
随机推荐
Leetcode topic [array]-34- find the first and last positions of elements in a sorted array
Read the configuration, explain the principle and read the interview questions. I can only help you here...
Process control and method
数据存储和传输文件之XML使用和解析详解
Final, override, polymorphic, abstract, interface
Flutter textfield setting can input multiple lines
绕过技术聊'跨端'......
Servlet details
Div element
Load local cifar10 dataset
Continuously improve the overall performance of adaoracle Oracle Oracle
The style of the mall can also change a lot. DIY can learn about it!
10 Super VIM plug-ins, I can't put them down
Alvaria宣布客户体验行业资深人士Jeff Cotten担任新首席执行官
Multiple decorators decorate a function
What are some tricks that novice programmers don't know?
一行代码可以做什么?
Preliminary understanding of JVM
The database records are read through the system time under the Android system, causing the problem of incomplete Reading Records!
Common APIs and exception mechanisms