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

边栏推荐
- 2022/6/8-2022/6/12
- Solve the problem of slow or failed vscode download
- 渗透工具-TrustedSec 公司的渗透测试框架 (PTF)
- Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
- EasyCVR集群视频广场页面切换时,请求流未能终止的问题优化
- Review the collection container again
- Arduino stepper library drive 28byj-48 stepper motor test program
- Detailed configuration of network security "Splunk" in national vocational college skills competition
- Oracle 死锁测试
- Iframe 父子页面通信
猜你喜欢

开发那些事儿:EasyCVR平台添加播放地址鉴权功能

Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?

STC 32位8051单片机开发实例教程 二 I/O工作模式及其配置

牛客编程题--必刷101之字符串(高效刷题,举一反三)

Interview question 1

开发那些事儿:EasyCVR集群设备管理页面功能展示优化

JS的Proxy

数据分析师听起来很高大上?了解这几点你再决定是否转型

SQL getting started plan-1-select

强大、好用、适合程序员/软件开发者的专业编辑器/笔记软件综合评测和全面推荐
随机推荐
Flask 常用组件
毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?
Using win7 vulnerability to crack the system login password
How to turn off the boot auto start software in win11
Install redis under Linux and configure the environment
Powerful, easy-to-use, professional editor / notebook software suitable for programmers / software developers, comprehensive evaluation and comprehensive recommendation
GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
STC 32-bit 8051 single chip microcomputer development example tutorial II i/o working mode and its configuration
New window open page -window open
优质笔记软件综合评测和详细盘点(一) Notion、Obsidian、RemNote、FlowUs
Anaconda安装虚拟环境到指定路径
qobject_ Cast usage
JS ternary expression complex condition judgment
DS Transunet:用于医学图像分割的双Swin-Transformer U-Net
Problems encountered in installing MySQL in docker Ubuntu container
Unreal Engine packaging project
HLS4ML报错The board_part definition was not found for tul.com.tw:pynq-z2:part0:1.0.
Mo Tianlun salon | Tsinghua qiaojialin: Apache iotdb, originated from Tsinghua, builds an open source ecological road
[untitled]
switch 有四样写法你知道么