MySQL row lock

background

Since the Chinese New Year on duty, I have been reading the book "45 Lectures on MySQL Practice" at the company. At that time, I read MySQL's global locks, table-level locks, row locks and gap locks. The row locks and gap locks among them have been incomprehensible for a long time. Recently, I read it again while I have time, and compared "Learning MySQL in a Simple Way", and found that the understanding of MySQL locks has reached a new level. Today, I will share the rows of MySQL. lock.

Row lock

MySQL row locks are divided into shared locks (S locks) and exclusive locks (X locks).

Normal select statement, InnoDB does not add any lock, we call it snapshot read

  • select * from test;

By adding S lock and X lock select statement or insert/update/delete operation, we call it the current read

  • select * from test lock in share mode;
  • select * from test for update;
  • insert into test values(…);
  • update test set …;
  • delete from test …;

Special note: The current reading above is the latest version of the record. All read records will be locked, except for the first statement lock in share mode, which adds S locks (shared locks) to the records, all other operations are X locks (exclusive locks) .

Two-phase lock protocol

Traditional relational database locking must follow a principle: the two-stage locking principle.

The two-phase lock divides the lock operation into two phases, the locking phase and the unlocking phase, and ensures that the locking phase and the unlocking phase do not intersect.

Share an example to illustrate the two-phase lock protocol:

Transaction A Transaction B
begin;
update test set name=‘god-jiang’ where id=1;
begin;
delete from test where name=‘god-jiang’;
commit;

After transaction A executes the update statement, it holds the X lock with name='god-jiang', and transaction B executes the delete statement to delete the record name='god-jiang', it will block until after transaction A executes commit , Transaction B will execute delete.

That is to say, in the InnoDB storage engine, the row lock is added when needed, but it is not needed to release the row lock immediately when it is not needed, but to be released when the transaction ends. This is the two-phase lock protocol .

Transaction isolation level

The row locks corresponding to different transaction isolation levels are also different, so we need to understand the transaction isolation level first, and then demonstrate how to add row locks with different isolation levels.

The 4 isolation levels of MySQL:

  1. READ UNCOMMITTED (read uncommitted): In any transaction, you can see the execution of other transactions, and dirty reads will occur.
  2. READ COMMITTED (read committed, referred to as: RC): In the current transaction, only the execution result of the committed transaction can be seen. When a new commit operation occurs during the read period of the same transaction, the phenomenon of non-repeatable read will occur.
  3. REPEATABLE READ (repeatable read, abbreviation: RR): This is the default isolation level of MySQL. Thanks to MVCC, it can see the same data rows when multiple instances of the same transaction read data concurrently, eliminating dirty reads, Non-repeatable read, no phantom read will occur by default ( MySQL row lock + gap lock solves the phantom read of snapshot read, but not the phantom read of current read )
  4. SERIALIZABLE (serial): MySQL's highest isolation level, through locking, forced transaction execution order, to ensure that there will be no phantom reading problem.

Locking analysis (the following default is the RC isolation level and all are currently read)

Here I have selected three common situations under the RC isolation level to analyze how SQL locks:

  • RC isolation level, where field has no index
  • RC isolation level, where field has a unique index
  • RC isolation level, where field has ordinary index

The following lock analysis default table name is test, primary key is id, unique index is a, ordinary index is b, and no index is c.

RC isolation level + where no index

img

Since the c field is not indexed, SQL will perform a full table scan. All records at this time will add X locks.

Why not just lock the record with c=10?

This is because in MySQL, if the where condition cannot be quickly filtered by the index, then all the records will be locked in the MySQL server layer and then the InnoDB storage engine will be called to query, so all the records will be locked.

Summary: Without an index, InnoDB's current read will lock all records. Therefore, in the actual development, if it is the current read or insert/update/delete operations, the index must be used, otherwise a large number of lock waits will be generated .

RC isolation level + where unique index

img

Since the a field has a unique index, the MySQL server layer will choose the index of column a to filter. After finding a=2 record, add X lock to the index record of a=1 on the unique index, and read the primary key id at the same time And find the clustered index tree to add X lock to the record with id=2.

Summary: If the query condition is a unique index, then SQL adds X locks on the records of the unique index that are satisfied, and adds X locks on the corresponding clustered index .

RC isolation level + where ordinary index

img

Since the b field has a common index, X locks are added to all records that satisfy b=2, and X locks are also added to the corresponding clustered index records. Compared with the unique index, the unique index query has at most one row of records locked, while the ordinary index will lock all the records that meet the conditions.

Summary: If the query condition is a normal index, then SQL will add X locks to the non-unique index records that meet the conditions, and add X locks on their corresponding clustered indexes .

to sum up

  • InnoDB supports row locks, which is one of the important reasons to replace the MyISAM storage engine
  • Shared two-phase lock protocol and MySQL transaction isolation level
  • Analyzed the common current read lock situation under the RC isolation level

The most important thing is that in the RC isolation level, we update data, insert data, and delete data as much as possible to go through the index, otherwise all records will be X locks, if online operations, it will seriously affect Business .

Reference

  • "MySQL 45 Lectures" Lin Xiaobin
  • "Introduction to MySQL" 20.3.4 InnoDB row lock implementation

Guess you like

Origin blog.csdn.net/weixin_37686415/article/details/114711276