当前位置:网站首页>[MySQL learning notes 30] lock (non tutorial)

[MySQL learning notes 30] lock (non tutorial)

2022-07-06 07:19:00 yqs_ two hundred and eighty-one million eight hundred and seven

classification

  • Global lock : Lock a library
  • Table lock : Lock a watch
  • Row-level locks : Lock a line of records

Global lock

Global lock is to lock the entire database , After locking, the entire database will be in a read-only state , Other operations will block .
The application scenario is that a global lock needs to be added when backing up a consistent database , Prevent data changes during backup .

Use global locks

flush tables with read lock;

mysql Backup command

mysqldump -u root -p 1234 db_name>backup.sql

Table lock

classification

  • Table locks ( It is divided into read lock and write lock)
  • Metadata lock (meta data lock,MDL)
  • Intent locks

Metadata lock

brief introduction
Metadata lock has mysql The system controls itself , We don't need to show using . Metadata lock is mainly used to maintain the data consistency of metadata , When there are active transactions on the table , You cannot write metadata .
understand
Metadata can be understood as the structural information of a table , When there are active transactions on a table , Metadata lock will be automatically added , Prevent the table structure from being modified during the operation data, resulting in various unexpected situations .
Lock timing
When adding, deleting, modifying and querying a table , Add MDL Read the lock .
When changing the structure of a table , Add MDL Write lock .

Add watch lock

lock tables Table name read;
Once the read lock is added , All connections, including yourself, cannot be written , Only read
lock tables Table name write;
Once the write lock is added , The locked connection can read and write , Other connections cannot be read or written

Unlock

unlock tables;

Intent locks

Introduce
Through the intentional lock , When another thread adds a table lock, it does not need to traverse all rows to determine whether it can add a lock , Improved efficiency .
classification
Intention sharing lock : By statement select … lock in share mode add to , Compatible with table lock sharing lock , Mutually exclusive with exclusive lock
Intention exclusive lock : from insert、update、delete、select … for update add to , Shared locks with table locks and exclusive locks are mutually exclusive

Row-level locks

Introduce

Row-level locks , Each operation locks the corresponding row of data . Minimum locking force , The probability of lock conflict is the lowest , Highest concurrency . Apply to InnoDB In the storage engine .
InnoDB The data is organized based on indexes , Row locking is achieved by locking the index items on the index , Instead of locking records

classification

  • Row lock : Lock a single row record , Prevent other transactions from doing this update and delete.
  • Clearance lock : Lock index record gap ( This record is not included ), Ensure that the index record gap remains unchanged , Prevent other transactions from going on in this gap insert, Produce pleasure .
  • Temporary key lock : Is a combination of row lock and clearance lock , It is equivalent to the clearance lock of the closed interval

Row lock

  • Shared lock : Allow other transactions to read
  • Exclusive lock : Prohibit other transactions from reading and writing

 Insert picture description here
ps: If the addition, deletion and modification conditions do not cover the index , that Innodb All records will be locked , Just upgrade to table lock

Clearance lock / Temporary key lock

 Insert picture description here

原网站

版权声明
本文为[yqs_ two hundred and eighty-one million eight hundred and seven]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060713326612.html