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

边栏推荐
- Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
- tensorflow报错Could not load dynamic library ‘libcudnn.so.8
- MYSLQ十种锁,一篇文章带你全解析
- STC 32位8051单片机开发实例教程 二 I/O工作模式及其配置
- math_ Use differentiation to calculate approximate value
- 大厂做狼,小厂做狗?
- Cookie和Session的相关概念
- C#聯合halcon應用——大華相機采集類
- qobject_ Cast usage
- 走进如心小镇,数智化变革连接“未来社区”
猜你喜欢

基于图的 Affinity Propagation 聚类计算公式详解和代码示例

math_ Use differentiation to calculate approximate value

JS 之 常用内置类的使用

振弦采集模塊測量振弦傳感器的流程步驟

Interesting! Database is also serverless!
![[research materials] iResearch tide Watching: seven major trends in the clothing industry - Download attached](/img/c8/a205ddc2835c87efa38808cf31f59e.jpg)
[research materials] iResearch tide Watching: seven major trends in the clothing industry - Download attached
![[research data] observation on the differences of health preservation concepts among people in 2022 - Download attached](/img/50/926cc5bce83f8b195b3e2072b656bf.jpg)
[research data] observation on the differences of health preservation concepts among people in 2022 - Download attached

Image acquisition and playback of coaxpress high speed camera based on pxie interface

渗透工具-TrustedSec 公司的渗透测试框架 (PTF)

Win11暂停更新点不了怎么办?Win11暂停更新是灰色的如何解决?
随机推荐
Win11怎么关闭开机自启动软件
Object creation
switch 有四样写法你知道么
Win11快捷键切换输入法无反应怎么办?快捷键切换输入法没有反应
servlet知识点
ModSim基本使用(Modbus模拟器)
今日群里分享的面试题
[untitled]
使用Zadig从0到1搭建持续交付平台
How to use console Log print text?
Stack Overflow 2022 开发者调查:行业走向何方?
Mo Tianlun salon | Tsinghua qiaojialin: Apache iotdb, originated from Tsinghua, builds an open source ecological road
Detailed configuration of network security "Splunk" in national vocational college skills competition
Redis installation and startup in Windows environment (background startup)
cocoaPods 添加成功后,导入不了头文件或者not found file 报错
HLS4ML/vivado HLS 报错解决方案
uniapp使用腾讯地图选点 没有window监听回传用户的位置信息,怎么处理
#yyds干货盘点#SQL聚合查询方法总结
Wechat applet realizes keyword highlighting
全国职业院校技能大赛网络安全“splunk“详细配置