Shared and Exclusive Locks
Intention Locks
Record Locks
Gap Locks
Next-Key Locks
Insert Intention Locks
AUTO-INC Locks
Predicate Locks for Spatial Indexes
Shared and Exclusive Locks
- latch: A lightweight structure used by InnoDB to implement a lock for its own internal memory structures
- latches Mutexes and rw-locks are known collectively as latches
- lock: The high-level notion(概念) of an object that controls access to a resource, such as a table, row, or internal data structure, as part of a locking strategy.
- InnoDB implements standard row-level locking
- rw-lock types include s-locks (shared locks), x-locks (exclusive locks), and sx-locks (shared-exclusive locks).
- A shared (S) lock (共享), provides read access to a common resource
- An exclusive (X) lock (排它), provides write access to a common resource while not permitting inconsistent reads by other threads.
- An sx-lock provides write access to a common resource while permitting inconsistent reads by other threads. sx-locks were introduced in MySQL 5.7 to optimize concurrency and improve scalability for read-write workloads.
S | SX | X | |
---|---|---|---|
S | Compatible | Compatible | Conflict |
SX | Compatible | Conflict | Conflict |
X | Conflict | Conflict | Conflict |
Intention Locks
- Intention locks are table-level locks
- Intention shared (IS):
- Intention exclusive (IX):
- main purpose of IX and IS locks is to show that someone is locking a row, or going to lock a row in the table.
- intention locks do not block anything except full table requests (for example, LOCK TABLES ... WRITE).
- The intention locking protocol is as follows:
- Before a transaction can acquire an S lock on a row in table t, it must first acquire an IS or stronger lock on t. (在获得row的 S lock前,必须在表t上面先获得 IS 或者 stronger lock)
- Before a transaction can acquire an X lock on a row, it must first acquire an IX lock on t. (在获得row的 X lock前,必须在表t上面先获得 IX)
- intention lock互相兼容
X | S | IX | IS | |
---|---|---|---|---|
X | Conflict | Conflict | Conflict | Conflict |
S | Conflict | Compatible | Conflict | Compatible |
IX | Conflict | Conflict | Compatible | Compatible |
IS | Conflict | Compatible | Compatible | Compatible |
Record Locks
- A record lock is a lock on an index record
- Record locks always lock index records, even if a table is defined with no indexes.For such cases, InnoDB creates a hidden index and uses this index for record locking. (如果表没有定义索引,那么Innodb会创建一个隐式的clustered index聚簇[cù]索引,然后用这个索引加锁)
Gap Locks
- A gap lock is a lock on a gap between index records, or a lock on the gap before the first or after the last index record.
- SELECT c1 FOR UPDATE FROM t WHERE c1 BETWEEN 10 and 20;
- A gap might span a single index value, multiple index values, or even be empty.(gap可以是单个index,多个index,empty这个怎么理解?)
- conflicting locks can be held on a gap by different transactions
Next-Key Locks
- InnoDB performs row-level locking in such a way that when it searches or scans a table index, it sets shared or exclusive locks on the index records it .
AUTO-INC Locks
- An AUTO-INC lock is a special table-level lock taken by transactions inserting into tables with AUTO_INCREMENT columns