当前位置:网站首页>Myslq ten kinds of locks, an article will take you to fully analyze
Myslq ten kinds of locks, an article will take you to fully analyze
2022-07-01 20:01:00 【Laoliu Xiaochu River】
MYSLQ Ten locks , An article will give you a full analysis
MySQL There are two core knowledge points , Index and lock . The previous articles have explained in detail MySQL Index implementation mechanism , Today, let's study together MySQL Lock of .
1 、 Why lock up ?
When multiple transactions operate on the same batch of data concurrently , If not locked , There is no guarantee of transaction isolation , Finally, the data is disordered .
Locking is to ensure the correctness of data under concurrent operations .
2 、 What are the categories of locks ?
According to the granularity of the lock, it can be divided into : Table locks 、 Page lock 、 Row lock 、 Record locks 、 Clearance lock 、 Temporary key lock
According to the attributes of the lock, it can be divided into : Shared lock 、 Exclusive lock
According to the locking mechanism, it can be divided into : Optimism lock 、 Pessimistic locking
Let's introduce these locks in turn :
Table locks :
MyISAM and InnoDB All engines support table locking .
advantage : Low overhead , Locked fast , A deadlock will not occur .
shortcoming : Strong locking , High probability of lock conflict , Lowest degree of concurrency .
Lock mode :
# Yes user Read lock on the meter
lock table user read;
# At the same time user Read lock on the meter , Yes order Table write lock
lock tables user read, order write;
When do I need a table lock ?
- When you need to update most of the data in the table
- Transactions involve multiple tables , Complex business logic , Adding table lock can avoid deadlock .
Page lock :
advantage : Cost and lock speed are between table lock and row lock .
shortcoming : A deadlock occurs , Lock granularity is between table lock and row lock , The concurrency is average .
At present, only BDB The engine supports page locking , Few application scenarios .
Row lock :
Only InnoDB The engine supports row locking , In addition, the lock is added to the index .
advantage : Spending big , Lock the slow ; A deadlock occurs .
shortcoming : The locking granularity is small , The probability of lock conflict is low , High concurrency .
In addition, record the lock 、 Clearance lock 、 Key lock belongs to row lock .
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;
Shared lock ( Also called read lock 、S lock ):
effect : Prevent other transactions from modifying the current data .
Lock mode :
stay select Add... At the end of the statement lock in share mode keyword .
# Yes id=1 The user adds a read lock
select * from user where id=1 lock in share mode;
Exclusive lock ( Also known as write lock 、X lock ):
effect : Prevent other transactions from reading or updating the current data .
Lock mode :
stay select Add... At the end of the statement for update keyword .
# Yes id=1 The user of adds a write lock
select * from user where id=1 for update;
Optimism lock :
Always assume that others will not modify the current data , So every time the data is read, it will not be locked , Only when updating data version Judge whether others have modified the data ,Java Of atomic The classes under the package use optimistic locks (CAS) Realized .
It is suitable for reading more and writing less .
Lock mode :
Read version
select id,name,age,version from user id=1;
Update data , Judge version Have you ever modified .
update user set age=age+1 where id=1 and version=1;
Pessimistic locking :
Always assume that others will modify the current data , So every time you read , Always lock .
It is suitable for writing more and reading less .
Lock mode :
# Add read lock
select * from user where id=1 lock in share mode;
# Add write lock
select * from user where id=1 for update;
This paper summarizes the knowledge points :
Lock mode :
# Add read lock
select * from user where id=1 lock in share mode;
# Add write lock
select * from user where id=1 for update;
This paper summarizes the knowledge points :
边栏推荐
- PowerDesigner设计Name和Comment 替换
- Realize pyramids through JS (asterisk pyramid, palindrome symmetric digital pyramid)
- tensorflow报错Could not load dynamic library ‘libcudnn.so.8
- STC 32-bit 8051 single chip microcomputer development example tutorial II i/o working mode and its configuration
- 开发那些事儿:EasyCVR平台添加播放地址鉴权功能
- MySQL reports an error can't create table 'demo01 tb_ Student‘ (errno: 150)*
- Bind this of the current scope for callback functions in other cases such as timers and delayers
- Difference between redo and undo
- list大集合等比分割成多个小list集合
- Hls4ml entry method
猜你喜欢
Class loading mechanism
GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
Modsim basic use (Modbus simulator)
Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
渗透工具-TrustedSec 公司的渗透测试框架 (PTF)
走进如心小镇,数智化变革连接“未来社区”
Arduino stepper library drive 28byj-48 stepper motor test program
Related concepts of cookies and sessions
振弦采集模块测量振弦传感器的流程步骤
关于元宇宙下一代入口——脑机接口的实现
随机推荐
3D panoramic model display visualization technology demonstration
Anaconda installs the virtual environment to the specified path
docker ubuntu容器中安装mysql遇到的问题
Iframe 父子页面通信
Sum the amount
再回顾集合容器
Image acquisition and playback of coaxpress high speed camera based on pxie interface
P2433 [deep foundation 1-2] primary school mathematics n in one
Tensorflow reports an error, could not load dynamic library 'libcudnn so. eight
JVM memory model
Servlet knowledge points
开发那些事儿:EasyCVR平台添加播放地址鉴权功能
振弦采集模块测量振弦传感器的流程步骤
ORA-01950
math_利用微分算近似值
Related concepts of cookies and sessions
Interview questions shared in today's group
math_ Use differentiation to calculate approximate value
C#联合halcon应用——大华相机采集类
【无标题】