An article about Insert Intention Lock in MySQL
Insert Intention Lock, we also call it insert intention lock in Chinese. It is a type of gap lock, specifically for insert operations. The official meaning is that when multiple transactions insert records in the same index and the same range, if the insertion positions do not conflict, they will not block each other. The following article will take you to understand the insertion intention lock in MySQL. I hope it will be helpful to you!
Insert Intention Lock, we also call it insertion intention lock in Chinese.
This can be regarded as a supplement to the Gap Lock we talked about before. Regarding Gap Lock, if you still don’t understand, you can refer to: Record Lock, Gap Lock and Next-Key Locks.
1. Why do we need to insert intention locks
We already had Gap Lock before. Gap Lock can help us solve the phantom read problem to a certain extent. However, the previous one seems to have some problems.
Suppose I have the following table:
CREATE TABLE `user` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `username` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `age` int(11) NOT NULL, PRIMARY KEY (`id`), KEY `age` (`age`) ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
id is an auto-incremented primary key; age is an ordinary index. Now there is the following data in the table:
Suppose I want to execute the following insert SQL:
begin;insert into user(username,age) values('wangwu',95);复制代码
Note that this SQL is executed but the transaction has not yet been committed.
According to the knowledge we learned about Gap Lock before, the range of Gap Lock at this time is (89,99), which means that no age in this range can be inserted.
If this is the case, friends will find that the efficiency of data insertion is too low, and lock conflicts are prone to occur. So what should we do?
The insertion intention lock we are going to introduce today is used to solve this problem.
2. What is an insert intention lock?
Let’s take a look at the introduction on the MySQL official website:
An insert intention lock is a type of gap lock set by INSERT operations prior to row insertion. This lock signals the intent to insert in such a way that multiple transactions inserting into the same index gap need not wait for each other if they are not inserting at the same position within the gap . Suppose that there are index records with values of 4 and 7. Separate transactions that attempt to insert values of 5 and 6, respectively, each lock the gap between 4 and 7 with insert intention locks prior to obtain the exclusive lock on the inserted row , but do not block each other because the rows are nonconflicting.
The rough translation is this:
The insertion intention lock is a gap set before the INSERT operation Lock, insertion intention lock represents an insertion intention, that is, when multiple different transactions insert data into the same gap of the same index at the same time, they do not need to wait for each other, that is, they will not block (if you simply follow According to the previous gap lock theory, you must wait for a gap lock to be released before the next transaction can insert data into the same gap). Suppose there are index records with values 4 and 7. Now there are two transactions trying to insert records with values 5 and 6 respectively. Each transaction uses an insert intent lock to lock between 4 and 7 before obtaining an exclusive lock on the inserted row. gap, but the two transactions will not block each other because the rows do not conflict.
This is to insert the intention lock.
3. Practice
Friends, please note that Brother Song talked about Gap Lock with everyone before and said that this is the isolation level of REPEATABLE READ. A unique product, so now Insert Intention Lock is a special Gap Lock, which of course also takes effect under the isolation level of repeatable read.
Next, we will demonstrate the insertion of intention locks through two simple cases.
3.1 Case 1
Our table structure and data are consistent with the first section.
First we execute the following code in session A:
Now the transaction in session A is not committed.
Next we also perform an insert operation in session B:
We found that session B can also be executed normally without blocking.
This shows that the two insertion intention locks are compatible and can coexist.
3.2 Case 2
Let’s look at another incompatible example.
First execute the following SQL in session A to query records with age greater than 80, and add an exclusive lock:
Next in session B, execute the following Code to insert a row of data:
Friends, you see, this operation will be blocked! The reason for blocking is that the insertion intention lock and the exclusive lock are mutually exclusive.
Taking advantage of the blocking situation, in session C, we use the show engine innodb status\G
command used in the previous article to check the locking situation. Key points Look at the TRANSACTION node:
In the output content, the place where the red box is selected clearly indicates the existence of the insertion intention lock.
4. Summary
To summarize:
Inserting an intention lock Although the name has the word intention, in fact It is a special gap lock.
Insertion intention locks are not mutually exclusive.
Insert mutual exclusion between intention lock and exclusive lock.
Okay, if you have any questions, please leave a message for discussion.
[Related recommendations: mysql video tutorial]
The above is the detailed content of An article about Insert Intention Lock in MySQL. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











MySQL and phpMyAdmin can be effectively managed through the following steps: 1. Create and delete database: Just click in phpMyAdmin to complete. 2. Manage tables: You can create tables, modify structures, and add indexes. 3. Data operation: Supports inserting, updating, deleting data and executing SQL queries. 4. Import and export data: Supports SQL, CSV, XML and other formats. 5. Optimization and monitoring: Use the OPTIMIZETABLE command to optimize tables and use query analyzers and monitoring tools to solve performance problems.

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

To safely and thoroughly uninstall MySQL and clean all residual files, follow the following steps: 1. Stop MySQL service; 2. Uninstall MySQL packages; 3. Clean configuration files and data directories; 4. Verify that the uninstallation is thorough.

In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.

Efficient methods for batch inserting data in MySQL include: 1. Using INSERTINTO...VALUES syntax, 2. Using LOADDATAINFILE command, 3. Using transaction processing, 4. Adjust batch size, 5. Disable indexing, 6. Using INSERTIGNORE or INSERT...ONDUPLICATEKEYUPDATE, these methods can significantly improve database operation efficiency.

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

Methods for configuring character sets and collations in MySQL include: 1. Setting the character sets and collations at the server level: SETNAMES'utf8'; SETCHARACTERSETutf8; SETCOLLATION_CONNECTION='utf8_general_ci'; 2. Create a database that uses specific character sets and collations: CREATEDATABASEexample_dbCHARACTERSETutf8COLLATEutf8_general_ci; 3. Specify character sets and collations when creating a table: CREATETABLEexample_table(idINT

To implement loose coupling design in C, you can use the following methods: 1. Use interfaces, such as defining the Logger interface and implementing FileLogger and ConsoleLogger; 2. Dependency injection, such as the DataAccess class receives Database pointers through the constructor; 3. Observer mode, such as the Subject class notifies ConcreteObserver and AnotherObserver. Through these technologies, dependencies between modules can be reduced and code maintainability and flexibility can be improved.
