"Lock" in MySQL

"Lock" in MySQL

Foreword

  • In software development, in the case of high concurrency, in order to ensure consistency or security, we usually solve it by locking. There is also such a problem in the MySQL database. The concurrent access to the database, on the other hand, needs to ensure that each user can read and modify the data in a consistent manner, a lock mechanism is introduced.
  • There are many types of locks in the MySQL database, but they can be roughly divided into three categories: global locks, table-level locks, and row-level locks.

1. Global Lock

  • Global locks are the locks with the largest granularity, which are basically unusable and control the entire database instance. Global lock is to lock the entire database, so that the entire database is in a read-only state, unable to write data to the database
  • MySQL adds a global read lock method, the command flush tables with read lock (FTWRL) , after locking, all the tables and databases in the entire MySQL database are in a read-only state, and commands for data operations will be executed unsuccessfully.
  • Global locks are mainly used for full database backups, and are only used for full database backups of storage engines that do not support consistent reads. For example, MyISAM requires global locks for full database backups. Global lock

2. Table-level lock

  • As the name implies, only lock tables, this is MySQL's most basic locking strategy, and table-level locking is the least expensive strategy
  • Table-level locks are the same as global locks. The lock commands provided by the MySQL database are: lock tables… read / write . For example: lock tables biao1 read, biao2 write;, other threads writing biao1 and reading biao2 will be blocked.
  • You can use the unlock tables command to actively release the lock, otherwise, it is automatically released when the client disconnects
  • There is a problem with table-level locks. If a query is traversing the data in a table, and another thread changes the table structure during the execution and deletes a column, then the result obtained by the query thread does not match the table structure. It doesn't work.
  • In order to solve this problem, meta data lock (MDL) was introduced after MySQL version 5.5 . MDL is an automatic database lock. When adding, deleting, and modifying a table, MDL read lock is added; When doing structure change operation, add MDL write lock.
  • MDL locks have the following two characteristics:
  • The read locks are not mutually exclusive, so you can have multiple threads adding, deleting, and modifying a table at the same time.
  • The read-write locks and the write locks are mutually exclusive, which is used to ensure the safety of the operation of changing the table structure. Therefore, if two threads need to add fields to a table at the same time, one of them must wait for the other to finish execution before it can start execution.

3. Row-level lock

  • Row-level locking, as the name implies, is to lock the row records in the database table. Row-level locking can support concurrent processing to the greatest extent, but it also brings the largest locking overhead.

  • Row-level locks are easier to understand. For example, transaction A updates a row, and transaction B also needs to update the same row at this time. You must wait for transaction A to complete the update.

  • Row-level locks are implemented by storage engines, and not all storage engines support row-level locks. For example, MyISAM engine does not support row-level locks, which means that MyISAM storage engine can only use table-level locks to control concurrency.

  • The InnoDB engine implements row-level locking, and the InnoDB storage engine implements two standard row-level locks:

  • Shared lock (S Lock) : allows transactions to read a row

  • Exclusive lock (X Lock) : allows transactions to delete and update a row

  • Shared locks are compatible locks , that is, when a transaction has acquired the shared lock of row r, other transactions can immediately obtain the shared lock of row r, because reading does not change the data of row r.

  • An exclusive lock is an incompatible lock . If a transaction wants to acquire an exclusive lock on row r, if there is a shared lock or exclusive lock on row r, it must wait for other transactions to release the lock on row r.

  • In the InnoDB storage engine, by default, consistent non-locking row reading is used, that is, row data is read through the row multi-version controller. We can display the row with shared locks and exclusive locks, statements as follows:

  • SELECT… FOR UPDATE : add an exclusive lock to the row records read, other transactions want to add any lock on these rows will be blocked

  • SELECT… LOCK IN SHARE MODE : Add a shared lock to the row record read. Other transactions can add a shared lock to the locked record, but want to add an exclusive lock. Will be blocked.

Published 140 original articles · 49 praises · 10,000+ views

Guess you like

Origin blog.csdn.net/double_happy111/article/details/105517307