MySQL lock analysis (summarize and analyze various situations)

Reference: MySQL lock processing analysis . The article has been very detailed and easy to understand. The following is just a personal summary.

 

1. Background

1.1 Isolation level

1.2 Locking process

  One by one processing, one by one lock.

update execution process

 

1.3 Two-phase lock 2PL

1.4 gap lock

  Gap lock is a gap lock, that is, a lock between two adjacent valid records (the lock is the gap), which is aimed at insert and is used to solve the occurrence of phantom reading. It will block insert, but not delete/update etc (the record doesn't exist anyway).

  An important difference between RC and RR is phantom reading. Therefore, RR needs to introduce gap locks.

 

2. Analysis of lock combination

  When talking about what lock to add, you must first understand two premises: 1) the isolation level; 2) the index used. Different isolation levels and different indexes will affect locking.

2.1 Commitable read RC

 

  primary key/unique key secondary index no index
select without without without
insert row lock row lock row lock
select...for update/update/delete row lock row lock on valid row Full table lock clustered index

2.2 Repeatable read RR

 

  primary key/unique key secondary index no index
select without without without
insert row lock row lock row lock
select...for update/update/delete row lock Row lock + gap lock for valid rows Full table lock clustered index

2.3 Serializable S

  primary key/unique key secondary index no index
select (snapshot read) S S S
Other (currently reading) X X X

 

  Note: When there is no index, select...for update/update/delete needs to lock the entire table, but mysql_server can be optimized. When locking one by one, if it is found that it is not the target record, the lock can be released. But that would go against the principles of 2PL.

2.4 Conclusion

  The occurrence of deadlock does not depend on the number of SQL statements in the transaction. The key to deadlock is that the order of locking of two (or more) sessions is inconsistent.

  The locking process is processed one by one, and locked one by one (all are finally reflected on the clustered index).

  • Primary key, added to the clustered index;
  • Secondary index, added to secondary index + clustered index;
  • No index, added to the clustered index.

Therefore, if two sessions use different secondary indexes, the locking sequence of the clustered index is inconsistent, which leads to the holding and competition of locks between sessions, and it is easy to cause deadlocks.

 

Author: Shuiyan
         

Articles not marked as reprinted in this blog belong to the author Shuiyan and Blog Garden. Reprints are welcome, but this statement must be retained without the author's consent, and a link to the original text should be given in an obvious position on the article page, otherwise the right to pursue legal responsibility is reserved.



https://www.cnblogs.com/waterystone/p/5127988.html


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325644957&siteId=291194637