这一次,带你搞清楚MySQL的事务隔离级别!

论坛 期权论坛     
选择匿名的用户   2021-5-29 17:28   59   0
<div class="preview-main">
<blockquote style="line-height:inherit;font-size:.9em;color:rgb(129,145,152);border-left-width:6px;border-left-color:rgb(220,230,240);background:rgb(242,247,251);">
  <p style="font-size:inherit;color:inherit;line-height:inherit;text-align:justify;">声明:MySQL专栏学习系列,基本上是本人学习极客时间《MySQL实战45讲》专栏内容的笔记,并在专栏基础上进行知识点挖掘。侵删。</p>
  <p style="font-size:inherit;color:inherit;line-height:inherit;text-align:justify;">本人也不是什么 DBA,所以有些错误的地方请大家指正,相互交流,共同进步!</p>
</blockquote>
<p style="text-align:center;margin-left:.5em;"><img alt="bb" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-1549ed2352fea2946b206f22932da0da.jpeg"></p>
<p style="text-align:center;"><strong><span style="color:rgb(84,84,84);font-size:16px;">MySQL 专栏学习系列</span></strong></p>
<p style="text-align:center;"><span style="font-size:14px;color:rgb(61,167,66);">01 </span><span style="font-size:14px;color:rgb(61,167,66);">当执行一条select语句时,MySQL到底做了啥?</span><span style="color:rgb(61,167,66);"><span style="font-size:14px;"><br></span></span></p>
<p style="text-align:center;"><span style="font-size:14px;color:rgb(61,167,66);">02 </span><span style="font-size:14px;color:rgb(61,167,66);">MySQL是如何做到可以恢复到任意一秒状态的?</span><br></p>
<p style="text-align:center;"><br></p>
<p style="text-align:justify;margin-left:.5em;"><span style="color:rgb(84,84,84);">使用过关系型数据库的,应该都事务的概念有所了解,知道事务有 ACID 四个基本属性:原子性(Atomicity)、一致性(Consistency)、隔离性(Isolation)和持久性(Durability),今天我们主要来理解一下事务的隔离性。</span></p>
<h1 style="line-height:inherit;font-weight:bold;color:rgb(0,0,0);font-size:1.6em;border-bottom:2px solid rgb(63,63,63);text-align:justify;"><span style="font-size:inherit;color:inherit;line-height:inherit;">什么是事务?</span></h1>
<blockquote style="line-height:inherit;font-size:.9em;color:rgb(129,145,152);border-left-width:6px;border-left-color:rgb(220,230,240);background:rgb(242,247,251);">
  <p style="font-size:inherit;color:inherit;line-height:inherit;text-align:justify;">数据库事务(简称:事务)是数据库管理系统执行过程中的一个逻辑单位,由一个有限的数据库操作序列构成。—— 维基百科</p>
</blockquote>
<p style="font-size:inherit;color:inherit;line-height:inherit;text-align:justify;">事务的概念看上去不难,但是需要注意以下几个点:</p>
<p style="font-size:inherit;color:inherit;line-height:inherit;text-align:justify;">1、首先,事务就是要保证一组数据库操作,要么全部成功,要么全部失败;</p>
<p style="font-size:inherit;color:inherit;line-height:inherit;text-align:justify;">2、在 MySQL 中,事务支持是在<strong style="font-size:inherit;color:inherit;line-height:inherit;">引擎层</strong>实现的;</p>
<p style="font-size:inherit;color:inherit;line-height:inherit;text-align:justify;">3、并不是所有引擎都支持事务,如 MyISAM 就不支持,InnoDB 就支持;</p>
<hr>
<p style="font-size:inherit;color:inherit;line-height:inherit;text-align:justify;">今天,我们的主角是<strong style="font-size:inherit;color:inherit;line-height:inherit;">隔离性</strong>,隔离性是指当多个用户并发操作数据库时,数据库为每一个用户开启不同的事务,这些事务之间相互不干扰,相互隔离。</p>
<h1 style="line-height:inherit;font-weight:bold;color:rgb(0,0,0);font-size:1.6em;border-bottom:2px solid rgb(63,63,63);text-align:justify;"><span style="font-size:inherit;color:inherit;line-height:inherit;">为什么需要隔离性?</span></h1>
<p style="font-size:inherit;color:inherit;line-height:inherit;text-align:justify;">如果事务之间不是互相隔离的,可能将会出现以下问题。</p>
<h2 style="line-height:inherit;font-weight:bold;color:rgb(0,0,0);font-size:1.4em;text-align:justify;"><span style="font-size:inherit;color:inherit;line-height:inherit;">1、脏读</span></h2>
<p style="font-size:inherit;color:inherit;line-height:inherit;text-align:justify;">脏读(dirty read),简单来说,就是一个事务在处理过程中读取了另外一个事务未提交的数据。</p>
<p style="font-size:inherit;color:inherit;line-height:inherit;text-align:justify;">这种未提交的数据我们称之为脏数据。依据脏数据所做的操作肯能是不正确的。</p>
<p style="font-size:inherit;color:inherit;line-height:inherit;text-align:justify;">还记得上节中我们提到的 dirty page 吗?这种临时处理的未提交的,都是「脏」的。</p>
<p style="font-size:inherit;color:inherit;line-height:inherit;text-align:justify;"><strong style="font-size:inherit;color:inherit;line-height:inherit;">举例</strong></p>
<p style="text-align:center;"><img alt="bb" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/56
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP