Written in front A database is essentially a shared resource, so while maximizing concurrent access performance, it is still necessary to ensure that each user can read and modify data in a consistent manner. Locking is the best weapon to solve this kind of problem. First, create a new table test, with id as the primary key, name as the secondary index, and address as the unique index. CREATE TABLE `test` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` int(11) NOT NULL, `address` int(11) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `idex_unique` (`address`), KEY `idx_index` (`name`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4; Row locks in INSERT methods It can be seen that if two transactions successively perform INSERT operations on row records with the same primary key, because transaction A obtains the row lock first, transaction B can only wait until the row lock is released after transaction A is committed. Similarly, if you insert data into the unique index field address, you also need to acquire a row lock. The diagram is similar to the primary key insertion process and will not be repeated here.
The Update method has similar results to the Insert method. Table locks and row locks under SELECT FOR UPDATE The SELECT FOR UPDATE statement of transaction A will obtain the Table Lock of table test. At this time, transaction B will be blocked when performing the insert operation. Only after transaction A commits and releases the table lock can transaction B obtain the corresponding row lock and perform the insert operation.
Gap locks under secondary indexes First look at the data under the test table: mysql> select * from test; +----+------+---------+ | id | name | address | +----+------+---------+ | 3 | 1 | 3 | | 6 | 1 | 2 | | 7 | 2 | 4 | | 8 | 10 | 5 | +----+------+---------+ 4 rows in set (0.00 sec) Gap lock can be said to be a type of row lock. The difference is that it locks records within a range. Its function is to avoid phantom reads, that is, the sudden increase or decrease of interval data entries. The main solutions are:
The conditions for InnoDB to automatically use gap locks are:
When InnoDB scans index records, it first adds a row lock to the selected index row record, and then adds a gap lock to the gaps on both sides of the index record (scanning to the left to find the first value smaller than the given parameter, and scanning to the right to find the first value larger than the given parameter, thereby building an interval). If a gap is locked by transaction A, transaction B cannot insert records into this gap.
Let's take a look at an example. When we create a table, we specify the name column as the auxiliary index. Currently, the values of this column are [1,2,10]. The gap ranges are (-∞, 1], [1,1], [1,2], [2,10], [10, +∞) Round 1:
Round 2:
Round 3:
Summary of InnoDB Lock Mechanism References
Summarize The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. You may also be interested in:
|
<<: Implementing simple chat room dialogue based on websocket
>>: CentOS 8 custom directory installation nginx (tutorial details)
Today someone talked to me about a website develo...
Scenario 1. Maintain a citizen system with a fiel...
The HTTP request methods specified by the HTTP/1....
This article shares the specific code of React to...
Table of contents Basic usage of Promise: 1. Crea...
This article shares the specific code of videojs+...
Dockerfile is a text file used to build an image....
This article example shares the specific code for...
Use JavaScript to implement a web page clock. The...
The Linux command to run the jar package is as fo...
Effect: Code: <template> <div class=&quo...
Table of contents 1. Event delegation Event Bubbl...
Anyone who has read my articles recently knows th...
Execution problem between mysql max and where Exe...
For sorting, order by is a keyword we use very fr...