当前位置:网站首页>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 .
边栏推荐
- Time effective simulation platform based on dataworks | acquisition technology
- 第四个专栏,Kubernetes云原生实战,它来了~
- Improving observability - business indicator monitoring practice
- [tcapulusdb] I wish you all a healthy Dragon Boat Festival!
- 6 - Dictionary
- 9个优秀的占位图服务
- Q: how bad can a programmer be?
- 2022 chemical automation control instrument test simulation 100 questions simulation test platform operation
- How to do a good job of gateway high availability protection in the big promotion scenario
- win10用cmake3.22与vs2019编译curl库源码并调用
猜你喜欢

MySQL中的日志管理 日志備份與恢複

Large scale sports events and crime risks

Redis persistence (les adolescents naviguent toujours rapidement, obstruent et ne tournent jamais)

"Jay bear" plummeted by 96.6%. Why is NFT with star goods cold?
Tidb v6.0.0 (DMR): initial test of cache table - tidb Book rush

Visio use

2022 review questions and answers for safety production management personnel of hazardous chemical production units

TDengine&nbsp;×英特尔 边缘洞见软件包 加速传统行业的数字化转型

传统运维不得不会的zabbix监控(你早起,我早起,我们迟早在一起)

MySQL高可用之MHA(一不小心和我的青春擦肩而过)
随机推荐
2022 review questions and answers for safety production management personnel of hazardous chemical production units
IDC:阿里云获2021中国数据治理平台市场份额第一
dataworks SQL脚本支持语句块的if else 判断吗
单片机修改网络硬件驱动(phy芯片更换)
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
CSDN Blogger
Talk about my understanding of image tearing, vertical synchronization, freesync and g-sync
2022 operation of simulated examination platform of hoisting machinery command examination question bank
How much is the data delay when you collect Oracle data? I can't keep it down for 3 seconds. Is there an industry reference
传统运维不得不会的zabbix监控(你早起,我早起,我们迟早在一起)
Squid代理服务器应用(我从远方赶来,赴你一面之约)
Redis主从复制、哨兵、cluster集群原理+实验(好好等,会晚些,但会更好)
Apache setting timeout parameter
Google推出Advanced API Security 保护API免受安全威胁
Summary and Thinking on interface test automation
Google launches advanced API security to protect APIs from security threats
NP tips: random create random matrix sample = np random. random([19, 64 , 64, 3])
Redis persistence (young people always set sail with a fast horse, with obstacles and long turns)
区间乘积的因子数之和
Write your own WordPress template