当前位置:网站首页>Locks in MySQL

Locks in MySQL

2022-06-22 13:22:00 Xiaotiantian 666

A lock is a mechanism by which a computer coordinates multiple processes or threads to access a resource concurrently . In the database , In addition to traditional computing resources (CPU、RAM、I/O) Beyond contention , Data is also a resource that is Shared by many users . How to ensure the consistency of data concurrent access 、 Validity is a problem that all databases must solve , Lock conflicts are also an important factor affecting the performance of concurrent database access . From this perspective , Locks are especially important for databases , It's more complicated .

  • classification
    MySQL In the lock , According to the granularity of the lock , It can be divided into three categories :
    1、 Global lock : Lock all tables in the database .
    2、 Table lock : Lock the whole table with each operation .
    3、 Row-level locks : Each operation locks the corresponding row data .

Global lock

Global lock is to lock the whole database instance , After locking, the entire instance is in a read-only state , Follow up DML Write statement ,DDL sentence , Transaction commit statements that have been updated will be blocked .
Its typical use scenario is to make a logical backup of the whole database , Lock all tables , To get a consistent view , Ensure data integrity .

  • demonstration
# Global lock statement 
flush tables with read lock;
# Backup database 
mysqldump -u root -p 123456 databasename > test.sql
# The lock 
unlock table; 
  • characteristic
    Add a global lock to the database , It's a heavy operation , There are the following problems :
    1、 If it is backed up on the primary database , The update cannot be performed during the backup , Business basically has to stop .
    2、 If you are backing up from a library , During the backup, the slave database cannot synchronize the binary logs from the master database (binlog), Will cause master-slave delay .

stay InnodDB In the engine , We can add parameters during backup --single-transaction To complete the consistent data backup without lock .

mysqldump --single-transaction -uroot -p123456 databasename > test.sql

Table lock

Table lock , Operate the entire table each time . Large locking size , The highest probability of lock collisions , Lowest degree of concurrency . Apply to MySQL、InnoDB、BDB Wait for the storage engine .

For table lock , It is mainly divided into the following three categories :
1、 Table locks
2、 Metadata lock (meta data lock,MDL)
3、 Intent locks

  • Table locks
    For table locks , There are two kinds of :
    1、 Table share read lock (read lock)
    2、 Table Write Lock (write lock)
    grammar :
    1、 Lock :lock tables Table name … read/write
    2、 Release the lock :unlock tables / Client disconnected
  • 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 .
    1、 Intention sharing lock (IS): By statement select … lock in share mode add to . Share lock with table lock (read) compatible , Exclusive lock with table lock (write) Mutually exclusive .
    2、 Intention exclusive lock (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 .

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 locking records . For row level locks , It is mainly divided into the following three categories :
1、 Row lock (Record Lock): Lock a single row record , Prevent other transactions from... On this line update and delete. stay RC、RR Both isolation levels support .
2、 Interval lock (Gap 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, It produces unreal reading . stay RR Both isolation levels support .
3、 Temporary key lock (Next-Key Lock): Combination of row lock and clearance lock , Lock the data at the same time , And lock the gap in front of the data Gap. stay RR Support at isolation level .

Row lock

InnoDB Two types of row locks are implemented :
1、 Shared lock (S): Allow a transaction to read a line , Prevent other transactions from obtaining exclusive locks on the same dataset .
2、 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 .

Lock type S( Shared lock )X( Exclusive lock )
S( Shared lock ) compatible Conflict
X( Exclusive lock ) Conflict Conflict
SQL Row lock type explain
INSERT … Exclusive lock Automatic locking
UPDATE … Exclusive lock Automatic locking
DELETE … Exclusive lock Automatic locking
SELECT( normal ) No locks
SELECTE … LOCK IN SHARE MODE Shared lock Need to manually re SELECT Then add LOCK IN SHARE MODE
SELECT … FOR UPDATE Exclusive lock Need to manually re SELECT Then add FOR UPDATE

By default ,InnoDB stay REPEATABLE READ Transaction isolation level running ,InnoDB Use next-key Lock for search and index scan , To prevent unreal reading .
1、 When searching for a unique index , When performing equivalence matching on existing records , It will be automatically optimized as row lock .
2、InnoDB The row lock of is the lock added for the index , Retrieve data without index criteria , that InnoDB Lock all records in the table , At this point, it will be upgraded to table lock .

You can use the following SQL, Check the locking of intention lock and row lock :

select OBJECT_SCHEMA,OBJECT_NAME,INDEX_NAME,LOCK_TYPE,LOCK_MODE,LOCK_DATA from performance_schema.data_locks;

Clearance lock / Temporary key lock

By default ,InnoDB stay REPEATABLE READ Transaction isolation level running ,InnoDB Use next-key Lock for search and index scan , To prevent unreal reading .
1、 Equivalent query on Index ( unique index ), When locking records that do not exist , Optimized for clearance lock .
2、 Equivalent query on Index ( General index ), When traversing to the right, the last value does not meet the query requirements ,next-key lock Degenerate to clearance lock .
3、 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 , A gap lock adopted by one transaction will not prevent another transaction from adopting a gap lock on the same gap .

原网站

版权声明
本文为[Xiaotiantian 666]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221230052204.html