Update 数据库系统原理.md

重新绘制配图,把兼容矩阵和隔离级别对照改为表格,补充 ACID 的实现和幻读的示例,更新可串行化的描述。
This commit is contained in:
CyC2018
2026-10-26 22:04:05 +08:00
parent d446e7bd69
commit 5fe7abfcb6
25 changed files with 855 additions and 30 deletions
+58
View File
@@ -0,0 +1,58 @@
<svg xmlns="http://www.w3.org/2000/svg" width="760" height="300" viewBox="0 0 760 300">
<style>
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
.b { font-weight: bold; }
.c { text-anchor: middle; }
.e { text-anchor: end; }
.sub { font-size: 11px; fill: #6E6E6E; }
.note { fill: #C0605A; }
.ok { fill: #6A9F68; }
.mono { font-family: Menlo, Consolas, monospace; }
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
.red { stroke: #C0605A; marker-end: url(#r); }
.green { stroke: #6A9F68; marker-end: url(#g); }
.dash { stroke-dasharray: 5 4; }
.line { stroke: #555; stroke-width: 1.1; fill: none; }
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
.shadow { fill: #DADADA; }
</style>
<defs>
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
</marker>
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
</marker>
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
</marker>
</defs>
<rect width="100%" height="100%" fill="#FFFFFF"/>
<text x="380" y="28" class="title">ACID:原子性、隔离性、持久性是手段,一致性是目的</text>
<rect x="40" y="48" width="170" height="46" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
<text x="125" y="69" class="b c">原子性</text>
<text x="125" y="84" class="sub c">undo log 回滚</text>
<rect x="40" y="128" width="170" height="46" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
<text x="125" y="149" class="b c">隔离性</text>
<text x="125" y="164" class="sub c">锁和 MVCC</text>
<rect x="40" y="208" width="170" height="46" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
<text x="125" y="229" class="b c">持久性</text>
<text x="125" y="244" class="sub c">redo log 刷盘</text>
<rect x="330" y="98" width="170" height="56" rx="6" fill="#E6E3FA" stroke="#B8B0DE"/>
<text x="415" y="124" class="b c">一致性</text>
<text x="415" y="139" class="sub c">满足约束和业务规则</text>
<line x1="212" y1="70" x2="328" y2="116" class="arrow "/>
<line x1="212" y1="150" x2="328" y2="132" class="arrow "/>
<text x="270" y="82" class="c sub">没有并发时</text>
<text x="270" y="166" class="c sub">有并发时还需要</text>
<rect x="560" y="103" width="170" height="46" rx="6" fill="#C6ECE9" stroke="#7DBCB7"/>
<text x="645" y="130.5" class="c sub b">事务的结果正确</text>
<line x1="502" y1="126" x2="558" y2="126" class="arrow "/>
<rect x="330" y="208" width="170" height="46" rx="6" fill="#EAE9E7" stroke="#BFBEBA"/>
<text x="415" y="229" class="b c">系统崩溃后</text>
<text x="415" y="244" class="sub c">已提交的修改不丢失</text>
<line x1="212" y1="230" x2="328" y2="230" class="arrow "/>
<text x="40" y="284" class="sub">例如转账:原子性保证不会只扣款不入账,隔离性保证并发的转账互不干扰,持久性保证提交之后断电也不丢。</text>
</svg>

After

Width:  |  Height:  |  Size: 3.4 KiB

+61
View File
@@ -0,0 +1,61 @@
<svg xmlns="http://www.w3.org/2000/svg" width="780" height="440" viewBox="0 0 780 440">
<style>
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
.b { font-weight: bold; }
.c { text-anchor: middle; }
.e { text-anchor: end; }
.sub { font-size: 11px; fill: #6E6E6E; }
.note { fill: #C0605A; }
.ok { fill: #6A9F68; }
.mono { font-family: Menlo, Consolas, monospace; }
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
.red { stroke: #C0605A; marker-end: url(#r); }
.green { stroke: #6A9F68; marker-end: url(#g); }
.dash { stroke-dasharray: 5 4; }
.line { stroke: #555; stroke-width: 1.1; fill: none; }
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
.shadow { fill: #DADADA; }
</style>
<defs>
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
</marker>
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
</marker>
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
</marker>
</defs>
<rect width="100%" height="100%" fill="#FFFFFF"/>
<text x="390" y="28" class="title">一级封锁协议:修改前加 X 锁,事务结束才释放</text>
<rect x="40" y="50" width="335" height="34" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
<text x="207.5" y="71.5" class="c sub b">事务 T1</text>
<line x1="207.5" y1="84" x2="207.5" y2="388" stroke="#D2D2D2" stroke-dasharray="4 3"/>
<rect x="405" y="50" width="335" height="34" rx="6" fill="#C6ECE9" stroke="#7DBCB7"/>
<text x="572.5" y="71.5" class="c sub b">事务 T2</text>
<line x1="572.5" y1="84" x2="572.5" y2="388" stroke="#D2D2D2" stroke-dasharray="4 3"/>
<text x="22" y="104" class="c sub">时间</text>
<line x1="22" y1="110" x2="22" y2="382" class="arrow"/>
<rect x="46" y="96" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7FB2D6"/>
<text x="56" y="113" class="sub mono" style="font-size:10px;fill:#3F3F3F">lock-x(A)</text>
<rect x="46" y="132" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7FB2D6"/>
<text x="56" y="149" class="sub mono" style="font-size:10px;fill:#3F3F3F">read A</text>
<text x="359" y="149" class="e sub mono" style="font-size:10px">→ 100</text>
<rect x="411" y="168" width="323" height="26" rx="5" fill="#F6F6F6" stroke="#7DBCB7" stroke-dasharray="4 3"/>
<text x="421" y="185" class="sub mono note" style="font-size:10px">lock-x(A),等待 T1 释放</text>
<rect x="46" y="204" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7FB2D6"/>
<text x="56" y="221" class="sub mono" style="font-size:10px;fill:#3F3F3F">write A = 70</text>
<rect x="46" y="240" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7FB2D6"/>
<text x="56" y="257" class="sub mono" style="font-size:10px;fill:#3F3F3F">commit,unlock(A)</text>
<rect x="411" y="276" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
<text x="421" y="293" class="sub mono" style="font-size:10px;fill:#3F3F3F">获得 X 锁,read A</text>
<text x="724" y="293" class="e sub mono ok b" style="font-size:10px">→ 70</text>
<rect x="411" y="312" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
<text x="421" y="329" class="sub mono" style="font-size:10px;fill:#3F3F3F">write A = 120</text>
<rect x="411" y="348" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
<text x="421" y="365" class="sub mono" style="font-size:10px;fill:#3F3F3F">commit,unlock(A)</text>
<text x="30" y="408" class="sub b" style="fill:#3F3F3F">解决丢失修改:T2 只能在 T1 提交之后读取并修改,结果为 120。</text>
</svg>

After

Width:  |  Height:  |  Size: 4.0 KiB

+56
View File
@@ -0,0 +1,56 @@
<svg xmlns="http://www.w3.org/2000/svg" width="780" height="368" viewBox="0 0 780 368">
<style>
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
.b { font-weight: bold; }
.c { text-anchor: middle; }
.e { text-anchor: end; }
.sub { font-size: 11px; fill: #6E6E6E; }
.note { fill: #C0605A; }
.ok { fill: #6A9F68; }
.mono { font-family: Menlo, Consolas, monospace; }
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
.red { stroke: #C0605A; marker-end: url(#r); }
.green { stroke: #6A9F68; marker-end: url(#g); }
.dash { stroke-dasharray: 5 4; }
.line { stroke: #555; stroke-width: 1.1; fill: none; }
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
.shadow { fill: #DADADA; }
</style>
<defs>
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
</marker>
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
</marker>
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
</marker>
</defs>
<rect width="100%" height="100%" fill="#FFFFFF"/>
<text x="390" y="28" class="title">二级封锁协议:读取前加 S 锁,读完立即释放</text>
<rect x="40" y="50" width="335" height="34" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
<text x="207.5" y="71.5" class="c sub b">事务 T1</text>
<line x1="207.5" y1="84" x2="207.5" y2="316" stroke="#D2D2D2" stroke-dasharray="4 3"/>
<rect x="405" y="50" width="335" height="34" rx="6" fill="#C6ECE9" stroke="#7DBCB7"/>
<text x="572.5" y="71.5" class="c sub b">事务 T2</text>
<line x1="572.5" y1="84" x2="572.5" y2="316" stroke="#D2D2D2" stroke-dasharray="4 3"/>
<text x="22" y="104" class="c sub">时间</text>
<line x1="22" y1="110" x2="22" y2="310" class="arrow"/>
<rect x="46" y="96" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7FB2D6"/>
<text x="56" y="113" class="sub mono" style="font-size:10px;fill:#3F3F3F">lock-x(A)</text>
<rect x="46" y="132" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7FB2D6"/>
<text x="56" y="149" class="sub mono" style="font-size:10px;fill:#3F3F3F">write A = 200</text>
<rect x="411" y="168" width="323" height="26" rx="5" fill="#F6F6F6" stroke="#7DBCB7" stroke-dasharray="4 3"/>
<text x="421" y="185" class="sub mono note" style="font-size:10px">lock-s(A),等待 T1 释放</text>
<rect x="46" y="204" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7FB2D6"/>
<text x="56" y="221" class="sub mono" style="font-size:10px;fill:#3F3F3F">rollback,unlock(A)</text>
<rect x="411" y="240" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
<text x="421" y="257" class="sub mono" style="font-size:10px;fill:#3F3F3F">获得 S 锁,read A</text>
<text x="724" y="257" class="e sub mono ok b" style="font-size:10px">→ 100</text>
<rect x="411" y="276" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
<text x="421" y="293" class="sub mono" style="font-size:10px;fill:#3F3F3F">unlock(A)</text>
<text x="30" y="336" class="sub b" style="fill:#3F3F3F">解决读脏数据:T1 持有 X 锁期间,T2 加不上 S 锁,读不到 T1 未提交的 200。</text>
</svg>

After

Width:  |  Height:  |  Size: 3.6 KiB

+59
View File
@@ -0,0 +1,59 @@
<svg xmlns="http://www.w3.org/2000/svg" width="780" height="404" viewBox="0 0 780 404">
<style>
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
.b { font-weight: bold; }
.c { text-anchor: middle; }
.e { text-anchor: end; }
.sub { font-size: 11px; fill: #6E6E6E; }
.note { fill: #C0605A; }
.ok { fill: #6A9F68; }
.mono { font-family: Menlo, Consolas, monospace; }
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
.red { stroke: #C0605A; marker-end: url(#r); }
.green { stroke: #6A9F68; marker-end: url(#g); }
.dash { stroke-dasharray: 5 4; }
.line { stroke: #555; stroke-width: 1.1; fill: none; }
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
.shadow { fill: #DADADA; }
</style>
<defs>
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
</marker>
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
</marker>
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
</marker>
</defs>
<rect width="100%" height="100%" fill="#FFFFFF"/>
<text x="390" y="28" class="title">三级封锁协议:读取前加 S 锁,事务结束才释放</text>
<rect x="40" y="50" width="335" height="34" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
<text x="207.5" y="71.5" class="c sub b">事务 T1</text>
<line x1="207.5" y1="84" x2="207.5" y2="352" stroke="#D2D2D2" stroke-dasharray="4 3"/>
<rect x="405" y="50" width="335" height="34" rx="6" fill="#C6ECE9" stroke="#7DBCB7"/>
<text x="572.5" y="71.5" class="c sub b">事务 T2</text>
<line x1="572.5" y1="84" x2="572.5" y2="352" stroke="#D2D2D2" stroke-dasharray="4 3"/>
<text x="22" y="104" class="c sub">时间</text>
<line x1="22" y1="110" x2="22" y2="346" class="arrow"/>
<rect x="411" y="96" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
<text x="421" y="113" class="sub mono" style="font-size:10px;fill:#3F3F3F">lock-s(A)</text>
<rect x="411" y="132" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
<text x="421" y="149" class="sub mono" style="font-size:10px;fill:#3F3F3F">read A</text>
<text x="724" y="149" class="e sub mono" style="font-size:10px">→ 100</text>
<rect x="46" y="168" width="323" height="26" rx="5" fill="#F6F6F6" stroke="#7FB2D6" stroke-dasharray="4 3"/>
<text x="56" y="185" class="sub mono note" style="font-size:10px">lock-x(A),等待 T2 释放</text>
<rect x="411" y="204" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
<text x="421" y="221" class="sub mono" style="font-size:10px;fill:#3F3F3F">read A</text>
<text x="724" y="221" class="e sub mono ok b" style="font-size:10px">→ 100</text>
<rect x="411" y="240" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
<text x="421" y="257" class="sub mono" style="font-size:10px;fill:#3F3F3F">commit,unlock(A)</text>
<rect x="46" y="276" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7FB2D6"/>
<text x="56" y="293" class="sub mono" style="font-size:10px;fill:#3F3F3F">获得 X 锁,write A = 200</text>
<rect x="46" y="312" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7FB2D6"/>
<text x="56" y="329" class="sub mono" style="font-size:10px;fill:#3F3F3F">commit,unlock(A)</text>
<text x="30" y="372" class="sub b" style="fill:#3F3F3F">解决不可重复读:T2 结束之前,T1 无法修改 A,T2 两次读到的都是 100。</text>
</svg>

After

Width:  |  Height:  |  Size: 3.9 KiB

+76
View File
@@ -0,0 +1,76 @@
<svg xmlns="http://www.w3.org/2000/svg" width="760" height="400" viewBox="0 0 760 400">
<style>
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
.b { font-weight: bold; }
.c { text-anchor: middle; }
.e { text-anchor: end; }
.sub { font-size: 11px; fill: #6E6E6E; }
.note { fill: #C0605A; }
.ok { fill: #6A9F68; }
.mono { font-family: Menlo, Consolas, monospace; }
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
.red { stroke: #C0605A; marker-end: url(#r); }
.green { stroke: #6A9F68; marker-end: url(#g); }
.dash { stroke-dasharray: 5 4; }
.line { stroke: #555; stroke-width: 1.1; fill: none; }
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
.shadow { fill: #DADADA; }
</style>
<defs>
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
</marker>
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
</marker>
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
</marker>
</defs>
<rect width="100%" height="100%" fill="#FFFFFF"/>
<text x="380" y="28" class="title">ReadView:沿版本链找到第一个对自己可见的版本</text>
<text x="30" y="64" class="sub">读事务开始读取时,事务 103、105 还没有提交,下一个要分配的事务 ID 是 106:</text>
<text x="30" y="82" class="sub mono" style="fill:#3F3F3F">TRX_IDs = {103, 105},TRX_ID_MIN = 103,TRX_ID_MAX = 106</text>
<line x1="50" y1="140" x2="650" y2="140" class="arrow"/>
<text x="650" y="128" class="e sub">事务 ID</text>
<circle cx="90" cy="140" r="15" fill="#C6ECE9" stroke="#7DBCB7"/>
<text x="90" y="144" class="c sub mono b" style="fill:#3F3F3F">101</text>
<text x="90" y="176" class="c sub b ok">可见</text>
<text x="90" y="194" class="c sub">已提交</text>
<circle cx="190" cy="140" r="15" fill="#C6ECE9" stroke="#7DBCB7"/>
<text x="190" y="144" class="c sub mono b" style="fill:#3F3F3F">102</text>
<text x="190" y="176" class="c sub b ok">可见</text>
<text x="190" y="194" class="c sub">已提交</text>
<circle cx="290" cy="140" r="15" fill="#F3C6C2" stroke="#D99A94"/>
<text x="290" y="144" class="c sub mono b" style="fill:#3F3F3F">103</text>
<text x="290" y="176" class="c sub b note">不可见</text>
<text x="290" y="194" class="c sub">在 TRX_IDs 中</text>
<circle cx="390" cy="140" r="15" fill="#C6ECE9" stroke="#7DBCB7"/>
<text x="390" y="144" class="c sub mono b" style="fill:#3F3F3F">104</text>
<text x="390" y="176" class="c sub b ok">可见</text>
<text x="390" y="194" class="c sub">已提交</text>
<circle cx="490" cy="140" r="15" fill="#F3C6C2" stroke="#D99A94"/>
<text x="490" y="144" class="c sub mono b" style="fill:#3F3F3F">105</text>
<text x="490" y="176" class="c sub b note">不可见</text>
<text x="490" y="194" class="c sub">在 TRX_IDs 中</text>
<circle cx="590" cy="140" r="15" fill="#F3C6C2" stroke="#D99A94"/>
<text x="590" y="144" class="c sub mono b" style="fill:#3F3F3F">106</text>
<text x="590" y="176" class="c sub b note">不可见</text>
<text x="590" y="194" class="c sub">之后才开始</text>
<text x="290" y="112" class="c sub mono" style="fill:#766AA8">TRX_ID_MIN</text>
<text x="590" y="112" class="c sub mono" style="fill:#766AA8">TRX_ID_MAX</text>
<text x="30" y="236" class="sub b">读取 id = 1 这一行:</text>
<rect x="30" y="250" width="150" height="34" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
<text x="105" y="271.5" class="c sub b">版本 x = 'c'</text>
<text x="196" y="265" class="sub mono">DB_TRX_ID = 103,在 TRX_IDs 中</text>
<text x="196" y="281" class="sub b note">不可见,沿回滚指针找上一个版本</text>
<line x1="105" y1="284" x2="105" y2="300" class="arrow "/>
<rect x="30" y="300" width="150" height="34" rx="6" fill="#C6ECE9" stroke="#7DBCB7"/>
<text x="105" y="321.5" class="c sub b">版本 x = 'b'</text>
<text x="196" y="315" class="sub mono">DB_TRX_ID = 102,小于 TRX_ID_MIN</text>
<text x="196" y="331" class="sub b ok">可见,读到 'b'</text>
<text x="30" y="374" class="sub">可重复读在第一次读取时创建 ReadView,之后一直使用它;提交读每次读取都创建新的 ReadView,103 提交后就能读到 'c'。</text>
<text x="30" y="392" class="sub">事务 ID 为示意;读到的结果在 MySQL 8.0 上实测一致。</text>
</svg>

After

Width:  |  Height:  |  Size: 4.8 KiB

+91
View File
@@ -0,0 +1,91 @@
<svg xmlns="http://www.w3.org/2000/svg" width="760" height="330" viewBox="0 0 760 330">
<style>
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
.b { font-weight: bold; }
.c { text-anchor: middle; }
.e { text-anchor: end; }
.sub { font-size: 11px; fill: #6E6E6E; }
.note { fill: #C0605A; }
.ok { fill: #6A9F68; }
.mono { font-family: Menlo, Consolas, monospace; }
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
.red { stroke: #C0605A; marker-end: url(#r); }
.green { stroke: #6A9F68; marker-end: url(#g); }
.dash { stroke-dasharray: 5 4; }
.line { stroke: #555; stroke-width: 1.1; fill: none; }
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
.shadow { fill: #DADADA; }
</style>
<defs>
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
</marker>
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
</marker>
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
</marker>
</defs>
<rect width="100%" height="100%" fill="#FFFFFF"/>
<text x="380" y="28" class="title">Undo 版本链:每次修改都把旧版本写入 undo log,用回滚指针连起来</text>
<text x="40" y="70" class="sub b">当前的数据行</text>
<rect x="40" y="76" width="44" height="24" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
<text x="62" y="92" class="c sub b" style="fill:#444">id</text>
<rect x="84" y="76" width="44" height="24" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
<text x="106" y="92" class="c sub b" style="fill:#444">x</text>
<rect x="128" y="76" width="92" height="24" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
<text x="174" y="92" class="c sub b" style="fill:#444">DB_TRX_ID</text>
<rect x="220" y="76" width="104" height="24" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
<text x="272" y="92" class="c sub b" style="fill:#444">DB_ROLL_PTR</text>
<rect x="40" y="100" width="44" height="24" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="62" y="116" class="c sub mono" style="fill:#444">1</text>
<rect x="84" y="100" width="44" height="24" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="106" y="116" class="c sub mono" style="fill:#444">c</text>
<rect x="128" y="100" width="92" height="24" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="174" y="116" class="c sub mono" style="fill:#444">103</text>
<rect x="220" y="100" width="104" height="24" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="272" y="116" class="c sub mono" style="fill:#444">→ 版本 2</text>
<text x="350" y="106" class="sub mono">UPDATE t SET x = 'c'(事务 103)</text>
<line x1="272" y1="125" x2="272" y2="152" class="arrow "/>
<text x="40" y="148" class="sub b">undo log 中的版本 2</text>
<rect x="40" y="154" width="44" height="24" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
<text x="62" y="170" class="c sub b" style="fill:#444">id</text>
<rect x="84" y="154" width="44" height="24" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
<text x="106" y="170" class="c sub b" style="fill:#444">x</text>
<rect x="128" y="154" width="92" height="24" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
<text x="174" y="170" class="c sub b" style="fill:#444">DB_TRX_ID</text>
<rect x="220" y="154" width="104" height="24" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
<text x="272" y="170" class="c sub b" style="fill:#444">DB_ROLL_PTR</text>
<rect x="40" y="178" width="44" height="24" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="62" y="194" class="c sub mono" style="fill:#444">1</text>
<rect x="84" y="178" width="44" height="24" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="106" y="194" class="c sub mono" style="fill:#444">b</text>
<rect x="128" y="178" width="92" height="24" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="174" y="194" class="c sub mono" style="fill:#444">102</text>
<rect x="220" y="178" width="104" height="24" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="272" y="194" class="c sub mono" style="fill:#444">→ 版本 1</text>
<text x="350" y="184" class="sub mono">UPDATE t SET x = 'b'(事务 102)</text>
<line x1="272" y1="203" x2="272" y2="230" class="arrow "/>
<text x="40" y="226" class="sub b">undo log 中的版本 1</text>
<rect x="40" y="232" width="44" height="24" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
<text x="62" y="248" class="c sub b" style="fill:#444">id</text>
<rect x="84" y="232" width="44" height="24" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
<text x="106" y="248" class="c sub b" style="fill:#444">x</text>
<rect x="128" y="232" width="92" height="24" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
<text x="174" y="248" class="c sub b" style="fill:#444">DB_TRX_ID</text>
<rect x="220" y="232" width="104" height="24" rx="3" fill="#EAE9E7" stroke="#BFBEBA" stroke-width="1"/>
<text x="272" y="248" class="c sub b" style="fill:#444">DB_ROLL_PTR</text>
<rect x="40" y="256" width="44" height="24" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="62" y="272" class="c sub mono" style="fill:#444">1</text>
<rect x="84" y="256" width="44" height="24" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="106" y="272" class="c sub mono" style="fill:#444">a</text>
<rect x="128" y="256" width="92" height="24" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="174" y="272" class="c sub mono" style="fill:#444">101</text>
<rect x="220" y="256" width="104" height="24" rx="3" fill="#FFFFFF" stroke="#BBBBBB" stroke-width="1"/>
<text x="272" y="272" class="c sub mono" style="fill:#444">无更早版本</text>
<text x="350" y="262" class="sub mono">INSERT INTO t VALUES (1, 'a')(事务 101)</text>
<text x="40" y="314" class="sub">DB_TRX_ID 是最后修改这一行的事务 ID,DB_ROLL_PTR 指向上一个版本;事务 ID 为示意。</text>
</svg>

After

Width:  |  Height:  |  Size: 6.5 KiB

+79
View File
@@ -0,0 +1,79 @@
<svg xmlns="http://www.w3.org/2000/svg" width="760" height="330" viewBox="0 0 760 330">
<style>
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
.b { font-weight: bold; }
.c { text-anchor: middle; }
.e { text-anchor: end; }
.sub { font-size: 11px; fill: #6E6E6E; }
.note { fill: #C0605A; }
.ok { fill: #6A9F68; }
.mono { font-family: Menlo, Consolas, monospace; }
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
.red { stroke: #C0605A; marker-end: url(#r); }
.green { stroke: #6A9F68; marker-end: url(#g); }
.dash { stroke-dasharray: 5 4; }
.line { stroke: #555; stroke-width: 1.1; fill: none; }
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
.shadow { fill: #DADADA; }
</style>
<defs>
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
</marker>
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
</marker>
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
</marker>
</defs>
<rect width="100%" height="100%" fill="#FFFFFF"/>
<text x="380" y="28" class="title">Next-Key Locks:锁住记录和它前面的间隙</text>
<text x="30" y="70" class="sub mono b" style="fill:#3F3F3F">① SELECT * FROM t WHERE c = 11 FOR UPDATE</text>
<rect x="194" y="94" width="102" height="14" rx="3" fill="#E6E3FA" stroke="#B8B0DE"/>
<text x="245" y="105" class="c sub mono" style="font-size:10px;fill:#3F3F3F">(10, 11]</text>
<rect x="304" y="94" width="112" height="14" rx="3" fill="#C6E4FB" stroke="#7FB2D6"/>
<text x="360" y="105" class="c sub mono" style="font-size:10px;fill:#3F3F3F">(11, 13)</text>
<line x1="60" y1="116" x2="720" y2="116" stroke="#999"/>
<text x="60" y="134" class="c sub mono">-∞</text>
<circle cx="190" cy="116" r="4" fill="#555"/>
<text x="190" y="134" class="c sub mono">c=10</text>
<circle cx="300" cy="116" r="4" fill="#555"/>
<text x="300" y="134" class="c sub mono">c=11</text>
<circle cx="420" cy="116" r="4" fill="#555"/>
<text x="420" y="134" class="c sub mono">c=13</text>
<circle cx="580" cy="116" r="4" fill="#555"/>
<text x="580" y="134" class="c sub mono">c=20</text>
<text x="720" y="134" class="c sub mono">+∞</text>
<text x="30" y="156" class="sub">data_locks:c = 11 的记录上是 X(Next-Key Lock),c = 13 上是 X,GAP;插入 c = 12 被阻塞,c = 9、14、21 可以插入</text>
<text x="30" y="200" class="sub mono b" style="fill:#3F3F3F">② SELECT * FROM t WHERE c BETWEEN 10 AND 20 FOR UPDATE</text>
<rect x="64" y="224" width="122" height="14" rx="3" fill="#E6E3FA" stroke="#B8B0DE"/>
<text x="125" y="235" class="c sub mono" style="font-size:10px;fill:#3F3F3F">(-∞, 10]</text>
<rect x="194" y="224" width="102" height="14" rx="3" fill="#E6E3FA" stroke="#B8B0DE"/>
<text x="245" y="235" class="c sub mono" style="font-size:10px;fill:#3F3F3F">(10, 11]</text>
<rect x="304" y="224" width="112" height="14" rx="3" fill="#E6E3FA" stroke="#B8B0DE"/>
<text x="360" y="235" class="c sub mono" style="font-size:10px;fill:#3F3F3F">(11, 13]</text>
<rect x="424" y="224" width="152" height="14" rx="3" fill="#E6E3FA" stroke="#B8B0DE"/>
<text x="500" y="235" class="c sub mono" style="font-size:10px;fill:#3F3F3F">(13, 20]</text>
<rect x="584" y="224" width="132" height="14" rx="3" fill="#E6E3FA" stroke="#B8B0DE"/>
<text x="650" y="235" class="c sub mono" style="font-size:10px;fill:#3F3F3F">(20, +∞]</text>
<line x1="60" y1="246" x2="720" y2="246" stroke="#999"/>
<text x="60" y="264" class="c sub mono">-∞</text>
<circle cx="190" cy="246" r="4" fill="#555"/>
<text x="190" y="264" class="c sub mono">c=10</text>
<circle cx="300" cy="246" r="4" fill="#555"/>
<text x="300" y="264" class="c sub mono">c=11</text>
<circle cx="420" cy="246" r="4" fill="#555"/>
<text x="420" y="264" class="c sub mono">c=13</text>
<circle cx="580" cy="246" r="4" fill="#555"/>
<text x="580" y="264" class="c sub mono">c=20</text>
<text x="720" y="264" class="c sub mono">+∞</text>
<text x="30" y="286" class="sub">data_locks:c = 10、11、13、20 和 supremum 上都是 X;插入 c = 9、12、14、21 都被阻塞</text>
<rect x="30" y="302" width="18" height="12" rx="3" fill="#E6E3FA" stroke="#B8B0DE"/>
<text x="54" y="312" class="sub">Next-Key Lock(前开后闭)</text>
<rect x="230" y="302" width="18" height="12" rx="3" fill="#C6E4FB" stroke="#7FB2D6"/>
<text x="254" y="312" class="sub">Gap Lock(开区间)</text>
<text x="420" y="312" class="sub">MySQL 8.0,c 上有普通索引,可重复读</text>
</svg>

After

Width:  |  Height:  |  Size: 5.0 KiB

+57
View File
@@ -0,0 +1,57 @@
<svg xmlns="http://www.w3.org/2000/svg" width="780" height="354" viewBox="0 0 780 354">
<style>
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
.b { font-weight: bold; }
.c { text-anchor: middle; }
.e { text-anchor: end; }
.sub { font-size: 11px; fill: #6E6E6E; }
.note { fill: #C0605A; }
.ok { fill: #6A9F68; }
.mono { font-family: Menlo, Consolas, monospace; }
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
.red { stroke: #C0605A; marker-end: url(#r); }
.green { stroke: #6A9F68; marker-end: url(#g); }
.dash { stroke-dasharray: 5 4; }
.line { stroke: #555; stroke-width: 1.1; fill: none; }
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
.shadow { fill: #DADADA; }
</style>
<defs>
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
</marker>
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
</marker>
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
</marker>
</defs>
<rect width="100%" height="100%" fill="#FFFFFF"/>
<text x="390" y="28" class="title">读脏数据:读到了另一个事务没有提交的修改</text>
<rect x="40" y="50" width="335" height="34" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
<text x="207.5" y="71.5" class="c sub b">事务 T1</text>
<line x1="207.5" y1="84" x2="207.5" y2="280" stroke="#D2D2D2" stroke-dasharray="4 3"/>
<rect x="405" y="50" width="335" height="34" rx="6" fill="#C6ECE9" stroke="#7DBCB7"/>
<text x="572.5" y="65" class="c sub b">事务 T2</text>
<text x="572.5" y="80" class="sub c">READ UNCOMMITTED</text>
<line x1="572.5" y1="84" x2="572.5" y2="280" stroke="#D2D2D2" stroke-dasharray="4 3"/>
<text x="22" y="104" class="c sub">时间</text>
<line x1="22" y1="110" x2="22" y2="274" class="arrow"/>
<rect x="46" y="96" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7FB2D6"/>
<text x="56" y="113" class="sub mono" style="font-size:10px;fill:#3F3F3F">BEGIN</text>
<rect x="46" y="132" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7FB2D6"/>
<text x="56" y="149" class="sub mono" style="font-size:10px;fill:#3F3F3F">UPDATE account SET balance = 200 WHERE id = 1</text>
<rect x="411" y="168" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
<text x="421" y="185" class="sub mono" style="font-size:10px;fill:#3F3F3F">SELECT balance FROM account WHERE id = 1</text>
<text x="724" y="185" class="e sub mono note b" style="font-size:10px">→ 200</text>
<rect x="46" y="204" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7FB2D6"/>
<text x="56" y="221" class="sub mono" style="font-size:10px;fill:#3F3F3F">ROLLBACK</text>
<rect x="411" y="240" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
<text x="421" y="257" class="sub mono" style="font-size:10px;fill:#3F3F3F">SELECT balance FROM account WHERE id = 1</text>
<text x="724" y="257" class="e sub mono" style="font-size:10px">→ 100</text>
<text x="30" y="300" class="sub b" style="fill:#3F3F3F">T2 读到的 200 从来没有提交过。</text>
<text x="30" y="322" class="sub" style="">T2 的隔离级别为 READ COMMITTED 及以上时,第一次读到的是 100。</text>
</svg>

After

Width:  |  Height:  |  Size: 3.7 KiB

+62
View File
@@ -0,0 +1,62 @@
<svg xmlns="http://www.w3.org/2000/svg" width="760" height="300" viewBox="0 0 760 300">
<style>
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
.b { font-weight: bold; }
.c { text-anchor: middle; }
.e { text-anchor: end; }
.sub { font-size: 11px; fill: #6E6E6E; }
.note { fill: #C0605A; }
.ok { fill: #6A9F68; }
.mono { font-family: Menlo, Consolas, monospace; }
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
.red { stroke: #C0605A; marker-end: url(#r); }
.green { stroke: #6A9F68; marker-end: url(#g); }
.dash { stroke-dasharray: 5 4; }
.line { stroke: #555; stroke-width: 1.1; fill: none; }
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
.shadow { fill: #DADADA; }
</style>
<defs>
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
</marker>
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
</marker>
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
</marker>
</defs>
<rect width="100%" height="100%" fill="#FFFFFF"/>
<text x="380" y="28" class="title">事务:要么全部提交,要么全部回滚</text>
<text x="30" y="64" class="sub">从 A 账户向 B 账户转账 30 元</text>
<rect x="30" y="84" width="140" height="58" rx="6" fill="#EAE9E7" stroke="#BFBEBA"/>
<text x="100" y="102" class="c sub b">一致的状态</text>
<text x="100" y="120" class="c sub mono">A = 100</text>
<text x="100" y="136" class="c sub mono">B = 0</text>
<rect x="220" y="84" width="140" height="58" rx="6" fill="#E6E3FA" stroke="#B8B0DE"/>
<text x="290" y="102" class="c sub b">中间状态</text>
<text x="290" y="120" class="c sub mono">A = 70</text>
<text x="290" y="136" class="c sub mono">B = 0</text>
<rect x="410" y="84" width="140" height="58" rx="6" fill="#E6E3FA" stroke="#B8B0DE"/>
<text x="480" y="102" class="c sub b">中间状态</text>
<text x="480" y="120" class="c sub mono">A = 70</text>
<text x="480" y="136" class="c sub mono">B = 30</text>
<rect x="600" y="84" width="140" height="58" rx="6" fill="#C6ECE9" stroke="#7DBCB7"/>
<text x="670" y="102" class="c sub b">一致的状态</text>
<text x="670" y="120" class="c sub mono">A = 70</text>
<text x="670" y="136" class="c sub mono">B = 30</text>
<line x1="172" y1="116" x2="216" y2="116" class="arrow "/>
<text x="196" y="106" class="c sub mono" style="font-size:10px">A − 30</text>
<line x1="362" y1="116" x2="406" y2="116" class="arrow "/>
<text x="386" y="106" class="c sub mono" style="font-size:10px">B + 30</text>
<line x1="552" y1="116" x2="596" y2="116" class="arrow "/>
<text x="576" y="106" class="c sub mono" style="font-size:10px">COMMIT</text>
<path d="M290,144 V186 H100 V148" class="arrow red dash"/>
<path d="M480,144 V186 H100 V148" class="arrow red dash"/>
<text x="250" y="206" class="sub note">出错或执行 ROLLBACK:用 undo log 撤销已做的修改,回到初始状态</text>
<text x="30" y="254" class="sub">在提交读及以上的隔离级别下,中间状态只有当前事务看得到;提交后修改才对其它事务可见,并由 redo log 保证不会丢失。</text>
<text x="30" y="274" class="sub">A、B 两个账户的余额总和在事务前后都是 100,这就是一致性。</text>
</svg>

After

Width:  |  Height:  |  Size: 3.7 KiB

+63
View File
@@ -0,0 +1,63 @@
<svg xmlns="http://www.w3.org/2000/svg" width="780" height="426" viewBox="0 0 780 426">
<style>
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
.b { font-weight: bold; }
.c { text-anchor: middle; }
.e { text-anchor: end; }
.sub { font-size: 11px; fill: #6E6E6E; }
.note { fill: #C0605A; }
.ok { fill: #6A9F68; }
.mono { font-family: Menlo, Consolas, monospace; }
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
.red { stroke: #C0605A; marker-end: url(#r); }
.green { stroke: #6A9F68; marker-end: url(#g); }
.dash { stroke-dasharray: 5 4; }
.line { stroke: #555; stroke-width: 1.1; fill: none; }
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
.shadow { fill: #DADADA; }
</style>
<defs>
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
</marker>
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
</marker>
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
</marker>
</defs>
<rect width="100%" height="100%" fill="#FFFFFF"/>
<text x="390" y="28" class="title">丢失修改:两个事务基于同一个旧值计算后写回</text>
<rect x="40" y="50" width="335" height="34" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
<text x="207.5" y="65" class="c sub b">事务 T1:取出 30 元</text>
<text x="207.5" y="80" class="sub c">REPEATABLE READ</text>
<line x1="207.5" y1="84" x2="207.5" y2="352" stroke="#D2D2D2" stroke-dasharray="4 3"/>
<rect x="405" y="50" width="335" height="34" rx="6" fill="#C6ECE9" stroke="#7DBCB7"/>
<text x="572.5" y="65" class="c sub b">事务 T2:存入 50 元</text>
<text x="572.5" y="80" class="sub c">REPEATABLE READ</text>
<line x1="572.5" y1="84" x2="572.5" y2="352" stroke="#D2D2D2" stroke-dasharray="4 3"/>
<text x="22" y="104" class="c sub">时间</text>
<line x1="22" y1="110" x2="22" y2="346" class="arrow"/>
<rect x="46" y="96" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7FB2D6"/>
<text x="56" y="113" class="sub mono" style="font-size:10px;fill:#3F3F3F">SELECT balance FROM account WHERE id = 1</text>
<text x="359" y="113" class="e sub mono" style="font-size:10px">→ 100</text>
<rect x="411" y="132" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
<text x="421" y="149" class="sub mono" style="font-size:10px;fill:#3F3F3F">SELECT balance FROM account WHERE id = 1</text>
<text x="724" y="149" class="e sub mono" style="font-size:10px">→ 100</text>
<rect x="46" y="168" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7FB2D6"/>
<text x="56" y="185" class="sub mono" style="font-size:10px;fill:#3F3F3F">UPDATE account SET balance = 70 WHERE id = 1</text>
<rect x="46" y="204" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7FB2D6"/>
<text x="56" y="221" class="sub mono" style="font-size:10px;fill:#3F3F3F">COMMIT</text>
<rect x="411" y="240" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
<text x="421" y="257" class="sub mono" style="font-size:10px;fill:#3F3F3F">UPDATE account SET balance = 150 WHERE id = 1</text>
<rect x="411" y="276" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
<text x="421" y="293" class="sub mono" style="font-size:10px;fill:#3F3F3F">COMMIT</text>
<rect x="46" y="312" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7FB2D6"/>
<text x="56" y="329" class="sub mono" style="font-size:10px;fill:#3F3F3F">SELECT balance FROM account WHERE id = 1</text>
<text x="359" y="329" class="e sub mono note b" style="font-size:10px">→ 150</text>
<text x="30" y="372" class="sub b" style="fill:#3F3F3F">正确结果应为 120,T1 取出的 30 元丢失了。</text>
<text x="30" y="394" class="sub" style="">改为 SET balance = balance - 30 后,T2 的 UPDATE 会等待 T1 的行锁,再在 70 的基础上加 50,结果为 120。</text>
</svg>

After

Width:  |  Height:  |  Size: 4.3 KiB

+58
View File
@@ -0,0 +1,58 @@
<svg xmlns="http://www.w3.org/2000/svg" width="780" height="354" viewBox="0 0 780 354">
<style>
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
.b { font-weight: bold; }
.c { text-anchor: middle; }
.e { text-anchor: end; }
.sub { font-size: 11px; fill: #6E6E6E; }
.note { fill: #C0605A; }
.ok { fill: #6A9F68; }
.mono { font-family: Menlo, Consolas, monospace; }
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
.red { stroke: #C0605A; marker-end: url(#r); }
.green { stroke: #6A9F68; marker-end: url(#g); }
.dash { stroke-dasharray: 5 4; }
.line { stroke: #555; stroke-width: 1.1; fill: none; }
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
.shadow { fill: #DADADA; }
</style>
<defs>
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
</marker>
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
</marker>
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
</marker>
</defs>
<rect width="100%" height="100%" fill="#FFFFFF"/>
<text x="390" y="28" class="title">不可重复读:同一个事务两次读取同一行,结果不同</text>
<rect x="40" y="50" width="335" height="34" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
<text x="207.5" y="65" class="c sub b">事务 T1</text>
<text x="207.5" y="80" class="sub c">自动提交</text>
<line x1="207.5" y1="84" x2="207.5" y2="280" stroke="#D2D2D2" stroke-dasharray="4 3"/>
<rect x="405" y="50" width="335" height="34" rx="6" fill="#C6ECE9" stroke="#7DBCB7"/>
<text x="572.5" y="65" class="c sub b">事务 T2</text>
<text x="572.5" y="80" class="sub c">READ COMMITTED</text>
<line x1="572.5" y1="84" x2="572.5" y2="280" stroke="#D2D2D2" stroke-dasharray="4 3"/>
<text x="22" y="104" class="c sub">时间</text>
<line x1="22" y1="110" x2="22" y2="274" class="arrow"/>
<rect x="411" y="96" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
<text x="421" y="113" class="sub mono" style="font-size:10px;fill:#3F3F3F">BEGIN</text>
<rect x="411" y="132" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
<text x="421" y="149" class="sub mono" style="font-size:10px;fill:#3F3F3F">SELECT balance FROM account WHERE id = 1</text>
<text x="724" y="149" class="e sub mono" style="font-size:10px">→ 100</text>
<rect x="46" y="168" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7FB2D6"/>
<text x="56" y="185" class="sub mono" style="font-size:10px;fill:#3F3F3F">UPDATE account SET balance = 200 WHERE id = 1</text>
<rect x="411" y="204" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
<text x="421" y="221" class="sub mono" style="font-size:10px;fill:#3F3F3F">SELECT balance FROM account WHERE id = 1</text>
<text x="724" y="221" class="e sub mono note b" style="font-size:10px">→ 200</text>
<rect x="411" y="240" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
<text x="421" y="257" class="sub mono" style="font-size:10px;fill:#3F3F3F">COMMIT</text>
<text x="30" y="300" class="sub b" style="fill:#3F3F3F">T1 在 T2 的两次读取之间修改并提交了这一行。</text>
<text x="30" y="322" class="sub" style="">T2 的隔离级别为 REPEATABLE READ 时,第二次读到的仍然是 100。</text>
</svg>

After

Width:  |  Height:  |  Size: 3.7 KiB

+58
View File
@@ -0,0 +1,58 @@
<svg xmlns="http://www.w3.org/2000/svg" width="780" height="354" viewBox="0 0 780 354">
<style>
text { font-family: "PingFang SC", "Microsoft YaHei", Helvetica, Arial, sans-serif; fill: #3F3F3F; font-size: 12px; }
.title { font-weight: bold; font-size: 15px; fill: #333; text-anchor: middle; }
.b { font-weight: bold; }
.c { text-anchor: middle; }
.e { text-anchor: end; }
.sub { font-size: 11px; fill: #6E6E6E; }
.note { fill: #C0605A; }
.ok { fill: #6A9F68; }
.mono { font-family: Menlo, Consolas, monospace; }
.arrow { stroke: #555; stroke-width: 1.1; fill: none; marker-end: url(#a); }
.red { stroke: #C0605A; marker-end: url(#r); }
.green { stroke: #6A9F68; marker-end: url(#g); }
.dash { stroke-dasharray: 5 4; }
.line { stroke: #555; stroke-width: 1.1; fill: none; }
.frame { fill: none; stroke: #999; stroke-width: 1; stroke-dasharray: 4 3; }
.shadow { fill: #DADADA; }
</style>
<defs>
<marker id="a" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#555"/>
</marker>
<marker id="r" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#C0605A"/>
</marker>
<marker id="g" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse">
<path d="M1,1.5 L9.5,5 L1,8.5 z" fill="#6A9F68"/>
</marker>
</defs>
<rect width="100%" height="100%" fill="#FFFFFF"/>
<text x="390" y="28" class="title">幻影读:同一个事务两次范围查询,行数不同</text>
<rect x="40" y="50" width="335" height="34" rx="6" fill="#C6E4FB" stroke="#7FB2D6"/>
<text x="207.5" y="65" class="c sub b">事务 T1</text>
<text x="207.5" y="80" class="sub c">自动提交</text>
<line x1="207.5" y1="84" x2="207.5" y2="280" stroke="#D2D2D2" stroke-dasharray="4 3"/>
<rect x="405" y="50" width="335" height="34" rx="6" fill="#C6ECE9" stroke="#7DBCB7"/>
<text x="572.5" y="65" class="c sub b">事务 T2</text>
<text x="572.5" y="80" class="sub c">READ COMMITTED</text>
<line x1="572.5" y1="84" x2="572.5" y2="280" stroke="#D2D2D2" stroke-dasharray="4 3"/>
<text x="22" y="104" class="c sub">时间</text>
<line x1="22" y1="110" x2="22" y2="274" class="arrow"/>
<rect x="411" y="96" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
<text x="421" y="113" class="sub mono" style="font-size:10px;fill:#3F3F3F">BEGIN</text>
<rect x="411" y="132" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
<text x="421" y="149" class="sub mono" style="font-size:10px;fill:#3F3F3F">SELECT id FROM account WHERE balance &gt; 50</text>
<text x="724" y="149" class="e sub mono" style="font-size:10px">→ 1,2</text>
<rect x="46" y="168" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7FB2D6"/>
<text x="56" y="185" class="sub mono" style="font-size:10px;fill:#3F3F3F">INSERT INTO account VALUES (3, 100)</text>
<rect x="411" y="204" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
<text x="421" y="221" class="sub mono" style="font-size:10px;fill:#3F3F3F">SELECT id FROM account WHERE balance &gt; 50</text>
<text x="724" y="221" class="e sub mono note b" style="font-size:10px">→ 1,2,3</text>
<rect x="411" y="240" width="323" height="26" rx="5" fill="#FFFFFF" stroke="#7DBCB7"/>
<text x="421" y="257" class="sub mono" style="font-size:10px;fill:#3F3F3F">COMMIT</text>
<text x="30" y="300" class="sub b" style="fill:#3F3F3F">T1 在 T2 查询的范围内插入了新的一行。</text>
<text x="30" y="322" class="sub" style="">T2 的隔离级别为 REPEATABLE READ 时,第二次查询仍然只有 1、2。</text>
</svg>

After

Width:  |  Height:  |  Size: 3.7 KiB