"MySQL Practical Combat 45 Lectures" Course Study Notes (5)

Database locks: global locks, table locks, and row locks

  • According to the scope of locking, the locks in MySQL can be roughly divided into three categories: global locks, table-level locks and row locks.

global lock

  • The global lock is to lock the entire database instance.
    • MySQL provides a way to add a global read lock, the command is Flush tables with read lock (FTWRL).
    • When you need to make the entire library in a read-only state, you can use this command, and then the following statements of other threads will be blocked: data update statement (addition, deletion and modification of data), data definition statement (including table creation, table structure modification, etc. ) and a commit statement for an update transaction.
  • A typical usage scenario of a global lock is to make a logical backup of the entire database. That is, select each table in the entire database and save it as text.
    • The official logical backup tool that comes with it is mysqldump.
      • When mysqldump uses the parameter --single-transaction, a transaction will be started before the data is imported to ensure a consistent view.
      • Due to the support of MVCC, the data can be updated normally during this process.

table lock

  • There are two types of table-level locks in MySQL: one is a table lock, and the other is a metadata lock (meta data lock, MDL).
  • The syntax for table locks is lock tables ... read/write. The lock tables syntax not only restricts the reading and writing of other threads, but also restricts the next operation objects of this thread.
  • Another type of table-level lock is MDL (metadata lock).
    • MDL does not need to be used explicitly, it will be added automatically when accessing a table.
    • The role of MDL is to ensure the correctness of reading and writing.
    • Read locks are not mutually exclusive, so you can have multiple threads add, delete, modify and query a table at the same time.
    • The read-write locks and the write locks are mutually exclusive to ensure the security of the operation of changing the table structure. Therefore, if two threads want to add fields to a table at the same time, one of them will not start executing until the other finishes executing.

row lock

  • InnoDB supports row locks, which is one of the important reasons why MyISAM is replaced by InnoDB.
  • A row lock is a lock for row records in a data table.
    • In InnoDB transactions, row locks are added when needed, but they are not released immediately when they are not needed, but are not released until the end of the transaction. This is the two-stage lock protocol.
    • If you need to lock multiple rows in your transaction, put the locks that are most likely to cause lock conflicts and affect concurrency as far back as possible.
  • Deadlocks and Deadlock Detection
    • When different threads in a concurrent system have cyclic resource dependencies, and the threads involved are all waiting for other threads to release resources, it will cause these threads to enter an infinite waiting state, which is called deadlock.
    • When a deadlock occurs, there are two strategies:
      • One strategy is to just go ahead and wait until it times out. This timeout can be set by the parameter innodb_lock_wait_timeout.
      • Another strategy is to initiate deadlock detection, and after a deadlock is found, actively roll back a transaction in the deadlock chain so that other transactions can continue to execute. Set the parameter innodb_deadlock_detect to on to enable this logic.
    • You can consider reducing lock conflicts by changing one line to logically multiple lines.

Guess you like

Origin blog.csdn.net/fangzhan666/article/details/132013448