当前位置:网站首页>The interviewer asked me how many locks were added to an update statement? I summarized the whole eight part essay
The interviewer asked me how many locks were added to an update statement? I summarized the whole eight part essay
2022-06-09 16:07:00 【Steen】
Here comes your interviewer , Wearing a plaid shirt , With a beer belly , A middle-aged man whose hairline has moved back seriously . Holding a thermos cup soaked with Chinese wolfberry , With arms MacBook,MacBook There are company slogans on the :“ I love working overtime ”.

The interview begins , Go straight to the point .
interviewer : Look at your resume. It says master MySQL, Let me ask you a question MySQL Lock related issues , Look at this one SQL What data will be locked ?
update user set name=' A lamp ' where age=5;The table structure is like this :
CREATE TABLE `user` (
`id` int NOT NULL AUTO_INCREMENT COMMENT ' Primary key ',
`name` varchar(255) DEFAULT NULL COMMENT ' full name ',
`age` int DEFAULT NULL COMMENT ' Age ',
PRIMARY KEY (`id`),
KEY `idx_age` (`age`)
) ENGINE=InnoDB COMMENT=' User table ';I : age Is non unique index ,MySQL The lock of is added to the index , Should only be right age=10 Lock your data .
interviewer : Are you sure? ?
I : Um. ..., Should be yes .
interviewer : 【 Taunt 】, This is what you master MySQL The level of ? Let's stop here for today's interview , I will contact you when there is news later .
Could there be any news later ?
When did you contact me on your own initiative ?
The truth is rejected , Instead of learning the eight part essay by heart, he was admitted .
ok , Let me see how Yideng summed it up MySQL The eight part essay of .
I : This article SQL Which data are locked , You also need to look at the data in the table .
MySQL There are three types of row locks :
Record locks (Record Locks):
That is, lock a record .
# Yes id=1 The user of is locked
update user set age=age+1 where id=1;Clearance lock (Gap Locks):
That is, lock a certain range , But the critical data that does not include the range .
# Yes id Greater than 1 And less than 10 The user of is locked
update user set age=age+1 where id>1 and id<10;above SQL The lock range of is (1,10).
Temporary key lock (Next-Key Locks):
It consists of record lock and clearance lock , Include both the record itself and the scope , Left open right closed interval .
# Yes id Greater than 1 And less than or equal to 10 The user of is locked
update user set age=age+1 where id>1 and id<=10;If there are only two pieces of data in the table :
id | name | age |
1 | Zhang San | 1 |
10 | Li Si | 10 |
in the light of age Indexes , There are three index ranges :
(-∞,1],(1,10],(10,+∞)
This one just now SQL:
update user set name=' A lamp ' where age=5;Because... Does not exist in the table age=5 The record of , also age=5 Just landed on (1,10] Within the range of , So it will be right (1,10] Range locking of .
We can test it with actual data :

When we execute update At the time of statement ,age=2 and age=8 The data range of is locked .
interviewer : The young man answered well . If it already exists age=5 The data of , The one just now update Statement will lock what data ?
I : If the data in the table is like this .
id | name | age |
1 | Zhang San | 1 |
5 | One light architecture | 5 |
10 | Li Si | 10 |
in the light of age Indexes , It is easy to generate such four index ranges :
(-∞,1],(1,5],(5,10],(10,+∞)
This one just now SQL:
update user set name=' A lamp ' where age=5;age=5 The data of this project falls in (1,5] Within the range of , So it will be right (1,5] Range locking of .
Do you think it's over ?MySQL Lock to ensure data security , It will also traverse to the right until the condition is not satisfied , A clearance lock will be added , That is to say (5,10] The scope of the .
therefore , This article SQL The lock return of is (1,5] and (5,10] .
Just now age=5 Nonexistent locking range (1,10] It's the same . If you don't believe me, you can run through the test cases just now .

interviewer : There's something about the lad . If I put SQL in where Replace the condition with the primary key ID, What is the locking range ?
update user set name=' A lamp ' where id=5;I : Because the lock is added to the index .
If it doesn't exist id=5 The data of , The locking range follows SQL It's the same , (1,10] .
If there is id=5 The data of ,MySQL Of Next-Key Locks It will degenerate into Record Locks , That is, only in the id=5 Lock this line of records .
interviewer : Young man , The chance to get a raise is for someone like you . Salary double, Come to work tomorrow .
Summary of knowledge points :
- MySQL The lock is added to the index record .
- If it is a non unique index , Whether or not the record exists in the table , In addition to locking the range of the record , It will also traverse to the right to lock the range that does not meet the conditions .
- If it's the only index , If the record exists in the table , Lock only this row of records . If the record does not exist in the table , In addition to locking the range of the record , It will also traverse to the right to lock the range that does not meet the conditions .
More interview questions and answers
边栏推荐
- May training (day 31) - status compression
- 构建从智能质检到对话分析的一体化智能对话分析平台 ,杭州银行客服中心打造智慧运营新名片
- Defi development logic of DAPP system
- How does the memory database give full play to its memory advantage?
- 67 2D drawing (antialiasing drawing / antialiasing rendering)
- 容器和镜像的区别
- Close the privacy collection window of stackexchange and other platforms
- ps填充快捷键是什么
- How to solve the problem that Epson printer cannot print
- Conference control keyboard instructions for ≥ 3.5-inch LCD screen
猜你喜欢

Experience sharing of technical we media realization -- starting to try to make CSDN's reply one year later

数据安全刻不容缓,国产智能化厂商首获SOC 2鉴证报告有何意义?

SAP Commerce Cloud 构建过程中的文件夹可写入性问题分析

PS how to copy layers

wps斜线表头并分别打字怎么实现

618. How to prepare for the great promotion

65 2D drawing (basic drawing and filling)

SaaS tenant multi store system in the catering industry accelerates the digital operation of catering and realizes cost reduction and efficiency increase

JWT思维导图

重构要点学习
随机推荐
May training (day 28) - Dynamic Planning
五月集训(第23天) —— 字典树
Wechat applet uses canvas to draw, save and download locally. Round head, dotted line network picture
Garymarcus publicly shouted that Hinton and musk: deep learning is like hitting the wall. I bet 100000 dollars
65 2D绘图(基本绘制和填充)
六月集训(第04天) —— 贪心
Cordova 开发,提示net::ERR_CLEARTEXT_NOT_PERMITTED
LeetCode 327. Number of interval sums
Mysql进阶优化篇01——四万字详解数据库性能分析工具(深入、全面、详细,收藏备用)
openwrt 防火墙
PS how to zoom in and out a picture
70 DOM reading XML
[error reporting] no module named 'torchvision‘
You really can't be poor any more. There are 400 film and television clips a day. It all depends on these tools
【报错】Cannot uninstall ‘certifi‘. It is a distutils installed project and thus we cannot accurately
数据存储需求多样化加剧,分而治之成大势所趋
LeetCode 1420. Generate array
Paper supply chain collaborative management system: full link digitalization, realizing intelligent order management and control of supply chain platform
The demand for data storage is increasingly diversified, and it is a general trend to divide and conquer
打新债开户安全么,怎么开户