当前位置:网站首页>Ten MySQL locks, one article will give you full analysis
Ten MySQL locks, one article will give you full analysis
2022-06-28 17:49:00 【pythonxxoo】
High quality resource sharing
| Learning route guidance ( Click unlock ) | Knowledge orientation | Crowd positioning |
|---|---|---|
| 🧡 Python Actual wechat ordering applet 🧡 | Progressive class | This course is python flask+ Perfect combination of wechat applet , From the deployment of Tencent to the launch of the project , Create a full stack ordering system . |
| Python Quantitative trading practice | beginner | Take you hand in hand to create an easy to expand 、 More secure 、 More efficient quantitative trading system |
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 :

Articles are constantly updated , You can search through wechat 「 One light architecture 」 Read more technical dry goods for the first time .
边栏推荐
- How to put your WordPress website in maintenance mode
- [tcapulusdb knowledge base] how webclient users read and modify data
- The MySQL installed in Alibaba cloud server is version 8. Is it because the MySQL driver version of dataworks does not support it? Now mention
- 传统运维不得不会的zabbix监控(你早起,我早起,我们迟早在一起)
- 2022起重机械指挥考试题库模拟考试平台操作
- jq插件分析啊啊
- Google推出Advanced API Security 保护API免受安全威胁
- 58 Technology Salon issue 31 flutter dynamic special Salon
- Visubit "ai+3d vision" product series | loading assembly workstation
- Talk about my understanding of image tearing, vertical synchronization, freesync and g-sync
猜你喜欢

'summary of common network protocols' summarized by Tsinghua leaders in 3 days

Redis persistence (young people always set sail with a fast horse, with obstacles and long turns)

2022 operation of simulated examination platform of hoisting machinery command examination question bank

2022危险化学品生产单位安全生产管理人员复习题及答案
![[dark horse morning post] Tencent responded that a large number of users' QQ numbers were stolen; Weiya's husband company was fined 190000 yuan; China Evergrande is applied for liquidation; Guanxiaoto](/img/d7/4671b5a74317a8f87ffd36be2b34e1.jpg)
[dark horse morning post] Tencent responded that a large number of users' QQ numbers were stolen; Weiya's husband company was fined 190000 yuan; China Evergrande is applied for liquidation; Guanxiaoto

使用Pega进行一个简单的RPA程序开发

Rsync remote synchronization (sunset is especially gentle, and the world is romantic)

Squid代理服务器应用(我从远方赶来,赴你一面之约)

Google推出Advanced API Security 保护API免受安全威胁
![[World Ocean Day] tcapulusdb calls on you to protect marine biodiversity together](/img/82/2357f9d195cfbb38c4052790804a6f.png)
[World Ocean Day] tcapulusdb calls on you to protect marine biodiversity together
随机推荐
WPF video hard decoding, rendering and playing (no airspace) (support 4K, 8K and high frame rate video)
Dpdk 20.11 compiling, installing and running program
RHEL6.4中使用Cacti+Spine监控主机实现发送邮件报警
Xiaoxin black apple sound card ID injection
2022危险化学品经营单位主要负责人复训题库及在线模拟考试
Redis master-slave replication, sentinel, cluster cluster principle + experiment (wait, it will be later, but it will be better)
9个优秀的占位图服务
IDC: Alibaba cloud ranks first in the market share of China's data governance platform in 2021
How to open a futures account? Where is it safer to open a futures account?
Can data sources only be connected to Alibaba cloud cloud databases? Can't you connect the databases installed in Alibaba cloud servers?
Ding! Techo day Tencent technology open day arrived as scheduled!
Metaq installation deployment document
VirtualBox中克隆了一个虚拟系统出现IP问题
Google launches advanced API security to protect APIs from security threats
2022年化工自动化控制仪表考试模拟100题模拟考试平台操作
How to solve the problem of Caton screen when easycvr plays video?
IDC:阿里云获2021中国数据治理平台市场份额第一
2022年7月计划(全力unreal)
[question skimming diary] and a subarray of K
"Jay bear" plummeted by 96.6%. Why is NFT with star goods cold?