当前位置:网站首页>MySQL lethal serial question 1 -- are you familiar with MySQL transactions?
MySQL lethal serial question 1 -- are you familiar with MySQL transactions?
2022-07-06 02:03:00 【Two marriages with three children】
Catalog
- What is a database transaction ?
- The four characteristics of things (ACID) Introduce to you ?
- Just now you said that atomicity is based on logs Redo/Undo Mechanism , Can you say Redo/Undo Mechanism ?
- You also mentioned the isolation level of transactions , Can you say something ?
- How to realize transaction isolation ?
- When will the rollback log be deleted ?
- Why don't you suggest not ?
- You mentioned above MVCC( Multi version concurrency control ), Can you explain the principle ?
- What is multi version concurrency control ?
- InnoDB Of MVCC Implementation mechanism
- Then you know ( View ) stay MVCC How does the bottom work ?
- That's for a snapshot , You until it's MVCC Is there any rule to follow in ?
- Do you want to copy a view for each transaction pair ?
- Suppose two transactions perform a write operation , How to ensure concurrency ?
- Why use views ? What is a view ?
- What are the features of views ?
- What are the usage scenarios of views ?
- Benefits of views / shortcoming
- What is a cursor ?
What is a database transaction ?
A group of sql Database logical processing unit composed of statements , In this group sql In operation , Or it all works , Or all failed .
Transaction support is implemented at the engine level .MySQL Native MyISAM The engine does not support transactions , This is also MyISAM By InnoDB One of the important reasons for replacement .
The four characteristics of things (ACID) Introduce to you ?
1. Atomicity : Transactions are the smallest unit of execution , Division is not allowed . The atomicity of the transaction ensures that the action is either complete , Or it doesn't work at all ; It's log based Redo/Undo Mechanism .
2. Uniformity : A transaction must be in a consistent state before and after execution . such as a And b Account co ownership 1000 block , After the transfer between two people, whether it succeeds or fails , The sum of their accounts is still 1000.
3. Isolation, : When accessing the database concurrently , One user's transaction is not interfered by other transactions , The database between concurrent transactions is independent ; The isolation level can be used to solve dirty reads 、 Fantasy reading 、 Unrepeatable read problem . The tighter the isolation is , The less efficient .
4. persistence : After a transaction is committed . Its changes to the data in the database are persistent , Even if the database fails, it should not have any impact .
Just now you said that atomicity is based on logs Redo/Undo Mechanism , Can you say Redo/Undo Mechanism ?
Redo/Undo The mechanism is relatively simple , They log all updates to the data .
Redo log Used to record the modified value of a data block , Can be used to recover unwritten data file Data updated by the successful transaction of ;Undo log Is used to record the value before data update , Ensure that the data update fails and can be rolled back .
If the database is executing , Accidentally broke , You can use this log to , Successful operations have been performed before rollback , Achieve transaction consistency .
Example : If at some point the database crashes , There were transactions before the crash A And transaction B In execution , Business A Submitted , And the business B Has not been submitted . When the database restarts crash-recovery when , Would pass Redo log Write the committed transaction changes to the data file , And those that haven't been submitted will pass Undo log Conduct roll back.
You also mentioned the isolation level of transactions , Can you say something ?
Isolation level , From low to high Read uncommitted、Read committed、Repeatable read、Serializable, These four levels can solve dirty reading one by one 、 It can't be read repeatedly 、 These questions of unreal reading .
SQL The standard defines four isolation levels :
Read uncommitted : Allow read of uncommitted data changes . Lowest isolation level , Can cause dirty reading 、 Phantom or unrepeatable reading .
Read committed : After a transaction is committed , Changes will be seen . Can prevent dirty reading , But phantom or unrepeatable reads can still occur .
Repeatable : The data seen during the execution of a transaction is consistent , Other transaction change submissions are also invisible . Can prevent dirty and unrepeatable read , But phantom reading can still happen .
Serializable : Lock everything , In case of transaction read-write conflict, execute in sequence . Highest isolation level , Completely obey ACID Isolation level , in other words , This level prevents dirty reads 、 Unrepeatable reading and phantom reading .
What needs to be noted here is :Mysql Default adopted REPEATABLE_READ Isolation level Oracle Default adopted READ_COMMITTED Isolation level
How to realize transaction isolation ?
The underlying implementation uses MVCC( Multi version concurrency control ) And through the undo log Version chain and read-view To implement . When each record is updated, a rollback operation log will be recorded at the same time . There can be multiple versions of the same record in the system .
When will the rollback log be deleted ?
When there is no earlier rollback log in the system read-view When .
Why don't you suggest not ?
Long transactions mean that the system has a very old transaction view . 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 .
You mentioned above MVCC( Multi version concurrency control ), Can you explain the principle ?
In the realization of MVCC The consistency view is used , It is used to support the implementation of read submission and repeatable read .
- Repeatable isolation level , Just create the consistency view at the beginning of the transaction , Also called snapshots , The consistency view is shared in subsequent queries , The data changes made by subsequent transactions are invisible to the current transaction , This enables repeatable reading .
- Read the submission , A new view is recalculated before each statement is executed , This is also a repeatable read and read submission in MVCC Differences at the implementation level .
MVCC The biggest benefit : Read without lock , Read write conflict . In the application of reading more and writing less , It's important to read and write without conflict , It greatly increases the concurrency of the system .InnoDB Using row locks , And multi version concurrency control is adopted to improve the performance of read operation .
What is multi version concurrency control ?
In fact, two hidden columns are added after each row of records , Record creation version number and delete version number . And when every transaction starts , There will be a unique incremental version number , The manipulated data will generate a new data row ( temporary ), However, it is invisible to other transactions before committing , For data update ( Including addition, deletion and modification ) Successful operation , This version number will be updated to the row of data , Transaction submitted successfully , Update the new version number to this data row . This ensures that the data of each transaction operation , They don't affect each other , There is no lock problem .
InnoDB Of MVCC Implementation mechanism
MVCC It can be considered as a variant of row level lock , It can avoid locking operation in many cases , So the cost is lower .MVCC Most of the implementations implement non blocking read operations , Write only locks the necessary lines .InnoDB Of MVCC Realization , It is achieved by saving a snapshot of the data at a certain point in time . A business , No matter how long it takes , The internal data is consistent , That is, transactions will not affect each other during execution .
Briefly MVCC stay InnoDB In the implementation of :
InnoDB Of MVCC, This is achieved by saving two hidden columns after each row of records : A save line creation time , Expiration time of a save line ( Delete time ), Of course , The time here is not a timestamp , It's the system version number , Every time you start a new business , The system version number will be incremented . stay RR Under isolation level ,MVCC The operation is as follows :
- select operation :
InnoDB Find only versions older than ( Contains equal to ) Data row of current transaction version . It can ensure that the rows read by the transaction , Or it already exists before the transaction starts , Or records inserted or modified by the transaction itself .
The deleted version of the line is either undefined , Either greater than the current transaction version number . It can ensure that the rows read by the transaction , Not deleted before the transaction started . - insert operation : Save the newly inserted row with the current version number as the row version number .
- delete operation : Save the deleted line with the current version number as the deletion ID .
- update operation : Turn into insert and delete Combination of operations ,insert The current version number of the saved line is the line version number ,delete Save the current version number to the original line as the deletion ID .
Because old data is not really deleted , So we have to clean up the data ,innodb A background thread will be opened to perform cleanup , The specific rule is to delete the row whose deletion version number is less than the current system version , This process is called purge.
Then you know ( View ) stay MVCC How does the bottom work ?
stay InnoDB Each transaction in the has its own transaction id, And it's the only one , Incremental .
about Mysql There may be multiple versions of each data row in the , Every time a transaction updates data , Will generate a new data version , And put your own data id Assigned to the current version row trx_id.
That's for a snapshot , You until it's MVCC Is there any rule to follow in ?
For a transaction view , Except that your updates are always visible , There are three situations :
1. Version not submitted , invisible ;
2. Version submitted , But it was submitted after the view was created , invisible ;
3. Version submitted , And it was submitted before the view was created , so .
InnoDB Each transaction has a unique transaction ID, called transaction id. It is at the beginning of the transaction to InnoDB Of the transaction system , It is strictly increasing in the order of application .
And each row of data has multiple versions . 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 .
in other words , A row in a data table , There may be multiple versions (row), Each version has its own row trx_id.
Do you want to copy a view for each transaction pair ?
On the implementation , A view will be created in the database , When accessing, the logical result of the view shall prevail . stay “ Repeatable ” Under isolation level , This view is created when the transaction starts , This view is used throughout the life of the transaction . stay “ Read the submission ” Under isolation level , This view is in each SQL Created when the statement begins execution . What needs to be noted here is ,“ Read uncommitted ” Directly return the latest value on the record under isolation level , There is no view concept ; and “ Serialization ” Under isolation level, lock is used to avoid parallel access .
Suppose two transactions perform a write operation , How to ensure concurrency ?
If the business 1 And transaction 2 To perform all update operation , Business 1 First update When data rows , Get the row lock first , Lock data , When a transaction 2 To carry out update During operation , It will also get the row lock of the data row , But the transaction has been 1 occupy , Business 2 Can only wait.
If it's a business 1 The lock has not been released for a long time , Business 2 A timeout exception will appear .
Why use views ? What is a view ?
It has no physical structure , It is used to define during the execution of a transaction “ What data can I see ”, Improve complexity SQL The reusability of statements and the security of table operations .
MySQL The view of :1. Query statement virtual table (view) 2. InnoDB Realization MVCC Consistent reading view at .
What are the features of views ?
- Columns can come from different tables
- Virtual table generated by real table
- Creating and deleting do not affect the basic table
- Update view content ( add to , Delete and modify ) Directly affect the real table .
- When views come from multiple base tables , Adding and deleting data is not allowed .
What are the usage scenarios of views ?
- Protection data .
- reusing SQL sentence ;
- Change data format and presentation . Return data different from the representation and format of the underlying table .
- Simplify complex SQL operation . After writing the query , It can be easily reused without knowing its basic query details ;
- Use parts of the table instead of the entire table ;
Benefits of views / shortcoming
advantage :
- Query simplification . The view can simplify the operation of users
- Data security . Views enable users to view the same data from multiple perspectives , Can provide security protection for confidential data
- Logical data independence . Views provide a certain degree of logical independence for refactoring databases
shortcoming :
- performance . The database must convert the query of the view into the query of the basic table , If the view is defined by a complex multi table query , that , Even a simple query of views , Databases also turn it into a complex combination , It takes a certain amount of time .
- Modify restrictions . When a user tries to modify certain rows of the view , The database must translate it into changes to certain rows of the base table . in fact , When inserting or deleting from a view , The same is true of the situation . For a simple view , It's very convenient , however , For more complex views , It may be immutable .
What is a cursor ?
Cursor is a data buffer opened by the system for users , Deposit SQL Statement execution result , Each cursor area has a name . Users can get records one by one through cursors and assign them to the primary variable , Leave it to the main language for further processing .
边栏推荐
- [solution] add multiple directories in different parts of the same word document
- Computer graduation design PHP college student human resources job recruitment network
- Redis如何实现多可用区?
- Install redis
- Xshell 7 Student Edition
- 【Flask】获取请求信息、重定向、错误处理
- [flask] static file and template rendering
- Flowable source code comments (36) process instance migration status job processor, BPMN history cleanup job processor, external worker task completion job processor
- Shutter doctor: Xcode installation is incomplete
- Using SA token to solve websocket handshake authentication
猜你喜欢
A Cooperative Approach to Particle Swarm Optimization
Redis-字符串类型
[depth first search notes] Abstract DFS
Derivation of Biot Savart law in College Physics
NumPy 数组索引 切片
Selenium waiting mode
Computer graduation design PHP college student human resources job recruitment network
Concept of storage engine
[flask] official tutorial -part2: Blueprint - view, template, static file
Redis string type
随机推荐
[flask] static file and template rendering
Maya hollowed out modeling
通过PHP 获取身份证相关信息 获取生肖,获取星座,获取年龄,获取性别
NiO related knowledge (II)
Dynamics 365 开发协作最佳实践思考
Using SA token to solve websocket handshake authentication
A basic lintcode MySQL database problem
Unreal browser plug-in
MySQL index
PHP campus financial management system for computer graduation design
Flowable source code comments (36) process instance migration status job processor, BPMN history cleanup job processor, external worker task completion job processor
Basic operations of databases and tables ----- unique constraints
Computer graduation design PHP college classroom application management system
leetcode-2. Palindrome judgment
Paddle框架:PaddleNLP概述【飞桨自然语言处理开发库】
Computer graduation design PHP animation information website
阿里测开面试题
Redis list
500 lines of code to understand the principle of mecached cache client driver
[network attack and defense training exercises]