Mysql advanced [2] discusses the lock mechanism, SQL lock situation

  

1.MySQL lock mechanism

1.1 According to the granularity, there are three types:

  1. Global lock: core service layer implementation, lock database, full table with read lock;
  2. Table-level lock: implemented by the core service layer, locking a certain table in the database
    1. Add table-level read lock: this transaction cannot read information about other unlocked tables
  3. Row-level lock: implemented by the storage engine layer, what is locked is the index of a certain row, or the gap between the indexes
    1. Record Locks: Lock a record in the index.
    2. Gap Locks: Either lock the value in the middle of the index record, or lock the value before the first index record or the value after the last index record.
    3. Next-Key Locks: It is a combination of record locks on index records and gap locks before index records (gap locks + record locks).
    4. Insert Intention Locks (Insert Intention Locks): The lock on the record id added during the insert operation.

The lock of the row lock is added to the index, not the data itself

  1. Next-Key Locks (Next-Key Locks) are equivalent to record locks + gap locks [ left open right closed interval ]
  2. Gap lock: If the statement is not queried, a gap lock is added to ensure that other data is not inserted into the index gap and prevent phantom reading. What is locked is an index range, an open range, excluding both sides.
  3. Record lock: equivalent query, the lock is added to the index of the record, not the record itself

1.2 Divided into two types by function:

  1. Shared locks Shared Locks (S locks, read locks): records with read locks added, allowing other transactions to add read locks (select....lock in share mode)
  2. Exclusive locks Exclusive Locks (X locks, write locks): Once a write lock is added, other transactions cannot add other write locks or read locks (select for update)

2. Analyze the locking situation of SQL: delete from tt where uid = 123;

2.1 Read the submitted RC

  1. If uid is an index and also a primary key, and this record is found, a write lock will be added to this record
  2. uid is a unique index, not a primary key. First find this record and add a write lock, and then take the primary key to find this record in the primary key index and add a write lock.
  3. uid is not a unique index, the query result is not unique, all eligible records are queried and locked, and the primary key index is found and locked through the primary key
  4. Uid is not an index, and a write lock is added to the full table scan, and a write lock is added whenever the condition is met

2.2 Repeatable read [RR] isolation level

  1. If uid is an index and also a primary key [find this record, then add a write lock to the record]
  2. uid is a unique index, not a primary key. [Find qualified records in the auxiliary index, add a write lock, take the primary key to the primary key index to find the corresponding data, and add a write lock]
  3. uid is not a unique index, and the query result is not unique, [Find all records that meet the conditions, and add a write lock to the record that is not unique, and then take the primary key to the primary key index to find all the corresponding records and add a write lock, and the records between records Add a gap lock to the data, find the next data that does not meet the conditions plus, add a gap lock to the gap between the qualified and unqualified data]
  4. uid is not an index [full table scan, add write locks and gap locks to all records, set the innodb_locks_unsafe_for_binlog parameter, for records that do not meet the query conditions, MySQL will release write locks in advance, without adding gap locks]

 

 

Guess you like

Origin blog.csdn.net/qq_45656077/article/details/131063725