当前位置:网站首页>MySQL practice 45 [global lock and table lock]
MySQL practice 45 [global lock and table lock]
2022-07-03 03:00:00 【Little fish 2020】
List of articles
06 | Global lock and table lock : How can adding a field to a table be so much of a hindrance ?
The original intention of database lock design is to deal with concurrent problems . Shared resources as multiple users , When concurrent access occurs , Database needs to control access rules of resources reasonably . Lock is an important data structure to implement these access rules .
According to the range of locking ,MySQL The locks inside can be roughly divided into global locks 、 Table level lock and row lock
Global lock
Global lock is to lock the whole database instance
FTWRL
Flush tables with read lock
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 .
Table lock
MySQL There are two types of lock at the inner table level : One is watch lock , One is metadata lock (meta data lock,MDL)
The syntax of table lock is
lock tables Table name read/write
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
for instance , If in a thread A In the implementation of lock tables t1 read, t2 write; This statement , Then other threads
Write t1、 Reading and writing t2 All of the statements will be blocked . meanwhile , Threads A In execution unlock tables Before , Can only hold
Line reading t1、 Reading and writing t2 The operation of . Linking t1 Not allowed , Naturally, you can't access other tables
Table level lock is MDL(metadata lock)
MDL You don't need to explicitly use , When accessing a table, it will be automatically added .MDL The role of is , Ensure the correctness of reading and writing .
stay MySQL 5.5 The version introduces MDL,
When adding, deleting, modifying and querying a table , Add MDL Read the lock ;
When you want to make structural changes to a table , Add MDL Write lock
Read locks are not mutually exclusive , So you can have multiple threads to add, delete, modify and query a table at the same time , Read-write lock 、 Write locks are mutually exclusive , To ensure the security of the operation to change the structure of the table . therefore , If two threads want to 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
In the transaction MDL lock , Apply at the beginning of statement execution , However, the statement is not released immediately after completion , It will wait until the whole transaction is committed before releasing
stay MySQL Of information_schema Library innodb_trx In the table , You can find the current transaction in progress . If you want to
do DDL The changed table happens to have a long transaction running , Consider suspending DDL, perhaps kill Drop this long business .
stay alter table Set the waiting time in the statement , If you can get it within the specified waiting time MDL It's better to write lock , Take no
Do not block the following business statements , Give up first . Then the developer or DBA Then repeat this through the retry command
A process .
ALTER TABLE tbl_name NOWAIT add column ...
ALTER TABLE tbl_name WAIT N add column ...
Online DDL The process is like this :
- take MDL Write lock
- Drop into MDL Read the lock
- Really doing DDL
- Upgrade to MDL Write lock
- Release MDL lock
1、2、4、5 If there are no lock conflicts , Execution time is very short . The first 3 Step to take up the DDL Most of the time , During this period, this watch
Data can be read and written normally , So it's called “online ”
Reference resources
边栏推荐
- Docker install redis
- Super easy to use logzero
- Sous - système I2C (IV): débogage I2C
- HW-初始准备
- Thunderbolt Chrome extension caused the data returned by the server JS parsing page data exception
- TCP handshake three times and wave four times. Why does TCP need handshake three times and wave four times? TCP connection establishes a failure processing mechanism
- random shuffle注意
- Apple releases MacOS 11.6.4 update: mainly security fixes
- [shutter] monitor the transparency gradient of the scrolling action control component (remove the blank of the top status bar | frame layout component | transparency component | monitor the scrolling
- Deep learning: multi-layer perceptron and XOR problem (pytoch Implementation)
猜你喜欢
Kubernetes cluster log and efk architecture log scheme
Kubernetes family container housekeeper pod online Q & A?
Creation and destruction of function stack frame
Add automatic model generation function to hade
Matlab tips (24) RBF, GRNN, PNN neural network
Force deduction ----- the minimum path cost in the grid
HTB-Devel
SqlServer行转列PIVOT
Can netstat still play like this?
sql server数据库添加 mdf数据库文件,遇到的报错
随机推荐
Introduction to cron expression
模糊查询时报错Parameter index out of range (1 > number of parameters, which is 0)
Counter统计数量后,如何返回有序的key
Use of check boxes: select all, deselect all, and select some
JMeter performance test JDBC request (query database to obtain database data) use "suggestions collection"
[C语言]给账号密码进行MD5加密
Tensorflow to pytorch notes; tf. gather_ Nd (x, y) to pytorch
你真的懂继电器吗?
"Analysis of 43 cases of MATLAB neural network": Chapter 43 efficient programming skills of neural network -- Discussion Based on the characteristics of the new version of MATLAB r2012b
[shutter] monitor the transparency gradient of the scrolling action control component (remove the blank of the top status bar | frame layout component | transparency component | monitor the scrolling
Yiwen takes you to know ZigBee
敏捷认证(Professional Scrum Master)模拟练习题
函数栈帧的创建与销毁
docker安装redis
SqlServer行转列PIVOT
The base value is too large (the error is marked as "08") [duplicate] - value too great for base (error token is'08') [duplicate]
Chart. JS multitooltip tag - chart js multiTooltip labels
一文带你了解 ZigBee
I2C subsystem (I): I2C spec
Le processus de connexion mysql avec docker