01.24 Day 5 - MySQL global locks, table locks, row lock

Hello everyone, I'm Snow Hide, as one of the "MySQL real" students of this column, this is my punch of the fifth day, and also my 45th this operation.

Today I review in this column called "global locks and table locks: add a field to the table how there are so many hinder? "" Row locks merits and demerits: how to reduce the impact on the performance of the line lock? "Article.

Keywords summary: Lock classification, global lock (lock method, application scenarios, the risk, it is recommended to use two reasons FTWRL way), table-level locking (table-level locking species (table lock, lock metadata, MDL write lock) ), lock engine support line, two-phase locking (row lock release times), deadlocks and deadlock detection (two strategies to solve the deadlock, solve performance problems caused by hot-line update).

 

The study summary:

 

Lock Category

Global Lock, table-level locks, row lock.
 

Global Lock

Lock method

FTWRL (Flush Tables With Read Lock), so that the whole library in read-only state, then some statements, other threads running will be blocked: data update (additions and deletions), data definition (construction of the table, modify table structure), update class transaction commits.

Scenarios

Full database logical backup.

risk

  • Can not be performed during the main library locked update, suspend business operations;
  • During the lock can not be performed from the main library database synchronization over binlog, leading to a master-slave delay.

The reason for using two recommended ways of FTWRL

  • readonly In some systems, other logic is used, for example, determines a master database or library equipment. Therefore modify global variables affect relatively large;
  • After performing FTWRL client if an exception is disconnected, library global lock automatically released and back to normal occurrence. If the client is set to take place during the readonly abnormal, the library will remain readonly state, will not return to normal state.
     

Table-level locking

Table-level locking species

Table lock, lock metadata (MDL, Meta Data Lock)

Table lock

lock tables ... read / write. And FTWRL Similarly, the lock can be released or locked automatically released when the client is disconnected by means of unlock tables. In addition to limiting write lock tables other threads, the threads will limit the next operation target.

Metadata lock

No need to explicitly declare, automatically lock when accessing the table, and to ensure the accuracy of reading and writing.

MDL write lock

  • Read locks are not mutually exclusive, can be multi-threaded on the table CRUD;
  • Read-write lock exclusive write locks to ensure the safety of the table structure changes. Multithreaded operating sequence table structure.
     

The engine supports row lock

InnoDB supports row lock.
 

Two-phase locking

Row lock release time

Wait until the end of the transaction release.
 

Deadlock and deadlock detection

Two strategies to solve deadlock

  • Waiting for the timeout. Timeout parameter settings: innodb_lock_wait_timeout;
  • Initiate deadlock detection, roll back a transaction deadlock chain, so that other transactions can continue. Innodb_deadlock_detech to set the parameters on.

Solve performance problems caused by hot-line update

  • In ensuring the business will not be a deadlock situation temporarily turn off deadlock detection, turn off deadlock detection may occur a lot of time out;
  • ? Control the degree of concurrency. Update Peer queued before entering the engine, in order to reduce the deadlock detection work inside the InnoDB;
  • To reduce conflicts into a row of multiple rows of logic, in order to reduce the number of wait lock.

 

Finally

Re summed up the contents mentioned in the text: global lock, table lock, lock the global scenario, the table lock application scenarios, MDL metadata locks, row lock, two-phase locking protocol, deadlock and deadlock detection, control access to the same concurrent transaction amount of resources.

Published 81 original articles · won praise 6 · views 1850

Guess you like

Origin blog.csdn.net/stevenchen1989/article/details/104079261