What is a MySQL gap lock

1. The words written in front

At present, all walks of life are involved in flying, especially in the IT industry. For many programmers, the difficulty, depth, and breadth of the interview make you bear "everything you shouldn't bear at your age". Take MySQL as an example. When I just graduated from university, I asked during the interview "How to deduplicate MySQL queries?" The architecture of MySQL", "Have you seen the source code of MySQL?". Thinking about it now, I am really glad that I graduated early. With the level of college graduation at that time, I would starve to death to find a job now.

Storage (database storage or memory storage) is the most important part of any system. They provide all the data needed for any system to run. Programmers work directly or indirectly with various storages almost every day. Because of this, storage must be the "hardest hit area" during interviews.

2. MySQL gap lock

The first time I heard about MySQL's gap lock was when I went to a company for an interview in 2016. The whole process was very pleasant and smooth. But the buddy in the interview suddenly asked, "Do you know MySQL's gap lock?", I quickly extracted various locks in my mind: "pessimistic lock", "optimistic lock", "row lock", "table lock", "Pessimistic lock", "optimistic lock"... Axi, what the hell is a gap lock? !

Then I could only answer helplessly: "I'm not very clear about this", and then the interview was over... After I came out of that company, I sat by the flower bed and checked all kinds of things with my mobile phone. After searching, I have a preliminary understanding of gap locks. In the next few days, I have been spending time to understand gap locks in depth.

The author believes that if you want to understand a certain concept in depth, you must understand the meaning of this thing. What problem was it created to solve? Then take these questions to find the answer. When the answers are solved layer by layer, you will feel refreshed and your whole body will feel full of energy.

Before I explain to you what gap locks are and what problems they can solve, I will take you to clear up some relevant knowledge: the isolation level of transactions , snapshot reads and current reads.

2.1 Transaction isolation level

There are four isolation levels for transactions, and the problems that each isolation level will cause are shown in the following table:

image.png

Some people may ask, what is the relationship between gap locks and isolation levels? In fact , the emergence of gap locks is to solve the phantom reading problem caused by repeatable reading in the current reading in the InnoDB storage engine!

2.2 Snapshot read and current read

2.2.1 Snapshot read

Before explaining the snapshot read, let's take a look at the author's SQL operation.

We know that MySQL's default isolation level is repeatable read. According to the serial number marked in red, there is no phantom reading problem. What is the reason? This is because MySQL solves the phantom read problem under the isolation level of repeatable read through snapshot read! So what is snapshot read?

Snapshot reading, simply put, means that there are many versions of data. When transactions are executed concurrently, a transaction reads one of the snapshots. The author uses another example that may not be very appropriate but is easier to understand to illustrate, that is, when a transaction or query starts, some copies are generated for the data to be queried, and then the data of the copies is obtained in subsequent queries.

2.2.2 Current read

The current read means that the latest data in the database is always queried, and there is no snapshot. The following statements apply:

  1. select ... from table_name in share mode;
  2. select ... from table_name for update;
  3. insert;
  4. update;
  5. delete

2.3 Gap lock

In the chapter  2.1 Isolation Level of Transactions  , we mentioned that the gap lock is to solve the phantom reading problem caused by repeatable reading in the InnoDB storage engine, so what exactly is a gap lock? Let's first look at the following operations:

For the second query SQL statement in the figure:  

select * from foo where id < 10 and id > 5 for update;

The author asks two questions:

  1. The SQL is obviously the current read, and the current read reads the latest data in the database. So now there is a question, at this time, if another transaction inserts a piece of data with an id of 8, and then executes the same SQL statement, will there be no phantom reading?
  2. If the result of question 1 is that there is no phantom reading, it must be through the locking mechanism, so how is this locked? So if the data with ids 8, 12, and 17 are inserted, what will happen? Doesn't the id being 12 and 17 cause phantom reading in the query results?

For question one, as shown in the figure below:

image.png

We know that the current read reads the latest data in the database. If the data on the right can be submitted, the query on the left will inevitably cause phantom reading problems, so the transaction will be prevented from being submitted by locking, and this lock is a gap lock. When you understand the second question, I am convinced that you will understand what a gap lock is.

For question 1, observe the following two pictures in turn:

The reason for the results in the above two figures is that because the data ids in the foo table are 1, 4, 7, 9, 15, and 18, when the query id is between (5,10), not the entire database table is locked , but look for the id value with the largest id value from the lower limit (5) and the smallest id value from the upper limit (10), which are 4 and 15 respectively; then the range of the lock is (4, 15], that is to say, the id in this interval is It cannot be inserted, so it leads to the results of the above two pictures.

Now you know what the gap lock is all about? If you still don't understand, you can leave a message in the comment area to discuss. Of course, you can also join our learning exchange group to interact and learn with everyone.

Guess you like

Origin blog.csdn.net/GUDUzhongliang/article/details/131914624