当前位置:网站首页>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
边栏推荐
- Strategy application of Dameng database
- Counter统计数量后,如何返回有序的key
- Can netstat still play like this?
- Informatics Olympiad one general question bank 1006 a+b questions
- A2L file parsing based on CAN bus (2)
- Creation and destruction of function stack frame
- Sous - système I2C (IV): débogage I2C
- Parameter index out of range (1 > number of parameters, which is 0)
- Sqlserver row to column pivot
- Docker install redis
猜你喜欢
[flutter] example of asynchronous programming code between future and futurebuilder (futurebuilder constructor setting | handling flutter Chinese garbled | complete code example)
xiaodi-笔记
Thunderbolt Chrome extension caused the data returned by the server JS parsing page data exception
Distributed transaction
[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)
ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc
Three. JS local environment setup
Add automatic model generation function to hade
你真的懂继电器吗?
随机推荐
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?
Pytest (6) -fixture (Firmware)
Source code analysis | layout file loading process
Use optimization | points that can be optimized in recyclerview
[error record] the parameter 'can't have a value of' null 'because of its type, but the im
Chart. JS multitooltip tag - chart js multiTooltip labels
Linear rectification function relu and its variants in deep learning activation function
Variable declarations following if statements
Last update time of all sqlserver tables
处理数据集,使用LabelEncoder将所有id转换为从0开始
docker安装mysql
C语言中左值和右值的区别
leetcode540
Global and Chinese ammonium dimolybdate market in-depth analysis and prospect risk prediction report 2022 Edition
ComponentScan和ComponentScans的区别
"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
Cancer biopsy instruments and kits - market status and future development trends
The core idea of performance optimization, dry goods sharing
[fluent] listview list (map method description of list set | vertical list | horizontal list | code example)
open file in 'w' mode: IOError: [Errno 2] No such file or directory