Home  >  Article  >  Database  >  The connection between mysql locks and indexes

The connection between mysql locks and indexes

小云云
小云云Original
2018-03-17 10:33:362285browse

When we usually use mysql locks, we rarely consider the efficiency of locks when we first come into contact with the database. Generally, we only want to achieve the purpose of preventing concurrency. However, as the amount of data increases, we will find that there are many We have written sql that is very optimized, but sometimes it is still very slow and it is difficult to find the reason. At this time, we should consider whether it is caused by the lock of mysql.

We first create a new data table:


Here our primary key has an index by default ;Add a few pieces of data here


Then we open two processes for testing:


First add a where condition that does not involve an index lock:


Then we update the data of this row in the second window, we will It is found that this operation will be stuck,



Then when we submit the transaction, we will find the data of the second window It will be executed immediately.

It seems that there is no problem from the above. This does achieve the purpose we want, but you can add the same lock and try to update other data such as what I executed below. Data:

I tested the above three situations using the same process. They will all be stuck, so the problem arises. We are actually locking name='test name'. Maybe we just want to lock the two rows id=133 and id=134. We don't want to lock 135, 136, 137, but we can't access these three rows because our lock is a table lock. Let's try another lock. When using the index:

The index was used when using the lock above, but we were still stuck when updating the data; here we will feel that the index is actually also It's useless, but if you encounter the same lock and use the index when updating the data, you can see the effect:


We will find that this is not locked. Live, updated directly;

The following is a summary: if our lock uses an index, it is a row lock, if it does not use an index, it is a table lock, but the data we operate must use a lock. ;

Let’s talk about why this is the case:

First of all, we know that if there is no index, we will use it when selecting or positioning data. It is carried out in the form of a full table scan, which will form a table lock. If there is an index, it will directly locate the specified row, which will form a row lock. However, be aware that when you update the data, if you do not use the index, the entire table will be locked. Table scan, when the locked row is scanned, it will also be locked, so the desired effect cannot be achieved;

Related recommendations:

mysql Lock mechanism_MySQL

MySQL lock usage table-level lock

How to optimize MySQL lock

The above is the detailed content of The connection between mysql locks and indexes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn