当前位置:网站首页>MySQL transaction isolation
MySQL transaction isolation
2022-06-23 23:08:00 【Know the black and accept the white】
Read the directory
An introduction to the business
A transaction is a set of atomic sql Inquire about , Or a separate unit of work .
In short , All statements in the transaction are executed successfully , Or all failed .
stay MySQL in , Transaction support is implemented at the engine level , But not all of them MySQL All engines support transactions , such as MyISAM The engine doesn't support transactions , This is also MyISAM By InnoDB One of the important reasons for replacement .
When it comes to business , We'll think about it ACID:
Atomicity (Atomicity)
Uniformity (Consistency)
Isolation, (Isolation)
persistence (Durability)
Isolation level
When multiple transactions are executed simultaneously in the database , Dirty reads may occur 、 It can't be read repeatedly 、 Illusory reading and so on , Because there is the concept of transaction isolation level .
SQL The standard is defining four levels of isolation :
1 READ UNCOMMITTED ( Uncommitted read )
Changes in transactions , Even if you haven't submitted , Visible to all other transactions . Transactions can read uncommitted data , Also known as dirty reading (Dirty Read).
2 READ COMMITTED( Submit to read )
After a transaction is committed , The changes made can only be seen by other transactions . This level is also called non repeatable , Because in a transaction 2 Same query times , The possible results are different .
3 REPEATABLE READ( Repeatable )
During the execution of a transaction , It is always consistent with the data seen when the transaction is started . Of course, at this level , Uncommitted data changes are also invisible to other transactions .
SERIALIZABLE( Serializable )
Record... On the same line , Both writing and reading are locked , When a read-write lock conflict occurs , A post access transaction must wait for the execution of the previous transaction to complete before it can continue , This will lead to a large number of timeout and lock contention problems .
On the implementation , A view will be created in the database , The logic of the view shall prevail when accessing .
At the repeatable read isolation level ,
This view is created when the transaction is started , Use this view throughout the transaction .
Under isolation level of read commit , This view is in sql Created when the statement begins execution .
Under read uncommitted isolation level , Directly return the latest value on the record , There is no view concept .
At the isolation level of serialization , Avoid parallel access by locking directly .
The configuration method is to start the parameter transaction-isolation Set to the desired isolation level .
View current settings :
mysql> show variables like 'transaction_isolation';
+-----------------------+-----------------+
| Variable_name | Value |
+-----------------------+-----------------+
| transaction_isolation | REPEATABLE-READ |
+-----------------------+-----------------+
1 row in set (0.00 sec)
All in all , Existence is reason , Different isolation levels apply to different scenarios , The details should be determined according to the business scenarios .
Implementation of transaction isolation
stay Mysql in , In fact, each record update will also record a rollback operation , The latest value on the record is rolled back , Can get the value of the previous state .
The system will judge automatically , When no transaction needs to roll back the log , The rollback log is deleted .
Why long transactions are not recommended :
Long transactions mean that there will be a very old transaction view in the system , Because these transactions can access any data in the database at any time , So before this transaction is committed , Rollback records that may be used in the database must be preserved , This will take up a lot of storage space . At the same time, long transactions also occupy lock resources , It could also drag down the entire warehouse .
How the transaction is started
1 Start the transaction statement explicitly ,begin perhaps start transaction , Submission is commit , For rollback rollback.
2 set autocommit = 0, This command will turn off the automatic submission of threads , It means that if a select sentence , This transaction starts , And will not automatically submit , Until you take the initiative to commit perhaps rollback, Or disconnect .
My personal suggestion is to start the transaction explicitly in the first way , Avoid long transactions .
stay set autocommit = 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 .
Query long transaction :
The following statement shows that the query duration exceeds 60s The business of :
mysql> select * from information_schema.innodb_trx where TIME_TO_SEC(timediff(now(),trx_started))>60;
Empty set (0.00 sec)
Sum up , We are in the process of development , Try not to use long transactions , If it can't be avoided , Ensure that the logical log space is large enough , And support dynamic log space growth . monitor Innodb_trx surface , Long transaction alarm found .
边栏推荐
- What should I do if the RDP fortress server connection times out? Why should enterprises use fortress machines?
- Notes to nodejs (III)
- AAAI 2022 | Tencent Youtu 14 papers were selected, including image coloring, face security, scene text recognition and other frontier fields
- Section 29 basic configuration case of Tianrongxin topgate firewall
- 混沌工程,了解一下
- The fortress computer is connected to the server normally, but what's wrong with the black screen? What should I do?
- What is the development prospect of face recognition technology?
- Recommended | January activity 2-core 4G lightweight application server, enterprise nationwide purchase 6.7 yuan / month!!!
- API gateway monitoring function the importance of API gateway
- How to handle the IP inconsistency in the contact when easygbs is cascaded with the upper level
猜你喜欢

Section 29 basic configuration case of Tianrongxin topgate firewall

SAVE: 软件分析验证和测试平台

Save: software analysis, verification and test platform

ASM文件系统 数据如何写和读数据

百万消息量IM系统技术要点分享

Slsa: accelerator for successful SBOM

【技术干货】蚂蚁办公零信任的技术建设路线与特点

专业“搬砖”老司机总结的 12 条 SQL 优化方案,非常实用!

Opengauss Developer Day 2022 was officially launched to build an open source database root community with developers

Section 30 high availability (HA) configuration case of Tianrongxin topgate firewall
随机推荐
Apache log4j 2 reported high-risk vulnerability, coding teamed up with Tencent to protect software security
Problem solving: inittramfs unpacking failed:decoding failed
What are the application flow restrictions of API gateway framework?
CS1.6 service startup tutorial
How to batch generate flattermark barcode
The Sandbox 归属周来啦!
How to use data warehouse to create time series
[tcapulusdb knowledge base] [generic table] read data interface description
SQL Server Common SQL
Performance test - LoadRunner obtains the return value and user-defined parameters (parameter operation)
混沌工程,了解一下
[JS reverse hundred examples] the first question of the anti crawling practice platform for netizens: JS confusion encryption and anti hook operation
Phpmailer sends mail PHP
Recommended | January activity 2-core 4G lightweight application server, enterprise nationwide purchase 6.7 yuan / month!!!
云原生流水线工具汇总
How to solve the problem that the GPU VNC has two mice with large deviation
Get and post are nothing more than TCP links in nature?
Notes to nodejs (III)
Section 30 high availability (HA) configuration case of Tianrongxin topgate firewall
Docker中部署Redis集群与部署微服务项目的详细过程