当前位置:网站首页>Mysql45 talks about transaction isolation: why can't I see it after you change it?
Mysql45 talks about transaction isolation: why can't I see it after you change it?
2022-07-26 05:53:00 【lingengy】
Some basic concepts of transaction can be seen from this article I wrote before :spring Business management
One 、 Implementation of transaction isolation
stay MySQL in , In fact, each record will record a rollback operation at the same time when it is updated . The latest value on the record , By rolling back operations , Can get the value of the previous state .
Suppose a value from 1 Has been changed in order to 2、3、4, In the rollback log, there will be records like the following .![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-HHkRMljW-1657094914100)(C3F362CD7B4E473483C046C8493CE31C)]](/img/92/9129800d2473945c30b5860376dfdf.png)
There will be different transactions started at different times read-view, There can be multiple versions of the same record in the system , It's multi version concurrency control of database (MVCC). When a transaction wants to see where it is read-view The value of , To rollback from the current value to its read-view In order to get the value of its transaction in the version .
Through this mechanism , Just record read-view The position of , It has nothing to do with how many times this value has been modified .
The system will judge that there is no earlier rollback log in the system read-view When , No transactions need these rollback logs anymore , The rollback log will be deleted .
About why try not to use long transactions
Long transactions mean that there will be a very old transaction view in the system . Since these transactions may access any data in the database at any time , So before this transaction is committed , All possible rollback records in the database must be kept , This can lead to a lot of storage space .
In addition to the impact on rollback segments , Long transactions also take up lock resources , It could also drag down the entire warehouse , We will expand the lock later .
Two 、 How to start a transaction
MySQL There are several ways to start a transaction :
- Start the transaction statement explicitly , begin or start transaction. The accompanying commit statement is commit, The rollback statement is rollback.
- set autocommit=0, This command will turn off the automatic submission of this thread . It means if you only execute one select sentence , This transaction starts , And it doesn't automatically commit . This business continues until you take the initiative to commit or rollback sentence , Or disconnect .
Some client connection frameworks will default to one after successful connection set autocommit=0 The order of . This results in the following queries being in the transaction , If it's a long connection , It leads to an unexpected long affair .
therefore , I would suggest that you always use set autocommit=1, Start the transaction through an explicit statement .
But some development students will struggle “ One more interaction ” The problem of . For a business that requires frequent use of transactions , The second way is that each transaction does not need to be actively executed once at the beginning “begin”, Reduce the number of statement interactions . If you have that concern , I suggest you use commit work and chain grammar .
stay autocommit by 1 Under the circumstances , use begin Explicitly started transactions , If you execute commit Then commit the transaction . If you execute commit work and chain, Commit the transaction and start the next transaction automatically , This also saves the need to execute again begin Statement overhead . At the same time, the benefit is to know clearly whether each statement is in a transaction from the perspective of program development .
You can information_schema Library innodb_trx Query long transactions in this table , Take the following statement , Used to find a duration greater than 60s The business of .
select * from information_schema.innodb_trx where TIME_TO_SEC(timediff(now(),trx_started))>60
边栏推荐
- JDBC streaming query and cursor query
- Unity2d animator cannot create transition
- MBA-day28 数的概念-练习题
- 知识沉淀一:架构师是做什么?解决了什么问题
- 调试利器!一款轻量级日志库 log.c
- Modifiers should be declared in the correct order 修饰符应按正确的顺序声明
- Rocbossphp free open source light community system
- ERROR: Could not open requirements file: [Errno 2] No such file or directory: ‘requirments.txt’
- How can programmers improve mental internal friction?
- Talking about the practice of software defect management
猜你喜欢

5-year-old Test Engineer - how to choose the next step?

Rocbossphp free open source light community system

Redis持久化-AOF

The idea YML file code does not prompt the solution

Using easyexcel to import tables to realize batch insertion of xlsx files ----- MySQL of Linux

leetcode-Array

Solve vagrant's error b:48:in `join ': incompatible character encodings: GBK and UTF-8 (encoding:: Compatib

The refurbishment and counterfeiting of chips have made people feel numb

Full binary tree / true binary tree / complete binary tree~

Benji Bananas 开启第二季边玩边赚奖励活动,支持使用多张 Benji 通行证!
随机推荐
How can red star Macalline design cloud upgrade the traditional home furnishing industry in ten minutes to produce film and television level interior design effects
Detailed explanation of the whole process of coding's pressure measurement operation
Who is responsible for the problems of virtual idol endorsement products? And listen to the lawyer's analysis
Full binary tree / true binary tree / complete binary tree~
A trick to teach you to easily understand Potter's map
Ros2 preliminary: basic communication with topic
MBA-day28 数的概念-练习题
又一开源神器,值得收藏学习!
Jupiter notebook shortcut key
三本毕业,三年嵌入式软件的心路历程
顺序查找,折半查找,分块查找 ~
高效,可靠,安全的串口通讯开源方案
Lemon class automatic learning after all
Processing method of CDC in SDC
Qu Weihai, chairman and CEO of Xinyi interactive, adheres to mutual benefit and win-win results, and Qu Weihai promotes enterprise development
Day110. Shangyitong: gateway integration, hospital scheduling management: Department list, statistics based on date, scheduling details
[MySQL must know and know] time function number function string function condition judgment
Establishment of log collection and analysis platform-1-environment preparation
Modifiers should be declared in the correct order 修饰符应按正确的顺序声明
MBA-day29 算术-绝对值初步认识