当前位置:网站首页>[mysql] what happens when things are executed concurrently?
[mysql] what happens when things are executed concurrently?
2022-07-27 01:39:00 【Caixinzhi】
List of articles
1. The goal is
The ultimate goal of this article is to be familiar with MySQL The four basic characteristics of things in ( especially Isolation, ), Then the isolation of things leads to concurrency , The most important things that appear in the implementation of things , The most common problem , At the same time, it also leads to MySQL And so on .
2. MySQL The four basic characteristics of things in
2.1 Atomicity
Atomicity is actually the core characteristic of things . Before things exist , As far as the bank's transfer system is concerned , A and B In my account 20, When A towards B Transfer accounts 10, At this time, in the database , A Your account balance should be reduced first 10, then B Add 10. Normally , There is nothing wrong with this situation , But such a situation may happen : When A reduce 10 And B Add 10 In this short period of time , The machine suddenly goes down , Then when the machine is restored, you will find — A It's less for no reason 10, and B But there is no increase in , In this case , The concept of things is introduced into the database .
Things will regard the above two steps as a whole , When one of them is abnormal , will Roll back (rollback) To perform the overall operation . Like this, some operations are regarded as an inseparable whole , We call it the " Atomicity "( Because the atom was the smallest unit before , Symbolizing the inseparable meaning ), This is also the core characteristic of things .
2.2 Uniformity
For example, when we submit the database , Things are often used , This ensures that the data is consistent , There will be no mistakes .
2.3 persistence
The operation of things is to write ( Store in ) On disk , Once things are submitted successfully , Resulting modifications , That's persistent , Even if the host is restarted , There is also , So the changes to the database are permanent , It well reflects persistence .
2.4 Isolation,
In the database , When multiple things are executed concurrently , This isolation will occur . So called concurrency , When multiple things are executed at the same time , They will affect each other , It may lead to inaccurate execution results , Even collapse … This is the time , The isolation of things will be reflected , Its goal is to prevent multiple things from executing concurrently , Reduce the influence between multiple things .
3. Problems and solutions of concurrent execution
3.1 What is concurrent behavior
The above is a brief introduction , Concurrency is when multiple things are executed at the same time , The process of influencing each other . Although it says , Concurrent execution may lead to inaccurate data , But the efficiency of concurrency is very high , In development , Concurrency is often used . in general , This behavior of concurrency is to improve the efficiency of execution , The act of reducing the accuracy of data .
among , Multithreaded execution is a concurrent behavior , Executing things with multiple clients is also a kind of concurrent behavior . In this article, we discuss the second case .
3.2 Problems with concurrent execution
3.2.1 Question 1 : Dirty reading problem
One thing A In the process of execution , Modify a series of data , And finishing this thing A( Or submit ) Before , If there is something else B Just reading these unmodified data , Because of these things B The data read are all temporary data , And the final thing A complete ( Or submit ) The subsequent data may not match ( It is possible in these things B After the read is complete , thing A Maybe the previous content has been modified ). In this case , These things B The data read is dirty data , Such behavior is called " Dirty reading ".
Problem one, the solution :
We can stipulate that , In things A When modifying data , Other things B You can't try to read the data , Must wait for things A complete ( Or submit ) after , To have access .
Operations like this , We call it " Lock the read operation " operation . Control it only in things A Read the data after completion . Reduce the degree of concurrency , Low efficiency , But the isolation is improved .
3.2.2 Question two : Repeat the question
After solving the dirty reading problem in problem 1 , Although it is stipulated that other things cannot be read in the process of data modification , But if there is a situation : When things A complete ( Or submit ) After that , Other things B In the process of execution , thing A Modify its data again , At this time, if these things B If it is executed again , The data read before and after will be different , This leads to inaccurate data . Such behavior is called " Repeated reading ".
Problem two, the solution :
Because in other things B When it comes to execution , thing A Modify , Cause other things B The data read twice is not equal , At this time, we can consider limiting things A In other things B During execution , These data cannot be modified , It further ensures that there will be no repetition .
Operations like this , We call it " Non repeatable read operation " operation . Control things A In other things B During execution , You cannot modify the data . Once again, the degree of concurrency is reduced , Efficiency decreases again , But the isolation has been further improved .
contrast " Dirty reading problem " and " Repeat the question ": " Dirty reading problem " It's something A When modifying data , Other things B The process of implementing the resulting problems . and " Repeat the question " It's something else B In the process of execution , thing A The process of modifying data to cause problems .
3.2.3 Question 3 : The problem of unreal reading
After solving the problems caused by problem one and problem two , We guarantee that in things A When modifying data , Other things B Cannot perform , At the same time, it also ensures that in these things B During execution , thing A The corresponding data cannot be modified . But if there is a situation : In these things B During execution , thing A Modify the data , But there is no direct modification of these things B Of read data , Although these things B The final result of reading may not have an impact , But it will still affect these things B Read result set , Make these things B The result set read twice is different , This leads to inaccurate data .
Problem three, the solution :
In fact, in essence , " The problem of unreal reading " yes " Repeat the question " A special case of , Our solution is still the same as " Repeat the question " The solution is similar , That is to limit things A In other things B During execution , Do not make any modification , This can perfectly ensure that there will be no repetition .
Operations like this , We call it " Unreal reading " operation . Control things A In other things B During execution , The data in it cannot be whatever Modify the operating . Achieve the highest isolation , Data accuracy is the best effect , But correspondingly , Concurrency will also become the lowest , Execution efficiency becomes the lowest .
3.3 summary
in general , Concurrency and isolation are rivals , They are often contradictory .
The problems of the above three concurrent execution things are gradually progressive , It can be divided into four " gear ", Namely : (1) Nothing is restricted — This situation has the highest degree of concurrency , Minimum isolation ; (2) Lock the read operation — In this case, the concurrency ability decreases , Isolation is further increased ; (3) Do not repeat the read operation — In this case, the concurrency ability decreases further , Isolation is further increased ; (4) Unreal read operation — This situation has the lowest degree of concurrency , Highest isolation .
In the process of actual development , You need to choose the above gear reasonably according to the actual scene , Such as , When it comes to transfers ( Money related ) The operation of , You must use the most isolated operation , Ensure the most accurate data ; When some just count the number of viewers ( Or playback volume ) The operation of , You can use the most concurrent operations , Improve execution efficiency , Greatly reduce the implementation cost .
4. MySQL The solution corresponding to the database
We know the problems of parallel execution and the corresponding solutions , So in MySQL How to realize these solutions ?
alike , MySQL There are four " gear " To correspond to the four mentioned above " gear ":
(1) read uncommitted — Concurrency is the strongest , Isolation is the weakest .
(2) read committed — It's solved " Dirty reading problem ", Concurrency decreases , Isolation is further increased .
(3) repeatable read — It's solved " Repeat the question ", Concurrency is further reduced , Isolation is further increased .
(4) serializable — It's solved " The problem of unreal reading ", Concurrency is the weakest , The strongest isolation .
For different scenarios , We are right. MySQL The four provided " gear " Make a selection , And in the configuration file my.ini Modify the isolation level of the database . my.ini This file is the file we used to modify the default code , If you don't know how to find this file, you can take a look at this article : For beginners MySQL Little partner's advice ( Strongly recommend ~ Don't look at regret ~~).
stay my.ini Add the following command to the file to specify the isolation level :
# The optional parameters are :read-uncommitted, read-committed, repeatable-read, serializable.
[mysqld]
transaction-isolation = repeatable-read
stay MySQL in , The default isolation level is repeatable-read, This is also the global default .
边栏推荐
猜你喜欢

ESP8266 AP_TCP_Server

十六、awk
Problem feedback: the synchronization of mobile app failed: the external changes of the data warehouse were damaged. The iPad app also downloaded the warehouse as soon as it was opened, and then flash

Unity twitter login access

FTP服务

4.1 It is super simple to install QT without using Sogou Chinese input method

Plantcv Chinese document

Excel changes the format of scientific counting method into text

EXPECT免交互

Esp8266 access to cloud platform ----- DNS domain name connection server
随机推荐
shell循环语句
十六、awk
Introduction to Internet of things platform
33三剑客awk
六、if语句
Linked list general OJ
引导过程与服务控制
5、 Conditional statement of shell
Understanding and learning of internal classes
13、 Command gadget
Unity twitter login access
【Oracle】获取最近工作日及前N个工作日
Some simple extension methods commonly used by unity
ESP8266 AP_UDP_Client
Esp8266 access to cloud platform ----- DNS domain name connection server
进程与计划任务管理
[SQL injection] joint query
五、Shell之条件语句
EXPECT免交互
16、 Awk