An article to understand MySQL locks

1 Introduction to MySQL locks

1.1 What is a lock

A lock is a mechanism used by a computer to coordinate concurrent access to the same shared resource among multiple processes. In order to ensure the consistency and validity of data access and other functions in MySQL, a lock mechanism is implemented. Locks in MySQL are implemented at the server layer or storage engine layer.

1.2 What problems are locks used to solve

Locks are used to solve the access problems of concurrent transactions. We already know the various problems that may arise when transactions are executed concurrently. The biggest difficulty is: on the one hand, we must maximize the use of concurrent access to the database; on the other hand, we must ensure that Each user can read and modify data in a consistent manner, especially if one transaction is doing the reading operation and another is making the modification operation at the same time.

One transaction performs a read operation, and the other performs a modification operation. As we said earlier, problems such as dirty reads, non-repeatable reads, and phantom reads may occur in this case.

How to solve the problems of dirty reads, non-repeatable reads, and phantom reads? There are actually two possible solutions:

Option 1: MVCC for read operations and lock for write operations

This solution has better performance, but may read old version records

Solution 2: lock both read and write operations

The performance of this solution is average, but the latest records can be read every time. For example, in banking scenarios, the security requirements are very high

2 Classification of locks

There are many locks in MySQL, which can be divided into the following types according to the mode and granularity

image-20221003154244472

3 Optimistic and pessimistic locking

3.1 Optimistic lock

1. Concept

Optimistic lock, as the name suggests, is very optimistic. Optimistic lock believes that data will not cause conflicts under normal circumstances, so it will be detected when the data is submitted for update.

2. Realize

Optimistic locking is implemented by the basic version number mechanism. A version field is added to the data table, and the version is read together when reading data. Every time the data is updated, the version field value + 1. When the modification needs to be submitted, compare the version number at the time of reading with the current version number of the database. If it is consistent, it means that no one has modified this record during this period. If it is inconsistent, it means that it has been modified and the submission fails.

3. Applicable scenarios

Optimistic locking is suitable for scenarios with many read operations and few write operations

3.2 Pessimistic lock

1. Concept

Pessimistic lock is more pessimistic than optimistic lock. Pessimistic lock believes that data will be modified every time it is operated, so it will add a lock every time it operates data.

2. Realize

Pessimistic locks are implemented through shared locks and exclusive locks (these two locks will be discussed below)

3. Applicable scenarios

Applicable to scenarios with low concurrency, many write operations, and few read operations

4 shared exclusive lock

4.1 Shared lock

1. Concept

Shared lock, also known as read lock, referred to as S lock. When a transaction adds a read lock to the data, other transactions can only add a read lock to the data, not a write lock.

2. Realize

Shared lock locking method: select …lock in share mode

image-20221003172941494

image-20221003173149274

4.2 Exclusive lock

1. Concept

Exclusive lock, also known as write lock, X lock for short, when a transaction adds an exclusive lock to the data, other transactions cannot query or modify the data

The MySQL InnoDB engine defaults to update, delete, and insert will automatically add an exclusive lock to the data involved, and the select statement will not add any lock type by default.

2. Realize

Exclusive lock locking method: select … for update

image-20221003173825780

image-20221003173942828

5 granular locks

5.1 Global lock

1. Concept

Global lock, as you can understand from the name, a global lock locks the entire MySQL database instance. During the lock period, any addition, deletion, or modification of the database cannot be performed.

2. Realize

MySQL provides a way to add a global read lock, the command isFlush tables with read lock (FTWRL)

3. Applicable scenarios

Full database data backup, you can use the global lock, do not use it in other cases

5.2 Table-level locks

1. Concept

Table-level lock, lock the table currently being operated, both MyISAM and InnoDB engines support table-level locking

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)

2. Realize

Add table lock: lock table read/write

image-20221003175254955

image-20221003175706554

Unlock the table:

Step 1: Find the locked table

show processlist

image-20221003180108421

Step 2: kill the process that drops the locked table

kill 21;
kill 22;

Update user table data again

image-20221003180453415

Can be updated normally

5.3 Page-level locks

Page-level lock is a kind of lock in MySQL whose locking granularity is between row-level lock and table-level lock. Table-level locks are fast, but have more conflicts, and row-level locks have fewer conflicts, but are slower. Therefore, a compromised page-level lock is adopted to lock a group of adjacent records at a time. The BDB engine supports page-level locks

5.4 Row-level locks

1. Concept

Row-level locks are the most fine-grained locks in MySQL, with the lowest probability of lock conflicts, but locking is slow and expensive

Only the InnoDB engine supports row locks in MySQL, and others do not

2. Realize

In MySQL, row-level locks are not inter-lock records, but lock indexes. MySQL will automatically add row locks when executing update and delete statements

image-20221003181021358

image-20221003181133774

6 intent lock

1. Concept

Intention locks are table locks. In order to coordinate the relationship between row locks and table locks, multi-granularity (table locks and row locks) locks coexist.

2. Function

When there is a row lock in transaction A, MySQL will automatically add an intent lock to the table. If transaction B wants to apply for a write lock on the entire table, it does not need to traverse each row to determine whether there is a row lock, but directly judge whether there is an intent lock. Enhance performance.

3. Compatibility

Intent shared lock (IS) Intent exclusive lock (IX)
shared lock compatible mutually exclusive
exclusive lock mutually exclusive mutually exclusive

7 Gap Pro Key Record Lock

1. Concept

Record locks, gap locks, and adjacent key locks are all exclusive locks, and the use of record locks is consistent with the introduction of exclusive locks.

7.1 Record locks

A record lock is a blocked record, and a record lock is also called a row lock, for example:

select * from user where id = 1 for update;

It will add a record lock on the record with id=1 to prevent other transactions from inserting, updating, and deleting the row with id=1.

7.2 Gap lock

Gap locks are based on non-unique indexes, which lock a range of index records. Gap lock is used to lock an interval, not just every piece of data in this interval

select * from user where id < 10 for update;

That is, all record rows in the interval [1, 10) will be locked, and the insertion of all data rows with ids of 1, 2, 3, 4, 5, 6, 7, 8, and 9 will be blocked

image-20221003182325290

image-20221003182340884

7.3 Pro key lock

Proximity key lock is a combination of record lock and gap lock. Its blocking range includes both index records and index intervals, which is a left-open and right-close interval. The main purpose of the key lock is also to avoid phantom reads (Phantom Read). If the isolation level of the transaction is downgraded to RC, the key lock will also fail.

There will be a temporary key lock on the non-unique index column on each data row. When a transaction holds the temporary key lock of the data row, it will lock a section of data in the left-open and right-close interval. It should be emphasized that row-level locks in InnoDB are implemented based on indexes, and temporary key locks are only related to non-unique index columns, and temporary key locks do not exist on unique index columns (including primary key columns).

Guess you like

Origin blog.csdn.net/wgzblog/article/details/127281354