MySQL interview questions mvcc principle transaction isolation level

osc_ g91p39eg 2021-01-21 10:14:04
mysql interview questions mvcc principle


mysql The transaction isolation level can read the interview questions repeatedly

 Big bear

Today, my beautiful sister sent me a picture as follows !

 Insert picture description here
Premise :


> This is a InnoDB The default isolation level under the engine .
> We know InnoDB The default level is rr That is, can ` Repeated reading `

Before we look at this value, let's first understand the concept of what MVCC

MVCC Multi version concurrency control refers to “ Maintaining multiple versions of one data , So that there is no conflict between read and write operations ” .
And snapshot reading is MySQL For us to achieve MVCC One of the specific non blocking read functions of the ideal model . And relatively speaking , The current read is the specific function of pessimistic lock .


snapshot ” stay MVCC How does Li work :

At repeatable read isolation level , When the transaction starts “ Took a snapshot ”. This snapshot is based on the entire library . Of course, it doesn't copy all the data .

in fact InnoDB Each transaction has a unique transaction ID, called transaction id. It is at the beginning of the transaction to InnoDB The transaction system of the application is strictly increasing according to the order of application .

There are multiple versions of each row of data . Every time a transaction updates data , Will generate a new data version , And the transaction id The transaction assigned to this data version ID, Write it down as row trx_id. meanwhile , Keep the old data version , And in the new data version , You can get information directly .

InnoBB It's also a feature that uses multiple versions of each data ( Actually, I remember row_trx_id) The second level snapshot reading ability is realized .
At the transaction isolation level, we talked about the repeatable isolation level

I'm blind to everything else in my business. Give me a million dollars .
therefore , A transaction only needs to declare at startup that ,“ At the moment I start , If a data version is generated before I start , Just admit it ; If it was generated after I started , I don 't know , I have to find the last version of it ”.
Of course , If “ Last version ” Nor visible , Then we have to move on . Of course, if the transaction updates its own data , It still has to recognize itself .

meanwhile InnoDB An array is constructed for each transaction , Used to save the transaction startup moment , Right now “ active ” All the business of ID.“ active ” Refers to , Launched but not yet submitted

So in the figure above, there are three transactions that generate their transactions id The arrays are as follows :
 Business id Array

At this point, the transaction A In the transaction B、 Business C Before that means Relative to transactions A On business B、 Business C It's invisible, which is also consistent with the definition of repeatable reading, so Q2 The value is 18.

that Q1 Well ?

In the transaction B Then the transaction is executed C Look at the transaction C UPDATE statement executed . So you don't know if you have any impression of the second stage submission
The process of an update statement is the second stage submission. If you forget Please click this article to deepen your memory Two stage submission
The main meaning of this is update Statement will commit by default

update The statement actually requests lock ( Row lock )
What happens if you don't apply for a lock ?

> Suppose we have a record in our database. The initial value is 1, Update the same data in a concurrent environment 
> First addition 1, The second time also added 1.
> Then it is very likely that they will find the same value and then update it 
> It turns out that 2, Then we can't accept the result .

Of course, the line lock is added when necessary , But it's not about releasing them immediately when they don't need to , It's about waiting until the end of the transaction . This is the two-stage lock protocol .

And update data is read before write , And this read , Can only read the current value , be called “ The current reading ”(current read).

> So we're looking at the business C Yes update That is, the current reading So at this point age 
> It has been changed to 19 Of course, if we commit the transaction manually, if the transaction C Late submission 
> The transaction B Of course update Statement will wait until the transaction C Release and execute .

So let's go back to business B

> Business B In the first query age There is no doubt that 18 Of And then tell the new through the above update
> Statement is locked and is currently read age Turned into 20.
> In the execution of the transaction B When querying statements , Take a look at their own version number is 2, The version number of the latest data 
> It's also 2, It's your own update , You can use it directly , So the query results in age The value of is 20.
>
版权声明
本文为[osc_ g91p39eg]所创,转载请带上原文链接,感谢
https://javamana.com/2021/01/20210121100046590v.html

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云