mirror of
https://github.com/CyC2018/Interview-Notebook.git
synced 2026-10-27 05:23:46 +00:00
Update 数据库系统原理.md
重新绘制二级封锁协议图和 ReadView 图:二级封锁协议中 T2 读取后释放 S 锁(unlock-s);ReadView 图按正文的三条可见性规则画出事务 ID 的三个区间。 Closes #1176
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 90 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 68 KiB |
+2
-2
@@ -189,7 +189,7 @@ MySQL 中提供了两种封锁粒度:行级锁以及表级锁。
|
||||
|
||||
可以解决读脏数据问题,因为如果一个事务在对数据 A 进行修改,根据 1 级封锁协议,会加 X 锁,那么就不能再加 S 锁了,也就是不会读入数据。
|
||||
|
||||
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/image-20191207220831843.png"/> </div><br>
|
||||
<div align="center"> <img src="pics/lock-protocol-2.png"/> </div><br>
|
||||
|
||||
**三级封锁协议**
|
||||
|
||||
@@ -291,7 +291,7 @@ INSERT、UPDATE、DELETE 操作会创建一个日志,并将事务版本号 TRX
|
||||
|
||||
MVCC 维护了一个 ReadView 结构,主要包含了创建 ReadView 时系统中未提交的事务列表 TRX_IDs {TRX_ID_1, TRX_ID_2, ...},该列表的最小值 TRX_ID_MIN,以及下一个将要分配的事务 ID TRX_ID_MAX。
|
||||
|
||||
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/image-20191208171445674.png"/> </div><br>
|
||||
<div align="center"> <img src="pics/readview.png"/> </div><br>
|
||||
|
||||
在进行 SELECT 操作时,根据数据行快照的 TRX_ID 与 TRX_ID_MIN 和 TRX_ID_MAX 之间的关系,从而判断数据行快照是否可以使用:
|
||||
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="544" height="477" viewBox="0 0 544 477">
|
||||
<style>
|
||||
text { font-family: Helvetica, Arial, "PingFang SC", "Microsoft YaHei", sans-serif; fill: #666; font-weight: bold; text-anchor: middle; }
|
||||
.title { fill: #777; }
|
||||
.sub { fill: #888; }
|
||||
.note { fill: #C38F8A; }
|
||||
.flow { stroke: #666; stroke-width: 1.3; fill: none; marker-end: url(#arrow); }
|
||||
.wait { stroke-dasharray: 4 3; }
|
||||
.box { fill: #9BC4CB; }
|
||||
.shadow { fill: #B8B8B8; }
|
||||
</style>
|
||||
<defs>
|
||||
<marker id="arrow" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto">
|
||||
<path d="M1,1 L9,5 L1,9" fill="none" stroke="#666" stroke-width="1.6"/>
|
||||
</marker>
|
||||
</defs>
|
||||
<rect width="100%" height="100%" fill="#F5F5F5"/>
|
||||
<text x="272" y="30" font-size="17" class="title">二级封锁协议</text>
|
||||
<text x="272" y="50" font-size="10" class="sub">CyC</text>
|
||||
<text x="223" y="74" font-size="11">T1</text>
|
||||
<text x="340" y="74" font-size="11">T2</text>
|
||||
<!-- T1:加 X 锁修改后回滚 -->
|
||||
<circle cx="224.5" cy="105.5" r="18" class="shadow"/>
|
||||
<circle cx="223" cy="104" r="18" fill="#61646B"/>
|
||||
<line x1="223" y1="122" x2="223" y2="143.5" class="flow"/>
|
||||
<polygon points="185.5,146 260.5,146 263.5,149 263.5,169 188.5,169 185.5,166" class="shadow"/>
|
||||
<polygon points="184,144.5 259,144.5 262,147.5 262,167.5 187,167.5 184,164.5" class="box"/>
|
||||
<text x="223" y="160" font-size="11">lock-x A</text>
|
||||
<line x1="223" y1="167.5" x2="223" y2="177.5" class="flow"/>
|
||||
<polygon points="185.5,180 260.5,180 263.5,183 263.5,203 188.5,203 185.5,200" class="shadow"/>
|
||||
<polygon points="184,178.5 259,178.5 262,181.5 262,201.5 187,201.5 184,198.5" class="box"/>
|
||||
<text x="223" y="194" font-size="11">read A=10</text>
|
||||
<line x1="223" y1="201.5" x2="223" y2="213.5" class="flow"/>
|
||||
<polygon points="185.5,216 260.5,216 263.5,219 263.5,239 188.5,239 185.5,236" class="shadow"/>
|
||||
<polygon points="184,214.5 259,214.5 262,217.5 262,237.5 187,237.5 184,234.5" class="box"/>
|
||||
<text x="223" y="230" font-size="11">write A=20</text>
|
||||
<line x1="223" y1="237.5" x2="223" y2="249.5" class="flow"/>
|
||||
<polygon points="185.5,252 260.5,252 263.5,255 263.5,275 188.5,275 185.5,272" class="shadow"/>
|
||||
<polygon points="184,250.5 259,250.5 262,253.5 262,273.5 187,273.5 184,270.5" class="box"/>
|
||||
<text x="223" y="266" font-size="11">rollback</text>
|
||||
<line x1="223" y1="273.5" x2="223" y2="284.5" class="flow"/>
|
||||
<polygon points="185.5,287 260.5,287 263.5,290 263.5,310 188.5,310 185.5,307" class="shadow"/>
|
||||
<polygon points="184,285.5 259,285.5 262,288.5 262,308.5 187,308.5 184,305.5" class="box"/>
|
||||
<text x="223" y="301" font-size="11">unlock-x A</text>
|
||||
<line x1="223" y1="308.5" x2="223" y2="413" class="flow"/>
|
||||
<circle cx="224.5" cy="434.5" r="18" class="shadow"/>
|
||||
<circle cx="223" cy="433" r="18" fill="#61646B"/>
|
||||
<!-- T2:加 S 锁读取,等待 T1 释放 X 锁 -->
|
||||
<circle cx="341.5" cy="105.5" r="18" class="shadow"/>
|
||||
<circle cx="340" cy="104" r="18" fill="#61646B"/>
|
||||
<line x1="340" y1="122" x2="340" y2="177.5" class="flow"/>
|
||||
<polygon points="302.5,180 377.5,180 380.5,183 380.5,203 305.5,203 302.5,200" class="shadow"/>
|
||||
<polygon points="301,178.5 376,178.5 379,181.5 379,201.5 304,201.5 301,198.5" class="box"/>
|
||||
<text x="340" y="194" font-size="11">lock-s A</text>
|
||||
<line x1="340" y1="201.5" x2="340" y2="310.5" class="flow wait"/>
|
||||
<text x="364" y="259" font-size="10" class="note">wait</text>
|
||||
<polygon points="302.5,313 377.5,313 380.5,316 380.5,336 305.5,336 302.5,333" class="shadow"/>
|
||||
<polygon points="301,311.5 376,311.5 379,314.5 379,334.5 304,334.5 301,331.5" class="box"/>
|
||||
<text x="340" y="327" font-size="11">obtain</text>
|
||||
<line x1="340" y1="334.5" x2="340" y2="343.5" class="flow"/>
|
||||
<polygon points="302.5,346 377.5,346 380.5,349 380.5,369 305.5,369 302.5,366" class="shadow"/>
|
||||
<polygon points="301,344.5 376,344.5 379,347.5 379,367.5 304,367.5 301,364.5" class="box"/>
|
||||
<text x="340" y="360" font-size="11">read A=10</text>
|
||||
<line x1="340" y1="367.5" x2="340" y2="376.5" class="flow"/>
|
||||
<polygon points="302.5,379 377.5,379 380.5,382 380.5,402 305.5,402 302.5,399" class="shadow"/>
|
||||
<polygon points="301,377.5 376,377.5 379,380.5 379,400.5 304,400.5 301,397.5" class="box"/>
|
||||
<text x="340" y="393" font-size="11">unlock-s A</text>
|
||||
<line x1="340" y1="400.5" x2="340" y2="413" class="flow"/>
|
||||
<circle cx="341.5" cy="434.5" r="18" class="shadow"/>
|
||||
<circle cx="340" cy="433" r="18" fill="#61646B"/>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 4.4 KiB |
@@ -0,0 +1,56 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="530" height="262" viewBox="0 0 530 262">
|
||||
<style>
|
||||
text { font-family: Helvetica, Arial, "PingFang SC", "Microsoft YaHei", sans-serif; fill: #666; font-weight: bold; text-anchor: middle; font-size: 11px; }
|
||||
.title { fill: #777; font-size: 17px; }
|
||||
.sub { fill: #888; font-size: 10px; }
|
||||
.label { fill: #C38F8A; }
|
||||
.ok { fill: #6A9F68; }
|
||||
.no { fill: #C0605A; }
|
||||
.box { fill: #9BC4CB; }
|
||||
.shadow { fill: #B8B8B8; }
|
||||
.guide { stroke: #999; stroke-width: 1; stroke-dasharray: 3 3; }
|
||||
</style>
|
||||
<defs>
|
||||
<marker id="arrow" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto">
|
||||
<path d="M1,1 L9,5 L1,9" fill="none" stroke="#666" stroke-width="1.6"/>
|
||||
</marker>
|
||||
</defs>
|
||||
<rect width="100%" height="100%" fill="#F5F5F5"/>
|
||||
<text x="265" y="30" class="title">ReadView</text>
|
||||
<text x="265" y="50" class="sub">CyC</text>
|
||||
|
||||
<!-- 创建 ReadView 时未提交的事务列表 -->
|
||||
<text x="262" y="80" class="label">TRX_IDs(创建 ReadView 时未提交的事务)</text>
|
||||
<rect x="140" y="88" width="244" height="50" rx="9" fill="#C1D8BF" stroke="#666" stroke-dasharray="3 2"/>
|
||||
<rect x="151.5" y="99.5" width="66" height="27" rx="3" class="shadow"/>
|
||||
<rect x="150" y="98" width="66" height="27" rx="3" class="box"/>
|
||||
<text x="183" y="115.5">TRX_ID_1</text>
|
||||
<rect x="230.5" y="99.5" width="66" height="27" rx="3" class="shadow"/>
|
||||
<rect x="229" y="98" width="66" height="27" rx="3" class="box"/>
|
||||
<text x="262" y="115.5">TRX_ID_2</text>
|
||||
<rect x="309.5" y="99.5" width="66" height="27" rx="3" class="shadow"/>
|
||||
<rect x="308" y="98" width="66" height="27" rx="3" class="box"/>
|
||||
<text x="341" y="115.5">TRX_ID_3</text>
|
||||
|
||||
<!-- 下一个将要分配的事务 ID,不在列表中 -->
|
||||
<rect x="405" y="98" width="72" height="27" rx="3" fill="none" stroke="#666" stroke-dasharray="3 2"/>
|
||||
<text x="441" y="115.5" class="label">下一个 ID</text>
|
||||
|
||||
<!-- 事务 ID 数轴,分成三个区间 -->
|
||||
<line x1="183" y1="125" x2="183" y2="170" class="guide"/>
|
||||
<line x1="441" y1="125" x2="441" y2="170" class="guide"/>
|
||||
<line x1="40" y1="170" x2="183" y2="170" stroke="#6A9F68" stroke-width="4"/>
|
||||
<line x1="183" y1="170" x2="441" y2="170" stroke="#D9B44A" stroke-width="4"/>
|
||||
<line x1="441" y1="170" x2="495" y2="170" stroke="#C0605A" stroke-width="4"/>
|
||||
<line x1="490" y1="170" x2="506" y2="170" stroke="#666" stroke-width="1.3" marker-end="url(#arrow)"/>
|
||||
<text x="498" y="160" class="sub">事务 ID</text>
|
||||
<circle cx="183" cy="170" r="3.5" fill="#666"/>
|
||||
<circle cx="441" cy="170" r="3.5" fill="#666"/>
|
||||
<text x="183" y="189" class="label">TRX_ID_MIN</text>
|
||||
<text x="441" y="189" class="label">TRX_ID_MAX</text>
|
||||
|
||||
<text x="111" y="214" class="ok">可以使用</text>
|
||||
<text x="312" y="214">在 TRX_IDs 中则不可使用,</text>
|
||||
<text x="312" y="230">否则可以使用</text>
|
||||
<text x="468" y="214" class="no">不可使用</text>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 2.9 KiB |
Reference in New Issue
Block a user