当前位置:网站首页>MySQL - transaction details
MySQL - transaction details
2022-07-06 21:26:00 【Shuangchen legend Technology (Newton it College)】
One 、 Business (Transaction) Definition
Business : A smallest indivisible unit of work ; Usually a transaction corresponds to a complete business .
● A complete business needs batch DML(insert、update、delete) The statements are jointly completed .
● stay MySQL Only use Innodb The database or table of the database engine supports transactions .
● Transactions can be used to maintain database integrity , Make sure that there are lots of SQL Statement or all execution , Or none at all .
● Business is only with DML The sentence is about , Or say DML Statements have transactions . This has to do with business logic , Business logic is different ,DML The number of sentences is different
●MySQL Transactions are mainly used to handle large amount of operations , Data with high complexity . for instance , In the people management system , You delete a person , You need to delete the basic information of the personnel , Also delete information related to the person , Such as mailbox , Articles, etc , such , These database operation statements constitute a transaction !
Two 、 The four characteristics of affairs (ACID)
Generally speaking , Transactions must be satisfied 4 Conditions (ACID):Atomicity( Atomicity or indivisibility )、Consistency( Uniformity )、Isolation( Isolation or independence )、Durability( persistence )
● Atomicity (Atomicity): A set of transactions , Or success ; Or withdraw , That is, if an error occurs during the execution of a transaction, it will be rolled back to the state before the start of the transaction .
● Uniformity (Consistency) : A transaction, whether before or after the beginning , The integrity of the database has not been destroyed . Therefore, the written data must fully comply with all preset rules ( Data accuracy 、 Serialization and subsequent databases can spontaneously complete the scheduled work ).
● Isolation, (Isolation): The database allows multiple transactions to read, write and modify its data simultaneously , Isolation can prevent data inconsistency caused by cross execution when multiple transactions are executed concurrently . Transaction isolation can be divided into :Read uncommitted( Read uncommitted )、Read committed( Read the submission )、Repeatable read( Repeatable )、Serializable( Serialization ).
● persistence (Durability): The changes made to the data after the transaction is completed are permanent , Cannot lose
2.1 Isolation, (Isolation) The level of
Business A And transaction B There is isolation between
● Isolation has the following 4 Isolation levels :
1、 Read uncommitted :read uncommitted
● Business A And transaction B
● At this isolation level , All things can see the execution results of other uncommitted transactions
● This isolation level is rarely used in practical applications
● Because its performance is not much better than other levels , Read uncommitted data , It's also called dirty reading (Dirty Read)
2、 Read submitted :read committed
● The default isolation level for most database systems ( But it's not MySQL default )
● Satisfies the simple definition of isolation : One thing is invisible to others before it is submitted , This isolation level also supports so-called non repeatable reads (Nonerepeatable Read), Because other instances of the same transaction may have new commit, So the same select May return different results .
3、 Repeatable :repeatable read
●MySQL Default isolation level
● Ensure that multiple instances of the same transaction read data concurrently , You will see the same data lines
● But this will lead to another thorny problem , Fantasy reading (Phantom Read)
● Unreal reading means that when a user reads a range of data rows , Another thing has inserted a new line in this range , When the user reads the data within this range again , You'll find something new " The phantom " That's ok ,Innodb and Falcon The storage engine is controlled by multi version concurrency (MVCC) The mechanism solved the problem
4、 Serialization :serializable
● Highest isolation level
● By forcing things to sort , Make it impossible for transactions to conflict with each other , Thus the problem of unreal reading is solved
● in short , It is to add a shared lock to each read data row
● At this level , It may lead to a large number of timeout phenomena and lock competition
2.2 The role of isolation
If there is no isolation , Several problems that will occur
● Dirty reading (Dirty Read)
One transaction reads data from another uncommitted transaction
● It can't be read repeatedly (NonRepeatable Read)
For a data in the database , Multiple queries within a transaction range return different data values , This is due to the interval between queries , Another transaction modifies and commits the data .
The difference between unrepeatable and dirty reading is that , A dirty read is when a transaction reads dirty data that is not committed by another transaction , A non-repeatable read reads the data committed by the previous transaction .
In some cases , Unrepeatable reading is not a problem , For example, we repeatedly query a certain data, of course, the results of the final query are mainly . But in other cases, problems can occur , For example, for the same data A and B The sequence of queries may be different ,A and B A fight might break out …….
● Fantasy reading (Phantom Read)
Data inserted by another transaction is read in one transaction , It leads to inconsistencies .
Business A Read data according to certain conditions , Period affairs B Insert new data with the same search criteria , Business A When reading again according to the original conditions , Found a transaction B Newly inserted data
3、 ... and 、 Terms about transactions
● Some terms related to transactions :
● Open transaction :Start Transaction
● End of transaction :End Transaction
● Commit transaction :Commit Transaction
● Roll back the transaction :Rollback Transaction
Four 、 How to start a transaction , What is the identification ? How the transaction ends , What is the identification ?
● Open the sign :
● To perform a DML sentence , Start the transaction .
●eg:insert into、update、delete
● End mark :
● Submit : The end of success , Will all DML Statement operation history is synchronized with the data of the underlying hard disk
● Roll back : The end of failure , Will all DML The statement operation history is cleared
4.1 Example : Commit of transaction
● Submit ( Transaction success )
mysql> select * from users;
±-----±-----+
| id | name |
±-----±-----+
| 1 | abc |
±-----±-----+
1 row in set (0.00 sec)
mysql> start transaction; # Start business
Query OK, 0 rows affected (0.00 sec)
mysql> insert into users values(2,‘test’); # perform DML sentence
Query OK, 1 row affected (0.00 sec)
mysql> commit; # Submit
Query OK, 0 rows affected (0.00 sec)
mysql> select * from users; #DML The data inserted by the statement already exists in the table
±-----±-----+
| id | name |
±-----±-----+
| 1 | abc |
| 2 | test |
±-----±-----+
2 rows in set (0.00 sec)
4.2 Example : Rollback of transaction
● Roll back ( Transaction failure )
mysql> select * from users;
±-----±-----+
| id | name |
±-----±-----+
| 1 | abc |
| 2 | test |
±-----±-----+
2 rows in set (0.00 sec)
mysql> start transaction; # Start business
Query OK, 0 rows affected (0.00 sec)
mysql> insert into users values(3,‘xx’); # perform DML sentence
Query OK, 1 row affected (0.00 sec)
mysql> rollback; # Roll back
Query OK, 0 rows affected (0.00 sec)
mysql> select * from users; #DML The data inserted by the statement is not written to the table
±-----±-----+
| id | name |
±-----±-----+
| 1 | abc |
| 2 | test |
±-----±-----+
2 rows in set (0.00 sec)
5、 ... and 、 Transaction control statement
1、 Start a transaction
●begin
or
●start transaction
2、 Set the savepoint , There can be multiple savepoints in a transaction
●savepoint Save point name
3、 Commit transaction , And make all changes made in the database permanent
●commit work
or
●commit
4、 Rollback ends the user's transaction , And undo all pending changes
●rollback work
or
●rollback
5、 Delete a transaction savepoint , If no save point is specified , Executing this statement will report an error
●release savepoint Save point name
6、 Roll the transaction back to the marked point
●rollback to Marker points
7、 Set the isolation level of the transaction
●set transaction isolation level[READ UNCOMMITTED|READ COMMITTED|REPEATABLE READ|SERIALIZABLE]
●InnoDB The isolation levels that the storage engine provides for transactions are READ UNCOMMITTED、READ COMMITTED、REPEATABLE READ and SERIALIZABLE
边栏推荐
- 【滑动窗口】第九届蓝桥杯省赛B组:日志统计
- Fastjson parses JSON strings (deserialized to list, map)
- Absolute primes (C language)
- SQL:存储过程和触发器~笔记
- Is it profitable to host an Olympic Games?
- 3D face reconstruction: from basic knowledge to recognition / reconstruction methods!
- 快过年了,心也懒了
- Aiko ai Frontier promotion (7.6)
- Interviewer: what is the internal implementation of ordered collection in redis?
- Torch Cookbook
猜你喜欢
2022菲尔兹奖揭晓!首位韩裔许埈珥上榜,四位80后得奖,乌克兰女数学家成史上唯二获奖女性
嵌入式开发的7大原罪
3D face reconstruction: from basic knowledge to recognition / reconstruction methods!
Four common ways and performance comparison of ArrayList de duplication (jmh performance analysis)
Set up a time server
HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
It's not my boast. You haven't used this fairy idea plug-in!
Opencv learning example code 3.2.3 image binarization
for循环中break与continue的区别——break-完全结束循环 & continue-终止本次循环
Reviewer dis's whole research direction is not just reviewing my manuscript. What should I do?
随机推荐
It's almost the new year, and my heart is lazy
Z function (extended KMP)
Thinking about agile development
What's the best way to get TFS to output each project to its own directory?
Three schemes of SVM to realize multi classification
Quick news: the flybook players' conference is held online; Wechat payment launched "education and training service toolbox"
【mysql】触发器
JS学习笔记-OO创建怀疑的对象
[in depth learning] pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
缓存更新策略概览(Caching Strategies Overview)
Hill | insert sort
It's not my boast. You haven't used this fairy idea plug-in!
首批入选!腾讯安全天御风控获信通院业务安全能力认证
document. Usage of write () - write text - modify style and position control
红杉中国,刚刚募资90亿美元
跨分片方案 总结
Yyds dry inventory run kubeedge official example_ Counter demo counter
VIM basic configuration and frequently used commands
Math symbols in lists
技术分享 | 抓包分析 TCP 协议