当前位置:网站首页>MySQL 45 lecture learning notes (VII) line lock
MySQL 45 lecture learning notes (VII) line lock
2022-07-04 06:38:00 【Tom Kong】
One . What is a row lock
- MySQL The row lock is implemented by each engine in the engine layer . But not all engines support row locks . such as MyISAM The engine doesn't support row locks . Not supporting row locks means that concurrency control can only use table locks .
- For tables with this engine . Only one update can be executed at any time on the same table , This will affect the business concurrency
- InnoDB It supports row locking . This is also MyISAM By InnoDB One of the important reasons for substitution .
Improve business concurrency by reducing lock conflicts
Row locks are locks for row records in the data table .
Two . Two stage lock
For example, affairs. A Updated a line , It's time for business B Also update the same line , Then we have to wait for business A You can't update until the operation of .

- Business B Of update The statement will be blocked , Until transaction A perform commit after , Business B In order to proceed .
- stay InnoDB Transaction , Row lock will be added when necessary , however Will not be released immediately , It is Wait for the transaction to end before releasing , This is it. Two stage lock protocol .
If you need to lock more than one hang in your affairs , The most likely cause of lock conflict , The locks that are most likely to affect concurrent reads should be put back as far as possible .
3、 ... and . Use scenarios
example :
Suppose you implement a movie ticket online trading business customer A In the cinema B Buying movie tickets requires the following operations :
- From customers A The movie ticket price is deducted from the account balance
- To the cinema B Your account balance increases the ticket price of this movie
- Keep a transaction log
analysis
- To close the deal , We need to update Two records , also insert A record .
- Of course, in order to ensure the atomicity of the transaction , We need to put these three operations in one transaction , How can we arrange the order of these three statements in a transaction ?
solve
- According to the two-stage lock protocol , No matter how you arrange the sentence order , All row locks required for operations are released only when the transaction is committed .
- therefore , If you put the sentence 2 Arrange until the end , If according to 3,1,2 The order of , Then the lock time of the cinema account balance line is the least , This greatly reduces the lock waiting between transactions , Increased concurrency .
Four . problem 2
If this theater is active , You can pre sell all movie tickets for a year at a low price , And it's only for one day . So when the activity time starts , Yours Mysql I hung up. , You log on to the server and have a look ,CPU Consumption is close to 100
But the whole database It cannot be executed every second 100 One transaction , What's the reason for this ?
Deadlock detection is costly CPU resources
5、 ... and . Deadlock and deadlock detection
When circular resources appear in different threads in the concurrent system , The threads involved are waiting for other threads to release resources , It will cause these threads to enter the state of infinite waiting , be called Deadlock .

- Business A Waiting for business B Release id=2 The row lock , And the business B Waiting for business A Release id=1 The row lock . Business A And transaction B Waiting for each other's resources to be released , Is to enter the deadlock state , When there's a deadlock , There are two strategies ;
- Another strategy is , Initiate deadlock detection , After deadlock is found , Actively roll back a transaction in the deadlock chain , Allow other business to continue . The parameter innodb_deadlock_detect Set to on, Indicates that the logic is turned on .
6、 ... and . Disadvantages of timeout release strategy
- stay InnoDB in ,innoDB_lock_wait_timeout The default value of is 50s, That means if you use the first strategy , When there's a deadlock , The first one is to lock the thread 50s Will time out
- Then it's possible for other threads to continue , For online services , This waiting time is often unacceptable ,
- But we can't set it to a small value , Such as 1s. When a deadlock occurs , It can really be solved soon , But if it's not deadlock , But simply lock and wait , If the timeout setting is too short , There will be many accidental injuries .
7、 ... and . Active deadlock detection
- and innoDB_deadlock_detect The default value of is on. Active deadlock detection when deadlock occurs , It can be found and processed quickly , But he has an extra burden .
- Whenever a transaction is locked , It depends on whether the thread he depends on is locked by others , So circular , Finally, judge whether there is a cycle waiting , That's deadlock .
Scene question :
All transactions need to update the scenario of the same row
- Every new blocked thread , We should judge whether it will lead to deadlock due to our participation , This is a time complexity of O(n) The operation of
- Suppose there is 1000 All concurrent threads update the same row at the same time , So the deadlock detection operation is 100w, Although the final test result is no deadlock , But it will consume a lot during this period CPU resources . So you'll see CPU High utilization ,
- But not a few transactions can be executed every second
Deadlock detection is costly CPU resources
Solution :
Temporarily turn off deadlock detection
- This operation itself carries certain risks , Therefore, deadlock is generally not regarded as a serious error in business design , After all, there is a deadlock , Just roll back , This is business intact
- Turning off deadlock detection means that there may be a lot of timeouts , It's business that's damaging .
The idea is to control concurrent reading
- For example, there is only one line at a time 10 Threads updating , So deadlock The cost of testing is very low , There will be no such problem. A direct idea is , Do concurrency control on the client side
边栏推荐
- selenium IDE插件下载安装使用教程
- Arcpy uses the updatelayer function to change the symbol system of the layer
- [backpack DP] backpack problem
- MySQL learning notes 3 - JDBC
- Tar source code analysis 4
- C # symmetric encryption (AES encryption) ciphertext results generated each time, different ideas, code sharing
- 8. Factory method
- Native Cloud - SSH articles must be read on Cloud (used for Remote Login to Cloud Server)
- Tar source code analysis Part 2
- 1、 Relevant theories and tools of network security penetration testing
猜你喜欢

C实现贪吃蛇小游戏

Google Chrome Portable Google Chrome browser portable version official website download method
![[MySQL] introduction, function, creation, view, deletion and modification of database view (with exercises)](/img/03/2b37e63d0d482d5020b7421ac974cb.jpg)
[MySQL] introduction, function, creation, view, deletion and modification of database view (with exercises)

Uniapp custom environment variables

Can the out of sequence message complete TCP three handshakes

ORICO ORICO outdoor power experience, lightweight and portable, the most convenient office charging station

How to realize multi account login of video platform members

【问题记录】03 连接MySQL数据库提示:1040 Too many connections

Which water in the environment needs water quality monitoring

Mysql 45讲学习笔记(七)行锁
随机推荐
树形dp
tars源码分析之2
11. Dimitt's law
2022, peut - être la meilleure année économique de la prochaine décennie, avez - vous obtenu votre diplôme en 2022? Comment est - ce prévu après la remise des diplômes?
tars源码分析之8
What is Gibson's law?
分布式CAP理论
【问题记录】03 连接MySQL数据库提示:1040 Too many connections
Mysql 45讲学习笔记(十二)MySQL会“抖”一下
selenium IDE插件下载安装使用教程
内卷怎么破?
Wechat applet scroll view component scrollable view area
ORICO ORICO outdoor power experience, lightweight and portable, the most convenient office charging station
2022 is probably the best year for the economy in the next 10 years. Did you graduate in 2022? What is the plan after graduation?
Tree DP
Option (024) - do all objects have prototypes?
Is the insurance annuity product worth buying? Is there a hole?
ABAP:OOALV实现增删改查功能
After the festival, a large number of people change careers. Is it still time to be 30? Listen to the experience of the past people
[March 3, 2019] MAC starts redis