当前位置:网站首页>09 MySQL lock

09 MySQL lock

2022-06-11 19:30:00 Runaway mine

1 The type of lock

1.1 Row-level locks

MySQL Two standard levels of row level locks are provided

  • Shared lock (S Lock): Allow a transaction to read a row of data
  • Exclusive lock (X Lock): Allow transactions to delete or update a row of data

Compatibility between exclusive locks and shared locks

XS
X Conflict Conflict
S Conflict compatible

1.2 Intent lock

InnoDB Support Multi granularity locking , Allow locks at row level and table level to exist at the same time

InnoDB It also supports additional locking methods , According to the Intent locks , yes surface Level lock , Used to reveal the type of lock requested by the next row in a transaction

  • Intention sharing lock (IS Lock): The transaction wants to obtain the shared lock of some rows in the table
  • Intention exclusive lock (IX Lock): The transaction wants to obtain exclusive locks for certain rows in the table

The intent lock shall be subject to the following protocol :

  • Before the transaction acquires the shared lock of a row , First get IS A lock or stronger lock
  • Before a transaction acquires an exclusive lock on a row , First, get... On the table IX lock
ISIXSX
IS compatible compatible compatible Are not compatible
IX compatible compatible Are not compatible Are not compatible
S compatible Are not compatible compatible Are not compatible
X Are not compatible Are not compatible Are not compatible Are not compatible

X Locks are not compatible with any locks ,S Lock and IX Locks are not compatible , The rest are compatible

Intentional locks only block table level locks , Row level locks are not blocked

InnoDB Two kinds of locking operation of storage engine :

  • select … for update: Add a... To the read line X lock , Other transaction locking will be blocked
  • select … lock in share mode: Add a... To the read row record S lock , Other business can add S lock , But for the X The lock will be blocked

1.3 Self increasing lock

The transaction is inserted into the with AUTO-INC A special table level lock for columns , When a transaction is inserted , A self incrementing lock must be obtained to obtain the value of the self incrementing column

innodb_autoinc_lock_mode Parameters

  • value 0: stay 5.1 Implementation of self growth before version , Locked by watch AUTO-INC Locking The way
  • value 1( Default ): about Simple Inserts, Use mutexes to accumulate counters in memory , about Bulk Inserts, Using traditional watch lock AUTO-INC Locking The way
  • value 2: This mode can be used at any time Row-Base Replication, The maximum concurrent performance and Replication Data synchronization

1.4 Locking mechanism of foreign keys

To insert and update a foreign key, you need to query the parent table record first , If the row record is added by another transaction X Lock not committed , Then subsequent insert operations will first add S lock , The insert operation will be blocked , Data inconsistency will also occur after submission

2 Lock algorithm

InnoDB Three row lock algorithms :

  • Record Lock: Lock of single line record
  • Gap Lock: Clearance lock , Lock a range , Does not include the record itself
  • Next-Key Lock: Lock a range , Include the record itself , The default row record locking algorithm at the repeatable read isolation level

3 The lock problem

  • Lost update

    Multiple transactions modify a row data at the same time , Cause the modification of a transaction to be overwritten

  • Dirty reading

    Dirty data concept : Pages whose buffers have been modified , The data on the disk has not been written

    Dirty reading : Under different affairs , Read uncommitted data from other transactions , Exists at the read uncommitted isolation level

  • It can't be read repeatedly

    In the same thing , If data is modified and committed by another transaction , The data read twice will be inconsistent , Violation of the principle of transaction consistency

    stay InnoDB in , adopt Next-Key Lock The algorithm avoids the problem of non repeatable reading , stay mysql Is defined as Phantom Problem( Fantasy reading ),Next-key The algorithm will lock the scanned and gaps , Avoid inserting in scope , Avoid the problem of non - repeatable reading

4 Blocking

The lock in one transaction needs to wait for the lock in another transaction to release the occupied resources , Use mutex The data structure implements the blocking mechanism

InnoDB Using parameter innodb_lock_wait_timeout Control the waiting time of the lock , Parameters innodb_rollback_on_timeout Set whether to rollback , Default OFF Does not roll back ,lock Parameters can be dynamically adjusted during database operation , and rollback Parameters are static , Can only be adjusted at startup

5 Deadlock

The database may deadlock in parallel , The mutual waiting between two resources

InnoDB After a deadlock is detected , The transaction will be rolled back . Such as oracle, Deadlocks often occur when an index is not added to a foreign key , and InnoDB The engine will automatically add , Do not delete the foreign key index , Therefore, this problem has been solved better

6 Lock escalation

Definition : Reduce the granularity of the current lock , For example, put the... Of a table 100 A row lock is upgraded to a page lock , Or page lock is upgraded to table lock , To avoid lock overhead , Lock escalation occurs frequently

mssql stay 05 Row lock is supported after version , When one SQL The number of locks on an object exceeds the threshold 5000 Or lock resources occupy more memory than 40% Lock escalation will occur

InnoDB There is no lock escalation problem in ,MySQL The cost of internal locking is independent of the quantity , similar Oracle The design of the

原网站

版权声明
本文为[Runaway mine]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111924287300.html