Introduction to MySQL shared locks, exclusive locks, record locks, gap locks and table locks and SQL examples

MySQL lock type

In MySQL, there are various types of locks used to manage concurrent access and ensure data consistency. Here are some common MySQL lock types and their descriptions:

  1. Shared lock (Shared Lock):

    • A shared lock allows multiple transactions to read a resource at the same time, but does not allow concurrent transactions to write to the resource.
    • Multiple transactions can hold shared locks at the same time, and shared locks are not mutually exclusive.
    • Example: Multiple transactions read data from the same table at the same time.
  2. Exclusive lock (Exclusive Lock):

    • An exclusive lock allows only one transaction to hold the lock exclusively, and no other transaction can hold the lock at the same time.
    • An exclusive lock is used for writing operations on a resource, and other transactions cannot read or write to the resource at the same time.
    • Example: A transaction updates a row of data, and other transactions cannot read or write the row of data at the same time.
  3. Record Lock (Record Lock):

    • A record lock is a lock for a row of data in a table, and is used to control concurrent access to the row of data.
    • A record lock can be a shared lock or an exclusive lock, depending on the transaction's read and write operations on the data.
    • Example: Transaction A acquires a shared lock on a row of data in the table, and transaction B cannot acquire an exclusive lock until transaction A releases the lock.
  4. Gap Lock:

    • Gap locks are used to lock gaps between index ranges to prevent other transactions from inserting data in the range.
    • Gap locks are used to prevent phantom reads and ensure the consistency of range queries.
    • Example: Transaction A locks an index range in the table to prevent other transactions from inserting new data in the range.
  5. Table Lock (Table Lock):

    • Table lock is to lock the entire table, and mutually exclude table-level resources, so that other transactions cannot access the entire table at the same time.
    • Table locks have the largest granularity and have a great impact on concurrency performance. Generally, try to avoid using them.
    • Example: Transaction A writes to the entire table, other transactions cannot read or write to the table at the same time.

These lock types have different usage scenarios and behaviors in MySQL. According to specific business needs and concurrency control requirements, an appropriate lock type can be selected to ensure data consistency and concurrency. In actual development, it is necessary to choose a reasonable lock strategy according to the specific situation to avoid problems such as deadlocks, performance bottlenecks, and concurrency conflicts.

SQL example

The following is sample SQL for the five locks mentioned above:

  1. Shared lock (Shared Lock):

    -- 事务A获取共享锁
    START TRANSACTION;
    SELECT * FROM table_name WHERE condition FOR SHARE;
    -- 事务B尝试获取共享锁(可以同时获取)
    START TRANSACTION;
    SELECT * FROM table_name WHERE condition FOR SHARE;
    
  2. Exclusive lock (Exclusive Lock):

    -- 事务A获取排他锁
    START TRANSACTION;
    SELECT * FROM table_name WHERE condition FOR UPDATE;
    -- 事务B尝试获取排他锁(等待事务A释放锁)
    START TRANSACTION;
    SELECT * FROM table_name WHERE condition FOR UPDATE;
    
  3. Record Lock (Record Lock):

    -- 事务A获取记录锁(共享锁)
    START TRANSACTION;
    SELECT * FROM table_name WHERE condition FOR SHARE;
    -- 事务B尝试获取记录锁(排他锁)
    START TRANSACTION;
    SELECT * FROM table_name WHERE condition FOR UPDATE;
    
  4. Gap Lock:

    -- 事务A获取间隙锁
    START TRANSACTION;
    SELECT * FROM table_name WHERE indexed_column BETWEEN value1 AND value2 FOR UPDATE;
    -- 事务B尝试在间隙内插入新数据(被阻塞)
    START TRANSACTION;
    INSERT INTO table_name (column1, column2) VALUES (value1, value2);
    
  5. Table Lock (Table Lock):

    -- 事务A获取表级锁
    LOCK TABLES table_name WRITE;
    -- 事务B尝试读取或写入整个表(被阻塞)
    START TRANSACTION;
    SELECT * FROM table_name;
    

The above examples show the basic usage of various locks, pay attention to the selection of lock statements and lock types. The specific use of locks needs to be adjusted according to actual needs and requirements of concurrency control.

Guess you like

Origin blog.csdn.net/a772304419/article/details/131039960