Introduction to Locks#
In MySQL, locks are categorized into three types based on their granularity:
- Global Lock: Locks all tables in the database
- Table Lock: Locks the entire table for each operation
- Row Lock: Locks the corresponding row data for each operation
Global Lock#
A global lock locks the entire database instance, putting the entire instance into a read-only state. Subsequent DML write statements, DDL statements, and transaction commit statements for updates will be blocked.
A typical scenario is locking all tables for a full database backup. Without locking, as shown in the image below, there may be inconsistencies where orders are generated but inventory is not deducted.
With the lock, only reading is allowed, ensuring global consistency at the time of backup.
flush tables with read lock; # Locking
mysqldump -uroot -p123456 database > database.sql # Backup
unlock tables; # Unlocking
Applying a global lock is a relatively heavy operation and has the following drawbacks:
- If backing up on the master database, no updates can be executed during the backup period, effectively halting business operations.
- If backing up on a slave database, the slave cannot execute the binary logs (binlog) synchronized from the master during the backup period, leading to master-slave lag.
In the InnoDB engine, adding the parameter --single-transaction
allows for consistent data backup without locking, using snapshot reads for the backup operation.
mysqldump --single-transaction -uroot -p123456 database > database.sql
Table Lock#
Table locks lock the entire table for each operation, resulting in a larger locking granularity, the highest probability of lock conflicts, and the lowest concurrency. They are applied in storage engines like MyISAM, InnoDB, and BDB.
Table locks are mainly divided into three categories:
- Table Lock
- Metadata Lock (MDL)
- Intent Lock
Table Lock#
Table locks are divided into two types:
- Shared Read Lock
- Exclusive Write Lock
Shared Read Lock allows reading by multiple transactions but prevents writing.
Exclusive Write Lock allows only the locking transaction to read and write, while others cannot operate.
Metadata Lock#
The metadata lock (MDL) process is automatically controlled by the program and does not require explicit use. It is automatically applied when accessing a table. The main purpose of the MDL lock is to maintain the consistency of table metadata. When there are active transactions on a table, write operations on the metadata are not allowed; this is to avoid conflicts between DML and DDL and ensure the correctness of reads and writes.
MDL was introduced in MySQL v5.5:
When performing DML operations (insert, delete, update, query) on a table, an MDL read lock is applied (shared).
When performing DDL operations that change the table structure, an MDL write lock is applied (exclusive).
The essence of the metadata lock is that while a transaction is ongoing, the table structure cannot be modified; DML during the transaction will request an MDL shared read lock (SHARE_READ/SHARE_WRITE), while DDL that modifies the table structure will request an MDL exclusive lock (EXCLUSIVE).
MDL mainly targets the table structure's metadata, ensuring that the shared read lock (SHARE) for reading the table structure and the exclusive write lock (EXCLUSIVE) for changing the table structure can restrict the consistency of DML operations.
Intent Lock#
To avoid conflicts between row locks and table locks during DML execution, InnoDB introduces intent locks, allowing table locks to avoid checking each row of data for locks, thereby reducing the checks for table locks.
When other transactions hold an intent shared lock (IS) on the table, it is possible to add a read table lock, but not a write table lock; the transaction must wait for the intent shared lock to be released.
When other transactions hold an intent exclusive lock (IX) on the table, it is not possible to add either a read or write table lock; the transaction must wait for the intent exclusive lock to be released.
Row Lock#
Row locks lock the corresponding row data for each operation, having the smallest locking granularity, the lowest probability of lock conflicts, and the highest concurrency. They are applied in the InnoDB storage engine.
Record Lock: Locks a single row record, preventing other transactions from performing updates and deletes on that row. It is supported under ReadCommitted and RepeatableRead isolation levels.
Gap Lock: Locks the gaps between index records, excluding the records themselves, ensuring that the gaps between index records remain unchanged and preventing other transactions from inserting into these gaps, thus avoiding phantom reads. It is supported under the RepeatableRead isolation level.
Next-Key Lock: A combination of row locks and gap locks, locking both the data and the gap before the data. It is supported under the RepeatableRead isolation level.
Record Lock#
There are two types of row locks:
- Shared Lock (S): Allows one transaction to read a row, preventing other transactions from obtaining an exclusive lock on the same dataset.
- Exclusive Lock (X): Allows the transaction holding the exclusive lock to update the data, preventing other transactions from obtaining either a shared or exclusive lock on the same dataset.
Similar to read/write.
Related SQL row lock types.
By default, InnoDB operates at the RepeatableRead transaction isolation level. InnoDB uses next-key locks for searching and index scanning to prevent phantom reads:
- When retrieving based on a unique index, if performing an equality match on existing records, it will automatically optimize to a row lock.
- InnoDB's row locks are applied to indexes; if data is retrieved without using index conditions, InnoDB will lock all records in the table, which will escalate to a table lock.
Gap Lock / Next-Key Lock#
The sole purpose of the gap lock is to prevent other transactions from inserting into the gaps. Gap locks can coexist; a gap lock used by one transaction will not prevent another transaction from using a gap lock on the same gap.
It can be a bit difficult to understand, so let's refer to the notes.
Summary#
- Overview
In concurrent access, it addresses the consistency and validity of data access.
Global locks, table locks, row locks. - Global Lock
Locks the entire database instance, putting the entire instance into a read-only state.
Poor performance, used for logical data backups. - Table Lock
Locks the entire table for operations, with a large locking granularity and a high probability of lock conflicts.
Table locks, metadata locks, intent locks. - Row Lock
Locks the corresponding row data for operations, with the smallest locking granularity and the lowest probability of lock conflicts.
Row locks, gap locks, next-key locks.
This article is synchronized and updated to xLog by Mix Space. The original link is https://blog.0xling.cyou/posts/mysql/mysql-1