当前位置:网站首页>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 :

  1. MySQL The lock is added to the index record .
  2. 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 .
  3. 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

原网站

版权声明
本文为[Steen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206091541474769.html