当前位置:网站首页>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
边栏推荐
- Use cve-2021-43893 to delete files on the domain controller
- I2C 子系统(一):I2C spec
- Edit and preview in the back pipe to get the value writing method of the form
- Kubernetes family container housekeeper pod online Q & A?
- [fluent] future asynchronous programming (introduction | then method | exception capture | async, await keywords | whencomplete method | timeout method)
- I2C 子系统(四):I2C debug
- How to limit the size of the dictionary- How to limit the size of a dictionary?
- Joking about Domain Driven Design (III) -- Dilemma
- Update and return document in mongodb - update and return document in mongodb
- Check log4j problems using stain analysis
猜你喜欢
![[flutter] example of asynchronous programming code between future and futurebuilder (futurebuilder constructor setting | handling flutter Chinese garbled | complete code example)](/img/04/88ce45d370a2e6052c2fce558aa531.jpg)
[flutter] example of asynchronous programming code between future and futurebuilder (futurebuilder constructor setting | handling flutter Chinese garbled | complete code example)

Sous - système I2C (IV): débogage I2C

sql server数据库添加 mdf数据库文件,遇到的报错

I2C subsystem (II): I3C spec

vfork执行时出现Segmentation fault
![[principles of multithreading and high concurrency: 1_cpu multi-level cache model]](/img/7e/ad9ea78868126b149bd9f15f587e6c.jpg)
[principles of multithreading and high concurrency: 1_cpu multi-level cache model]

Add some hard dishes to the interview: how to improve throughput and timeliness in delayed task scenarios!

docker安装mysql

The Linux server needs to install the agent software EPS (agent) database

Baidu map - surrounding search
随机推荐
Cron表达式介绍
用docker 连接mysql的过程
js根据树结构查找某个节点的下面的所有父节点或者子节点
Deep learning: multi-layer perceptron and XOR problem (pytoch Implementation)
[error record] the parameter 'can't have a value of' null 'because of its type, but the im
Two dimensional format array format index subscript continuity problem leads to return JSON format problem
Process the dataset and use labelencoder to convert all IDs to start from 0
The base value is too large (the error is marked as "08") [duplicate] - value too great for base (error token is'08') [duplicate]
Force freeing memory in PHP
What is the way out for children from poor families?
ComponentScan和ComponentScans的区别
Linear rectification function relu and its variants in deep learning activation function
tensorflow转pytorch笔记;tf.gather_nd(x,y)转pytorch
A2L file parsing based on CAN bus (2)
模糊查詢時報錯Parameter index out of range (1 > number of parameters, which is 0)
Informatics Olympiad one general question bank 1006 a+b questions
Global and Chinese ammonium dimolybdate market in-depth analysis and prospect risk prediction report 2022 Edition
The Linux server needs to install the agent software EPS (agent) database
Unity3d human skin real time rendering real simulated human skin real time rendering "suggestions collection"
左值右指解释的比较好的