mysql various locks

Including global locks, table locks (table locks, metadata locks, self-increment locks), row locks (row locks, gap locks, adjacent key locks, shared locks, exclusive locks, intent locks, and insert intent locks)

 

1. Global lock (FUWRL)

The statement is: Flush tables with read lock. You can lock the entire database instance so that the entire database is in a read-only state. The usage scenario is when the whole library is backed up logically.

There is also a global lock method is set global readonly=true (not recommended) , the reason:

  1. The readonly value is used to determine that the master-slave library is invalid.
  2. If something goes wrong. The FUWRL method automatically releases the global lock when the client is disconnected, while readonly does not.
  3. readonly is invalid for super users

In fact, our backup library can also use mysqldump --single-transaction , a transaction will be started before exporting the data to ensure data consistency. With MVCC support, this process data can be updated normally.

But not all engines support transactions, such as the MyIsam engine.

Two, table lock

(1) Table lock

InnoDb engine should set AUTOCOMMIT to 0

Locking syntax: lock tables ……read/write

Unlock syntax: commit statement + unlock tables statement

Example:

SET AUTOCOMMIT=0;
LOCAK TABLES t1 WRITE, t2 READ, ...;
COMMIT;
UNLOCK TABLES;

Note: When the storage engine is InnoDb, the table lock is not managed by InnoDb but the upper Mysql server is responsible. Only when autocommit=0, innodb_table_lock=1, Innodb can automatically detect and handle table-level deadlocks

(2) Metadata Lock (MDL) (Metadata Lock)

After mysql5.5, it is used to solve the problem of consistency of DDL and DML operations . MDL locks do not need to be used explicitly. When a DML statement is executed on a table (addition, deletion, modification, and check), the MDL read lock is added. Add MDL write lock when executing DDL statement (modify table structure).

The read locks are not mutually exclusive , and you can add, delete, modify, and check a table at the same time. The read-write lock and the write-write lock are mutually exclusive , and the table structure cannot be modified at the same time, and can only be executed in sequence.

So when we operate the production environment, such as adding fields to the table, pay attention to the mutual exclusion problem may lock the table. You can choose to add fields during low peak periods. Or check if there is a long transaction in execution, wait or kill it.

Note: MDL lock has no timeout limit.

(3) AUTO-INC Locks

 It is a special table-level lock that involves transactional insert operations involving AUTO_INCREMENT columns.

Three, row lock

(1) Record Lock

InnoDB's marketing is based on indexes. If you do not access data through indexes, InnoDB will use table locks (so the column after where needs to be indexed)

The row lock locks the index record, not the row data, which means that the key is locked

(2) Gap Lock

     Lock the gap of index records to ensure that the index gap remains unchanged. (Prevent other transactions from inserting operations to prevent phantom reads from occurring.), for repeatable reads or above isolation level

   Example: If the index record contains 1, 10, 100. It will contain 4 gaps (the interval that opens before and closes):

  • (negative infinity, 1]
  • (1, 10]
  • (10, 100]
  • (20, positive infinity)

(5) Next Key Lock

The result of the row lock and gap lock combination. Syntax:  select …… for update;

Under the repeatable read isolation level, InnoDB locks data rows in the Next-Key Lock mode. When scanning index records, a row lock (Record Lock) is added to the index record, and then a gap lock (Gap Lock) is added to the gap on both sides of the index.

When the queried index contains unique attributes, the lock will be downgraded to Record Lock, and only the index itself will be locked. There are other downgrade scenarios:

Scenes Lock type after downgrade
Unique index = match, and the record exists Record Lock
Unique index = match, and the record does not exist Gap Lock
Unique index range matching (< and >) Adjacent key lock, lock the upper bound, not lock the lower bound

(3) SHARED Lock (S lock)

     Statement: select …… lock in share model;    shared lock means that this transaction is readable and writable, and other transactions are readable and not writable.

(4) Exclusive lock (EXCLUSIVE Lock) (X lock) (mutual exclusion lock)

    Syntax:  select …… for update; exclusive lock means that this transaction is readable and writable, and other transactions are not readable or writable.

(6) Intention Lock

The intention lock is automatically added by InnoDB without manual intervention.

  • Intentional shared lock (IS) : The transaction prepares to add a shared lock to the data row. Obtain the IS lock of the table before locking it.
  • Intent exclusive lock (IX) : The transaction prepares to add an exclusive lock to the data row. Obtain the IX lock of the table before locking.

 (7) Insert Intention Lock (Insert Intention Lock)

The insertion intention lock is actually a gap lock, which is generated during insert. Allow multiple transactions to insert different data values ​​in the same index gap at the same time

Three, page lock

 The engine BDB is used. The entire table is directly locked, and other processes cannot write to the table. If it is a write lock, other processes are also not allowed to read.

 

other:

(1) View the method of transaction data about locks in InnoDB monitoring

Use the SHOW ENGINE INNODB STATUS command

(2) Methods to reduce lock conflicts or deadlocks:

  1. Use a lower isolation level.
  2. Use index to access data.
  3. The transaction is as small as possible.
  4. When explicitly locking, it is best to request a sufficient level of lock at a time. For example: when modifying data, directly request an exclusive lock. Instead of requesting a shared lock first, and then request an exclusive lock. Prone to deadlock.
  5. Do not apply for more than the actual lock level required. Explicit locking is not recommended. For example: row lock can be solved, must apply for table lock
  6. For specific transactions, you can directly apply for table locks to improve processing speed and reduce the possibility of deadlocks.

 

 

Guess you like

Origin blog.csdn.net/sumengnan/article/details/112727820