当前位置:网站首页>Mysql database lock learning notes

Mysql database lock learning notes

2022-07-07 09:14:00 White butterfly

What is a lock

A lock is a mechanism by which a computer coordinates multiple processes or threads to access a resource concurrently , Used to manage concurrent access to shared resources

Table lock

Table lock , Lock the whole table with each operation . Large locking size , The highest probability of lock collisions , Lowest degree of concurrency . Apply to MyISAM、 InnoDB Wait for the storage engine

Table locks

  • Watch reading lock ( grammar :lock tables Table name read; )
  • Watch write lock ( grammar :lock tables Table name write; )
  • Release the lock (unlock tables)

Read the lock :
 Insert picture description here
 Insert picture description here
Write lock :
 Insert picture description here
 Insert picture description here

Metadata lock

meta data lock , Metadata lock , Abbreviation MDL.

MDL The main function of locks is to maintain Data consistency of table metadata , When there are transactions on the table , The table structure cannot be modified . for fear of DML And DDL Conflict , Ensure the correctness of reading and writing

 Insert picture description here

Intent locks

for fear of DML When executed , Conflict between row lock and table lock , stay InnoDB Intention lock is introduced in , So that the table lock does not need to check whether each row of data is locked , Use intent lock to reduce the checking of table lock .

  • Intention sharing lock (IS): By statement select … lock in share mode add to ; And Table locks share locks (read) compatible , Exclusive lock with watch lock (write) Mutually exclusive .
  • Intending to be exclusive (IX): from insert、update、delete、select…for update add to ; Share lock with table lock (read) And exclusive lock (write) Are mutually exclusive , Intent locks are not mutually exclusive .

Once the transaction is committed , Intention sharing lock 、 The intentional exclusive lock will be automatically cancelled
 Insert picture description here

 Insert picture description here

Row-level locks

Row-level locks , Each operation locks the corresponding row data . Locking granularity minimum , The lowest probability of lock collisions , 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 adding to the record lock . For row level locks , It is mainly divided into the following three categories :

Row lock

  • Shared lock (S): Allow a transaction to read a line , Prevent other transactions from obtaining exclusive locks on the same dataset .

  • Exclusive lock (X): Allow to get transaction update data of exclusive lock , Prevent other transactions from obtaining shared and exclusive locks of the same dataset .

 Insert picture description here
 Insert picture description here

  • When searching for a unique index , When performing equivalence matching on existing records , It will be automatically optimized as row lock .

  • InnoDB The row lock of is a lock added to the index , Retrieve data without index criteria , that InnoDB All items in the table will be marked Record and lock , here Will be upgraded to table lock .

 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here

Clearance lock & Temporary key lock

  • Equivalent query on Index ( unique index ), When locking records that do not exist , Optimized for clearance lock .
  • Equivalent query on Index ( It's not the only common index ), When traversing to the right, the last value does not meet the query requirements ,next-key lock Degenerate to clearance lock .
  • Range query on Index ( unique index )– Access to the first value that does not satisfy the condition .

Be careful : The only purpose of a gap lock is to prevent other transactions from inserting gaps . Gap locks can coexist , The gap lock adopted by a transaction will not Prevent another transaction from adopting gap lock on the same gap .

The only purpose of a gap lock is to prevent other transactions from inserting gaps . Gap locks can coexist , A gap lock adopted by one transaction will not prevent another transaction from adopting a gap lock on the same gap .
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here

原网站

版权声明
本文为[White butterfly]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070627573660.html