当前位置:网站首页>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 日志中,需要持久性的保护.
- 作用:回滚数据;多版本并发控制

边栏推荐
- Understand the Chisel language. 30. Chisel advanced communication state machine (2) - FSMD: Take Popcount as an example
- MySQL-数据库设计规范
- 自然语言处理 文本预处理(下)(张量表示、文本数据分析、文本特征处理等)
- LeetCode 283. Shifting Zeros (Simple, Array)
- spark read local file
- MySQL database design specification
- spark 读取本地文件
- OC-NSSet(集合)
- Appium 滑动问题
- 爬虫 视频爬取工具you-get
猜你喜欢

2022-2023 十大应用开发趋势

主流定时任务解决方案全横评

通过建立新的SaaS业务来推动增长的六种方法

MySQL - slow query log

Aided by training and learning by battle | The new version of the Offensive and Defense World Platform is officially launched!

Mysql error 2003 solution Can 't connect to Mysql server on' localhost '(10061).

MySQL-数据库事务详解

redis-高级篇

(2022牛客多校五)B-Watches(二分)

研发创新编码器霍尔板,引领企业高质量发展
随机推荐
MySQL-多版本并发控制
CSRF-跨站请求伪造-相关知识
正则表达式
条件构造器~wapper
从云计算到函数计算
MySQL batch update
sql 远程访问链接服务器
OC-error prompt
OC-NSSet(集合)
数据中心的网络安全操作规范
企业实训复现指导手册——基于华为ModelArts平台的OpenPose模型的训练和推理、基于关键点数据实现对攀爬和翻越护栏两种行为的识别、并完成在图片中只标注发生行为的人
Link with Game Glitch(spfa判负环)
图腾柱和推挽电路介绍
Splunk Filed Alias 字段改名
OC-NSArray
常用的云安全防护措施盘点
postgres groupby 合并字符串
Install Metasploitable2 on VMware
MySQL-数据库事务详解
2022-08-01 第四小组 修身课 学习笔记(every day)