当前位置:网站首页>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
边栏推荐
- Wechat - developed by wechat official account Net core access
- C语言初阶-指针详解-庖丁解牛篇
- From C to capable -- use the pointer as a function parameter to find out whether the string is a palindrome character
- docker安装mysql
- Pytest (6) -fixture (Firmware)
- Can netstat still play like this?
- How to select the minimum and maximum values of columns in the data table- How to select min and max values of a column in a datatable?
- ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc
- [fluent] future asynchronous programming (introduction | then method | exception capture | async, await keywords | whencomplete method | timeout method)
- Kubernetes family container housekeeper pod online Q & A?
猜你喜欢

vfork执行时出现Segmentation fault

C language beginner level - pointer explanation - paoding jieniu chapter

力扣------网格中的最小路径代价

Add automatic model generation function to hade

Your family must be very poor if you fight like this!

Distributed transaction
![ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc](/img/cb/145937a27ef08050a370d5a255215a.jpg)
ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc

函数栈帧的创建与销毁

A2L file parsing based on CAN bus (2)

超好用的日志库 logzero
随机推荐
I2C subsystem (IV): I2C debug
Add MDF database file to SQL Server database, and the error is reported
Today, it's time to copy the bottom!
Kubernetes family container housekeeper pod online Q & A?
I2C 子系统(一):I2C spec
疫情当头,作为Leader如何进行代码版本和需求开发管控?| 社区征文
销毁Session和清空指定的属性
Deep reinforcement learning for intelligent transportation systems: a survey paper reading notes
docker安装mysql
Sous - système I2C (IV): débogage I2C
Matlab tips (24) RBF, GRNN, PNN neural network
左值右指解释的比较好的
SQL server queries the table structure of the specified table
Didi programmers are despised by relatives: an annual salary of 800000 is not as good as two teachers
Notifydatasetchanged not applicable to recyclerview - notifydatasetchanged not working on recyclerview
Yiwen takes you to know ZigBee
Three. JS local environment setup
超好用的日志库 logzero
[fluent] future asynchronous programming (introduction | then method | exception capture | async, await keywords | whencomplete method | timeout method)
I2C 子系統(四):I2C debug