Mysql table lock and row lock

Preface: What is a lock

  • A lock is a mechanism by which a computer coordinates concurrent access to a resource by multiple processes or threads. In the database, in addition to the contention of traditional computing resources (CPU, RAM, I/O), the data is also
  • A resource shared by many users. How to ensure the consistency and validity of concurrent access to data is a problem that all databases must solve, and lock conflicts are also an important factor affecting the performance of concurrent access to databases. From this point of view, locks are particularly important and more complex for databases.

One: global lock

1.1 Concept

  • The global lock is to lock the entire database instance. After locking, the entire instance is in a read-only state. Subsequent DML write statements, DDL statements, and transaction commit statements that have been updated will be blocked.
  • Its typical usage scenario is to do a logical backup of the entire database and lock all tables to obtain a consistent view and ensure data integrity.

1.2 Function

insert image description here
When the full database is backed up, the tables are backed up one by one. If the overall database is not locked, the business system is still writing data. Since some tables have already been backed up, other tables will have data inconsistencies.

1.3 use

  • 加锁:flush tables with read lock
  • Unlock: unlock tables

1.4 Features

Adding a global lock to the database is a relatively heavy operation, and there are the following problems:

  1. If it is backed up on the main library, no updates can be performed during the backup period, and the business basically has to be shut down.
  2. If it is backed up on the slave library, the slave library cannot execute the binary log (binlog) synchronized from the master library during the backup period, which will cause master-slave delay.
    Solution:
    In the InnoDB engine, we can add the parameter -single-transaction during backup to complete the consistent data backup without locking

Two: table-level lock

2.1 Concept

Table-level locks lock the entire table for each operation. The locking granularity is large, the probability of lock conflicts is the highest, and the concurrency is the lowest. It is used in storage engines such as MyISAM, InnoDB, and BDB.

2.2 Classification

2.2.1 Table locks

For table locks, there are two categories:

  1. After the table shared read lock (read lock)
    is added with the read lock, all clients can only read table data and cannot modify table data.
    Lock: lock tables table name... read/write, release lock: unlock tables/client disconnect
  2. After the table exclusive write lock (write lock)
    is added to the write lock, other clients cannot read or write. The current client can read and write

2.2.2 Metadata lock MDL

  • The MDL locking process is automatically controlled by the system and does not need to be used explicitly. It will be added automatically when accessing a table.
  • The main function of the MDL lock is to maintain the data consistency of the table metadata. When there are active transactions on the table, the metadata cannot be written.
  • In order to avoid conflicts between DML and DDL, ensure the correctness of reading and writing.
    Remarks:
    DDL: Data Definition Language
    DML: Data Manipulation Language
    DQL: Data Query Language
    DCL: Data Control Language

2.2.3 Intention lock

In order to avoid conflicts between row locks and table locks added during DML execution, intent locks are introduced in IoDB so that table locks do not need to check whether each row of data is locked, and intent locks are used to reduce table lock checks.
There are two types of intent locks:

  1. Intent Shared Lock (IS): Compatible with Table Lock Shared Lock (read), and mutually exclusive with Table Lock Exclusive Lock (write). Added by the statement select...lock in share mode.
  2. Intent exclusive lock (IX): It is mutually exclusive with the shared lock (read) and the exclusive lock (write) of the table lock. There is no mutual exclusion between intent locks. Added by insert, .update, delete, select...for update.

Three: Row-level locks

Row-level lock, each operation locks the corresponding row data. The locking granularity is the smallest, the probability of lock conflicts is the lowest, and the concurrency is the highest. Applied in the InnoDB storage engine.
InnoDB's data is organized based on indexes, and row locks are implemented by locking index items on the index, rather than locking records.
Row-level locks are mainly divided into the following three categories:

3.1 row lock (Record Lock)

A lock that locks a single row record, preventing other transactions from updating and deleting this row. Supported at both RC and RR isolation levels
insert image description here

  • Locking rules
    insert image description here

3.2 Gap Lock

Lock the index record gap (excluding this record), ensure that the index record gap remains unchanged, and prevent other transactions from inserting in this gap, resulting in phantom reading. It is supported under the RR isolation level.

3.3 Next-Key Lock:

The combination of row lock and gap lock locks the data at the same time, and locks the Gap in front of the data. Supported under RR isolation level.

Four: lock summary

This article introduces the locking mechanism in MySQL, and discusses the concepts, usage methods, and characteristics of global locks, table-level locks, and row-level locks.

  • A global lock locks the entire database instance and is often used for logical backup of the entire database to obtain a consistent view and ensure data integrity. However, the global lock will block the write operation and synchronization process of the database, so the business system may need to be stopped or the master-slave delay will be caused during the backup. The solution is to use the parameter -single-transaction in the InnoDB engine to complete the consistent data backup without locking.

  • Table-level locks lock the entire table for each operation, and are applicable to storage engines such as MyISAM, InnoDB, and BDB. Table-level locks are divided into table shared read locks and table exclusive write locks, which control the read and write operations on the table respectively. In addition, MySQL also introduces metadata locks (MDL) to maintain the consistency of table metadata and avoid conflicts between DML and DDL.

  • Row-level locks lock the corresponding row data for each operation, and are applicable to the InnoDB storage engine. Row-level locks are implemented by locking index items, which are divided into row locks, gap locks, and adjacent key locks. Row locks are used to lock a single row record to prevent other transactions from modifying or deleting the row. Gap locks are used to lock index record gaps to prevent other transactions from inserting data in the gaps, thereby avoiding phantom read problems. Proximity key locks are a combination of row locks and gap locks, and lock data rows and gaps in front of the data at the same time.

To sum up, locks play an important role in ensuring the consistency and effectiveness of concurrent access to data in the database. Different levels of locking mechanisms are suitable for different scenarios, and developers need to choose an appropriate locking strategy according to the actual situation to improve the concurrency performance of the database.

Guess you like

Origin blog.csdn.net/hlzdbk/article/details/131779296