当前位置:网站首页>MySQL - Detailed Explanation of Database Transactions
MySQL - Detailed Explanation of Database Transactions
2022-08-02 07:56:00 【kk_lina】
目录
一、事务的ACID特性
- 原子性:事务是一个不可分割的工作单位,要么全部提交,要么全部失败回滚;基础.
- 一致性:Data from a legitimate change into another legitimacy,This state is refers to the semantic,Related to business requirements,需要满足约束,Such as when a transfer is beyond the total;手段.
- 隔离性:A transaction is executed can't distracted by other matters,Use the data for other concurrent transactions are isolated,并发执行的各个事务之间不能互相干扰;约束条件.
- 持久性:一个事务一旦提交,它对数据库中数据的改变是永久的,The following data operation and fault should not have any influence on the.目的.
二、事务的状态
- 活动的:事务对应的数据库操作正在执行过程中时;
- 部分提交的:事务中的最后一个操作执行完成,But because the operation is performed in memory,所造成的影响并没有刷新到磁盘时,For part of the submitted state at this time;
- 失败的:Affairs are in a state of activity or part of the submission,可能遇到某些错误无法继续执行,For failure state at this time;
- 中止的:Transactions carried out part of into a failed state,那么就需要把已经修改的事务中的操作还原到事务执行前的状态.The state of the rollback is complete;
- 提交的:Part of the submitted data to modify persisted to disk.
三、使用事务
3.1、事务完成的过程
步骤一:开启事务
步骤二:一系列的DML操作
步骤三:事务结束的状态:提交的状态(COMMIT)、中止的状态(ROLLBACK)
3.2、显示事务
步骤一:开启 使用关键字:start transaction 或 begin
start transation 后面可以跟:read only / read write (默认) / with consistent snapshot(启动一致性读)
步骤二:保存点
3.3、隐式事务
autocommit:是否自动提交
3.4、事务隔离级别
Multiple requests distribution in different affairs,May also request to change a data,At this moment need a mechanism to balance the concurrency and isolation.
3.4.1、事务问题
- 脏写:一个事务A修改B未提交事务,此时B回滚,AThe modified data is invalid;
- 脏读:事务A读到了B未提交的事务,之后B回滚,A读取的内容是临时且无效的;
- 不可重复读:事务A读取一个字段,事务B更新一个字段,之后ARead the same field,值不同;
- 幻读:事务A从表中读取一个字段,事务B插入一些行,ARead the same table will be more a few lines;
3.4.2 SQL的隔离级别
mysqlThe default is repeatable isolation level.
# 查看隔离级别
show variables like 'tx_isolation';
show variables like 'transaction_isolation';
# 设置隔离级别
set [global|session] transaction isolation level 隔离级别;
set [global|session] transaction_isolation ='隔离级别' # Crossed link
四、事务日志
Mechanism of implementing transaction features.
- Transaction isolation are all made of the locking mechanism to realize;
- 事务的原子性、一致性、持久性是由事务的redo日志和undo日志来保证;
- redo log:重做日志,提供再写入操作,恢复提交事务修改的页操作,用来保证事务的持久性;Physical level changes.
- undo log: 回滚日志,回滚行记录到某个特定版本,用来保证事务的原子性、一致性.Logically modify.
4.1、redo 日志
好处:
- redo日志降低了刷盘频率;
- redo日志占用的空间非常小;
存储表空间id、页面、偏移量以及需要更新的值,所需的存储空间是很小的,刷盘快.
特点:
- redo 日志是顺序写入磁盘的;
- 事务执行过程中,redo log 不断记录;
redo 的组成:
- 重做日志的缓冲,保存在内存中,是易失的;
Brush brush plate is not really into the disk,But in the buffer to the file system,To the disk
4.2、undo日志
redo log是事务持久性的保证,undo logLog is the guarantee of atomicity,Update transaction data pre operation is to write aundo log.
- Insert the data need to record the primary key,Roll back the time according to the primary key of the corresponding value to delete;
- Delete records need to record the content,When rolled back again by the content of record is inserted into the table;
- Modify the record need to record the old values of the record,Roll back again when the record update is ready for the old value,对于每个update操作,Innodb存储引擎会执行一个相反的update,将修改前的行放回去;
- undoLog operation will be recordedredo 日志中,需要持久性的保护.
- 作用:回滚数据;多版本并发控制
边栏推荐
猜你喜欢
Hack The Box - File Transfers Module详细讲解中文教程
2022-2023 十大应用开发趋势
(2022牛客多校五)C-Bit Transmission(思维)
MySQL报错1055解决办法:[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains
条件构造器~wapper
pnpm install出现:ERR_PNPM_PEER_DEP_ISSUES Unmet peer dependencies
【CV】OpenVINO安装教程
Install Metasploitable2 on VMware
带手续费买卖股票的最大利益[找DP的状态定义到底缺什么?]
Metasploit(MSF)基础超级详细版
随机推荐
redis-高级篇
spark read local file
View port number occupancy
Install Metasploitable2 on VMware
Aided by training and learning by battle | The new version of the Offensive and Defense World Platform is officially launched!
Debian 10 dhcp relay (dhcp 中继) dhcp 固定分配
Splunk Filed Alias 字段改名
OC-NSArray
Link with Game Glitch(spfa判负环)
hdu1752 copy
敏捷、DevOps和嵌入式系统测试
About the SQL concat () function problem, how to splice
有关 sql中的 concat()函数问题,如何拼接
Xilinx约束学习笔记—— 时序约束
LeetCode brush questions (7)
regular expression
MySQL-索引优化和查询优化
OC-Category
Understand Chisel language. 31. Chisel advanced communication state machine (3) - Ready-Valid interface: definition, timing and implementation in Chisel
埋点开发流程