Home Database Mysql Tutorial The connection between mysql locks and indexes

The connection between mysql locks and indexes

Mar 17, 2018 am 10:33 AM
mysql index connect

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 of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

PHP's big data structure processing skills

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

How to optimize MySQL query performance in PHP?

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

How to use MySQL backup and restore in PHP?

What are the application scenarios of Java enumeration types in databases? What are the application scenarios of Java enumeration types in databases? May 05, 2024 am 09:06 AM

What are the application scenarios of Java enumeration types in databases?

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into a MySQL table using PHP?

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

How to fix mysql_native_password not loaded errors on MySQL 8.4

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

How to use MySQL stored procedures in PHP?

Performance optimization strategies for PHP array paging Performance optimization strategies for PHP array paging May 02, 2024 am 09:27 AM

Performance optimization strategies for PHP array paging

See all articles