当前位置:网站首页>MySQL transaction isolation

MySQL transaction isolation

2022-06-23 23:08:00 Know the black and accept the white

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 .

原网站

版权声明
本文为[Know the black and accept the white]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231906098908.html