MySQL row lock parse (MySql lock mechanism)

Row lock (partial write)

Features:
bias InnoDB storage engine, lock large lock slow; there will be a deadlock; locking granularity smallest and lowest probability of lock conflicts, have the highest degree of concurrency.
The biggest difference InnoDB and MyISAM are two points: First, to support the transaction (TRANSACTION); the second is using row-level locking.

Affairs (Line Lock Support Services)

ACID properties of the transaction and its
Here Insert Picture Description
problems caused by concurrent transactions:
update loss
Here Insert Picture Description
dirty reads,
Here Insert Picture Description
non-repeatable read (solutions to add a row lock)
Here Insert Picture Description
phantom reads
Here Insert Picture Description
transaction isolation level: (MYSQL default repeatable read)
Here Insert Picture Description

case study:

Build SQL table
Here Insert Picture Description

Line Lock Basic Presentation (add an index (row locks) do not have a full table scan to locate the line directly, using the primary key is the use of row locks)
Here Insert Picture Description
different rows updated no problem

No index, row lock escalation to a table lock
varchar type b written in a numeric column index failed to make at this time session 2-operation will be stepping into a different line of waiting
Here Insert Picture Description
gap lock Hazard
Here Insert Picture Description
Here Insert Picture Description
face questions: How often test a row lock
Here Insert Picture Description

Case Conclusion

Here Insert Picture Description

Row lock analysis

Here Insert Picture Description
Here Insert Picture Description

Optimization Tips

  1. As far as possible to retrieve all the data are done by the index, the index line to avoid no lock escalation to a table lock.
  2. Rational design of the index, minimizing lock range
  3. As a search condition is less, to avoid gaps lock
  4. Try to control the size of the transaction, and the length of time to reduce the amount of resources locked
  5. The transaction isolation level as low as possible
Published 217 original articles · won 125 Like · views 10000 +

Guess you like

Origin blog.csdn.net/qq_39885372/article/details/104204053