-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql-02.html
191 lines (87 loc) · 35.9 KB
/
mysql-02.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- iOS Safari -->
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<!-- Chrome, Firefox OS and Opera Status Bar Color -->
<meta name="theme-color" content="#FFFFFF">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.11.1/katex.min.css">
<link rel="stylesheet" type="text/css"
href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.19.0/themes/prism.min.css">
<link rel="stylesheet" type="text/css" href="css/SourceSansPro.css">
<link rel="stylesheet" type="text/css" href="css/theme.css">
<link rel="stylesheet" type="text/css" href="css/notablog.css">
<!-- Favicon -->
<link rel="shortcut icon" href="https://www.notion.so/signed/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2Ffc9b3a94-67d3-4485-bdf3-5e0c0b341ebe%2FAA238E8485C55D168DCF034BC7482B61.png?table=collection&id=c97ea4eb-3d30-4977-8edc-ee98d0f07149">
<style>
:root {
font-size: 20px;
}
</style>
<title>[MySQL 02] SQL更新语句执行流程 | Patrick’s Blog</title>
<meta property="og:type" content="blog">
<meta property="og:title" content="[MySQL 02] SQL更新语句执行流程">
<meta name="description" content="《MySQL实战45讲》02 | 日志系统:一条SQL更新语句是如何执行的?">
<meta property="og:description" content="《MySQL实战45讲》02 | 日志系统:一条SQL更新语句是如何执行的?">
<meta property="og:image" content="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text text-anchor=%22middle%22 dominant-baseline=%22middle%22 x=%2250%22 y=%2255%22 font-size=%2280%22>🌟</text></svg>">
<style>
.DateTagBar {
margin-top: 1.0rem;
}
</style>
</head>
<body>
<nav class="Navbar">
<a href="index.html">
<div class="Navbar__Btn">
<span><img class="inline-img-icon" src="https://www.notion.so/signed/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2Ffc9b3a94-67d3-4485-bdf3-5e0c0b341ebe%2FAA238E8485C55D168DCF034BC7482B61.png?table=collection&id=c97ea4eb-3d30-4977-8edc-ee98d0f07149"></span>
<span>Home</span>
</div>
</a>
<span class="Navbar__Delim">·</span>
<a href="about.html">
<div class="Navbar__Btn">
<span><img class="inline-img-icon" src="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text text-anchor=%22middle%22 dominant-baseline=%22middle%22 x=%2250%22 y=%2255%22 font-size=%2280%22>😀</text></svg>"></span>
<span>About me</span>
</div>
</a>
<span class="Navbar__Delim">·</span>
<a href="categories.html">
<div class="Navbar__Btn">
<span><img class="inline-img-icon" src="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text text-anchor=%22middle%22 dominant-baseline=%22middle%22 x=%2250%22 y=%2255%22 font-size=%2280%22>📃</text></svg>"></span>
<span>Categories</span>
</div>
</a>
</nav>
<header class="Header">
<div class="Header__Spacer Header__Spacer--NoCover">
</div>
<div class="Header__Icon">
<span><img class="inline-img-icon" src="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text text-anchor=%22middle%22 dominant-baseline=%22middle%22 x=%2250%22 y=%2255%22 font-size=%2280%22>🌟</text></svg>"></span>
</div>
<h1 class="Header__Title">[MySQL 02] SQL更新语句执行流程</h1>
<div class="DateTagBar">
<span class="DateTagBar__Item DateTagBar__Date">Posted on Sun, Apr 3, 2022</span>
<span class="DateTagBar__Item DateTagBar__Tag DateTagBar__Tag--gray">
<a href="tag/📖Note.html">📖Note</a>
</span>
<span class="DateTagBar__Item DateTagBar__Tag DateTagBar__Tag--orange">
<a href="tag/Database.html">Database</a>
</span>
</div>
</header>
<article id="https://www.notion.so/af92f3c8cf834c95b470c0aaf4622c9d" class="PageRoot"><div id="https://www.notion.so/d4bd35cc46ab43c4aa1fb2c7bb31553d" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">假设表的创建语句如下,这个表有一个主键ID和一个整型字段c:</span></span></p></div><pre id="https://www.notion.so/3e6f866c03404df9a9ee07eddf9c0e53" class="Code Code--NoWrap"><code><span class="SemanticStringArray"><span class="SemanticString"><span><span class="token keyword">create</span> <span class="token keyword">table</span> T<span class="token punctuation">(</span>ID <span class="token keyword">int</span> <span class="token keyword">primary</span> <span class="token keyword">key</span><span class="token punctuation">,</span> c <span class="token keyword">int</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span></span></span></code></pre><div id="https://www.notion.so/916bafbfb0c64f96b005dc9b673c856f" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">如果要将ID=2这一行的值加1,SQL语句为:</span></span></p></div><pre id="https://www.notion.so/5e279668fcdc4765b30dd1a7e567b96d" class="Code Code--NoWrap"><code><span class="SemanticStringArray"><span class="SemanticString"><span><span class="token keyword">update</span> T <span class="token keyword">set</span> c<span class="token operator">=</span>c<span class="token operator">+</span><span class="token number">1</span> <span class="token keyword">where</span> ID<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">;</span></span></span></span></code></pre><div id="https://www.notion.so/ee06ea6957ad48e1834b7ab34170dbe1" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">查询语句的那一套流程,更新语句也是同样会走一遍。</span></span></p></div><div id="https://www.notion.so/3dd9eb8637b142ce8cd6ddd9679a9a31" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">执行语句前要先连接数据库,这是连接器的工作。</span></span></p></div><div id="https://www.notion.so/63db1357e82045f48cc31f27e1e3501f" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">在一个表上有更新的时候,跟这个表有关的查询缓存会失效,所以这条语句就会把表T上所有缓存结果都清空。这也就是一般不建议使用查询缓存的原因。</span></span></p></div><div id="https://www.notion.so/282e1caee4734955908ffb264bd29241" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">接下来,分析器会通过词法和语法解析知道这是一条更新语句。优化器决定要使用ID这个索引。然后,执行器负责具体执行,找到这一行,然后更新。</span></span></p></div><div id="https://www.notion.so/0082cd42824b424186bd1086baa4788e" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">与查询流程不一样的是,更新流程还涉及两个重要的日志模块:</span><span class="SemanticString"><strong class="SemanticString__Fragment SemanticString__Fragment--Bold">redo log(重做日志)</strong></span><span class="SemanticString">和</span><span class="SemanticString"><strong class="SemanticString__Fragment SemanticString__Fragment--Bold">binlog(归档日志)</strong></span><span class="SemanticString">。</span></span></p></div><h3 id="https://www.notion.so/76cd534d072d406e828e7a9dc12f1531" class="ColorfulBlock ColorfulBlock--ColorDefault Heading Heading--3"><a class="Anchor" href="#https://www.notion.so/76cd534d072d406e828e7a9dc12f1531"><svg width="16" height="16" viewBox="0 0 16 16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a><span class="SemanticStringArray"><span class="SemanticString">重要的日志模块:redo log</span></span></h3><div id="https://www.notion.so/b3d7b69da85f46eab2e2c73630d01004" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">《孔乙己》里,酒店掌柜有一个粉板,专门用来记录客人的赊账记录。如果赊账的人不多,那么他可以把顾客名和账目写在板上。但如果赊账的人多了,粉板总会有记不下的时候,这个时候掌柜一定还有一个专门记录赊账的账本。</span></span></p></div><div id="https://www.notion.so/7c7e3be5e0eb4e5cbb1f8693d63d1df8" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">如果有人要赊账或者还账的话,掌柜一般有两种做法:</span></span></p></div><ul class="BulletedListWrapper"><li id="https://www.notion.so/723f29d70a024665a69262b1dcf88f24" class="BulletedList"><span class="SemanticStringArray"><span class="SemanticString">一种做法是直接把账本翻出来,把这次赊的账加上去或者扣除掉;</span></span></li><li id="https://www.notion.so/a5f4670c9e9a4ecba83418a0f83905a1" class="BulletedList"><span class="SemanticStringArray"><span class="SemanticString">另一种做法是先在粉板上记下这次的账,等打烊以后再把账本翻出来核算。</span></span></li></ul><div id="https://www.notion.so/092c3b25f2664c8082978a81af6b20a8" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">在生意红火柜台很忙时,掌柜一定会选择后者,因为前者操作实在是太麻烦了。首先,你得找到这个人的赊账总额那条记录。密密麻麻几十页,掌柜要找到那个名字,可能还得带上老花镜慢慢找,找到之后再拿出算盘计算,最后再将结果写回到账本上。</span></span></p></div><div id="https://www.notion.so/cdf10d34bcfc4f2897fbe8d61ff6e18d" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">这整个过程想想都麻烦。相比之下,还是先在粉板上记一下方便。如果掌柜没有粉板的帮助,每次记账都得翻账本,效率低得让人难以忍受。</span></span></p></div><div id="https://www.notion.so/9aa13ee1ca714427b0dee3a2f1724c86" class="Divider"></div><div id="https://www.notion.so/f9aae383bacf479eacc460dc02f9e69d" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">同样,在MySQL里也有这个问题,如果每一次的更新操作都需要写进磁盘,然后磁盘也要找到对应的那条记录,然后再更新,整个过程IO成本、查找成本都很高。为了解决这个问题,MySQL的设计者就用了类似酒店掌柜粉板的思路来提升更新效率。</span></span></p></div><div id="https://www.notion.so/87f169922b9843cda96b3c013eb9fb55" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">而粉板和账本配合的整个过程,其实就是MySQL里经常说到的WAL技术,WAL的全称是Write-Ahead Logging(预写式日志),它的关键点就是先写日志,再写磁盘,也就是先写粉板,等不忙的时候再写账本。</span></span></p></div><div id="https://www.notion.so/7156d5c778594de680d0480cabc041e0" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">具体来说,当有一条记录需要更新的时候,InnoDB引擎就会先把记录写到redo log(粉板)里面,并更新内存,这个时候更新就算完成了。同时,InnoDB引擎会在适当的时候,将这个操作记录更新到磁盘里面,而这个更新往往是在系统比较空闲的时候做,这就像打烊以后掌柜做的事。</span></span></p></div><div id="https://www.notion.so/628bad51ef7f4a6ea98a48b13b8556a8" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">如果今天赊账的不多,掌柜可以等打烊后再整理。但如果某天赊账的特别多,粉板写满了,又怎么办呢?这个时候掌柜只好放下手里的活,把粉板中的一部分赊账记录更新到账本中,然后把这些记录从粉板上擦掉,为记新账腾出空间。</span></span></p></div><div id="https://www.notion.so/5cc0d85b02ab470a83bd744defa2bbbd" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">与此类似,InnoDB的redo log是固定大小的,比如可以配置为一组4个文件,每个文件的大小是1GB,那么这块“粉板”总共就可以记录4GB的操作。从头开始写,写到末尾就又回到开头循环写,如下图所示。</span></span></p></div><div id="https://www.notion.so/86c939bbc8334975a46e1cf8c09669f2" class="Image Image--PageWidth"><figure><a href="https://www.notion.so/signed/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F011611b7-acf1-4c75-b342-7fb96792085a%2FUntitled.png?width=1142&table=block&id=86c939bb-c833-4975-a46e-1cf8c09669f2"><img src="https://www.notion.so/signed/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F011611b7-acf1-4c75-b342-7fb96792085a%2FUntitled.png?width=1142&table=block&id=86c939bb-c833-4975-a46e-1cf8c09669f2" style="width:100%"/></a><figcaption><span class="SemanticStringArray"></span></figcaption></figure></div><div id="https://www.notion.so/f15d42a35a4f474588980642b0f5601a" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">write pos是当前记录的位置,一边写一边后移,写到第3号文件末尾后就回到0号文件开头。</span></span></p></div><div id="https://www.notion.so/c73a009b3d6f476c979d2070debcd041" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">checkpoint是当前要擦除的位置,也是往后推移并且循环的,擦除记录前要把记录更新到数据文件。</span></span></p></div><div id="https://www.notion.so/564d707f2daa43e693a7a2f4dcdf577d" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">write pos和checkpoint之间的是“粉板”上还空着的部分,可以用来记录新的操作。如果write pos追上checkpoint,表示“粉板”满了,这时候不能再执行新的更新,得停下来先擦掉一些记录,把checkpoint推进一下。</span></span></p></div><div id="https://www.notion.so/cf17b5ee168a47dabf58a3ba3dcd8c40" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">有了redo log,InnoDB就可以保证即使数据库发生异常重启,之前提交的记录都不会丢失,这个能力称为</span><span class="SemanticString"><strong class="SemanticString__Fragment SemanticString__Fragment--Bold">crash-safe</strong></span><span class="SemanticString">。</span></span></p></div><div id="https://www.notion.so/0e3941a3728c405b9a39aff317e4855f" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">要理解crash-safe这个概念,可以想想前面赊账记录的例子。只要赊账记录记在了粉板上或写在了账本上,之后即使掌柜忘记了,比如突然停业几天,恢复生意后依然可以通过账本和粉板上的数据明确赊账账目。</span></span></p></div><h3 id="https://www.notion.so/f77d1d58b5a34d848d3d97a03277c90a" class="ColorfulBlock ColorfulBlock--ColorDefault Heading Heading--3"><a class="Anchor" href="#https://www.notion.so/f77d1d58b5a34d848d3d97a03277c90a"><svg width="16" height="16" viewBox="0 0 16 16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a><span class="SemanticStringArray"><span class="SemanticString">重要的日志模块:binlog</span></span></h3><div id="https://www.notion.so/d8ae2d56648544c797e4fbd2c80c189b" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">MySQL整体来看,其实就有两块:一块是Server层,它主要做的是MySQL功能层面的事情;还有一块是引擎层,负责存储相关的具体事宜。上面的粉板redo log是InnoDB引擎特有的日志,而Server层也有自己的日志,称为binlog(归档日志)。</span></span></p></div><div id="https://www.notion.so/e34c094e4153410bb88828c9b9388085" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">最开始MySQL里并没有InnoDB引擎。MySQL自带的引擎是MyISAM,但是MyISAM没有crash-safe的能力,binlog日志只能用于归档。而InnoDB是另一个公司以插件形式引入MySQL的,既然只依靠binlog是没有crash-safe能力的,所以InnoDB使用另外一套日志系统——也就是redo log来实现crash-safe能力。</span></span></p></div><div id="https://www.notion.so/215f8a273f4c470a944fa5ba77c98d35" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">这两种日志有以下三点不同:</span></span></p></div><ol class="NumberedListWrapper"><li id="https://www.notion.so/093ade291a7c45298181d659124f4ce4" class="NumberedList" value="1"><span class="SemanticStringArray"><span class="SemanticString">redo log是InnoDB引擎特有的;binlog是MySQL的Server层实现的,所有引擎都可以使用。</span></span></li><li id="https://www.notion.so/67b133f1245c4ce184fd4a38cd7f130c" class="NumberedList" value="2"><span class="SemanticStringArray"><span class="SemanticString">redo log是物理日志,记录的是“在某个数据页上做了什么修改”;binlog是逻辑日志,记录的是这个语句的原始逻辑,比如“给ID=2这一行的c字段加1”。</span></span></li><li id="https://www.notion.so/2b194ee08d664b4695e645b2cd89e461" class="NumberedList" value="3"><span class="SemanticStringArray"><span class="SemanticString">redo log是循环写的,空间固定会用完;binlog是可以追加写入的。“追加写”是指binlog文件写到一定大小后会切换到下一个,并不会覆盖以前的日志。</span></span></li></ol><div id="https://www.notion.so/c4fc942863844953bb89163faaafffd5" class="Divider"></div><div id="https://www.notion.so/c5d02de13a9b44efb4962d41e25f2741" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">有了对这两个日志的概念性理解,再来看执行器和InnoDB引擎在执行这个简单的update语句时的内部流程。</span></span></p></div><ol class="NumberedListWrapper"><li id="https://www.notion.so/91fa94caa52b4e83b44a9af9d792ab70" class="NumberedList" value="1"><span class="SemanticStringArray"><span class="SemanticString">执行器先找引擎取ID=2这一行。ID是主键,引擎直接用树搜索找到这一行。如果ID=2这一行所在的数据页本来就在内存中,就直接返回给执行器;否则,需要先从磁盘读入内存,然后再返回。</span></span></li><li id="https://www.notion.so/25b9e5984d664f67a34d080e5a35b22d" class="NumberedList" value="2"><span class="SemanticStringArray"><span class="SemanticString">执行器拿到引擎给的行数据,把这个值加上1,比如原来是N,现在就是N+1,得到新的一行数据,再调用引擎接口写入这行新数据。</span></span></li><li id="https://www.notion.so/37bbb4a4b6214dbcb0697a7139afa367" class="NumberedList" value="3"><span class="SemanticStringArray"><span class="SemanticString">引擎将这行新数据更新到内存中,同时将这个更新操作记录到redo log里面,此时redo log处于prepare状态。然后告知执行器执行完成了,随时可以提交事务。</span></span></li><li id="https://www.notion.so/6c56054b25274d81ac3e230da2bc4e21" class="NumberedList" value="4"><span class="SemanticStringArray"><span class="SemanticString">执行器生成这个操作的binlog,并把binlog写入磁盘。</span></span></li><li id="https://www.notion.so/2e6b82bb815a421ca4ef481c9ff5d387" class="NumberedList" value="5"><span class="SemanticStringArray"><span class="SemanticString">执行器调用引擎的提交事务接口,引擎把刚刚写入的redo log改成提交(commit)状态,更新完成。</span></span></li></ol><div id="https://www.notion.so/6713f97e205d4f67b7507605568d2a12" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">update语句执行流程图:(图中浅色框表示是在InnoDB内部执行的,深色框表示是在执行器中执行的)</span></span></p></div><div id="https://www.notion.so/28037c9d52814d44af0c72a59db6447f" class="Image Image--PageWidth"><figure><a href="https://www.notion.so/signed/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F4b6f5025-889f-4924-a963-5c65bbecf8b8%2FUntitled.png?width=1142&table=block&id=28037c9d-5281-4d44-af0c-72a59db6447f"><img src="https://www.notion.so/signed/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F4b6f5025-889f-4924-a963-5c65bbecf8b8%2FUntitled.png?width=1142&table=block&id=28037c9d-5281-4d44-af0c-72a59db6447f" style="width:100%"/></a><figcaption><span class="SemanticStringArray"></span></figcaption></figure></div><div id="https://www.notion.so/ba037221bf0145f0b100422fdd1c1364" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">最后三步看上去有点“绕”,将redo log的写入拆成了两个步骤:prepare和commit,这就是“两阶段提交”。</span></span></p></div><h3 id="https://www.notion.so/f960d60e590240009ff44ca3a818a55a" class="ColorfulBlock ColorfulBlock--ColorDefault Heading Heading--3"><a class="Anchor" href="#https://www.notion.so/f960d60e590240009ff44ca3a818a55a"><svg width="16" height="16" viewBox="0 0 16 16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a><span class="SemanticStringArray"><span class="SemanticString">两阶段提交</span></span></h3><div id="https://www.notion.so/4aa3fcfbf69d45f2928427c2d094086c" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">为什么必须有“两阶段提交”呢?这是为了让两份日志之间的逻辑一致。要说明这个问题,得从这个问题说起:怎样让数据库恢复到半个月内任意一秒的状态?</span></span></p></div><div id="https://www.notion.so/c79594d9dc1b4a4f936116f85233b8bc" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">binlog会记录所有的逻辑操作,并且是采用“追加写”的形式。如果DBA承诺说半个月内可以恢复,那么备份系统中一定会保存最近半个月的所有binlog,同时系统会定期做整库备份。这里的“定期”取决于系统的重要性,可以是一天一备,也可以是一周一备。</span></span></p></div><div id="https://www.notion.so/0976a3511b5c44928a1088c9710fd794" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">当需要恢复到指定的某一秒时,比如某天下午两点发现中午十二点有一次误删表,需要找回数据,那可以这么做:</span></span></p></div><ul class="BulletedListWrapper"><li id="https://www.notion.so/fcbe8d84a3e04852b2377e3ed4e239c8" class="BulletedList"><span class="SemanticStringArray"><span class="SemanticString">首先,找到最近的一次全量备份,如果运气好,可能就是昨天晚上的一个备份,从这个备份恢复到临时库;</span></span></li><li id="https://www.notion.so/a2b03c6d8f8d4e609800826ef5236f0a" class="BulletedList"><span class="SemanticStringArray"><span class="SemanticString">然后,从备份的时间点开始,将备份的binlog依次取出来,重放到中午误删表之前的那个时刻。</span></span></li></ul><div id="https://www.notion.so/63c78cd229ea4783b30bad0ac6dc0eee" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">这样临时库就跟误删之前的线上库一样了,然后可以把表数据从临时库取出来,按需要恢复到线上库去。</span></span></p></div><div id="https://www.notion.so/b9caf5c6800e4e008a88c5754df4f7c7" class="Divider"></div><div id="https://www.notion.so/a9ffb3f508274e59862ce9f6d6d36f21" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">说完了数据恢复过程,回来说说为什么日志需要“两阶段提交”。这里不妨用反证法来进行解释。</span></span></p></div><div id="https://www.notion.so/fcc5e50df41f4b15a040ecda6641eeb5" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">由于redo log和binlog是两个独立的逻辑,如果不用两阶段提交,要么就是先写完redo log再写binlog,或者采用反过来的顺序。来看看这两种方式会有什么问题。</span></span></p></div><div id="https://www.notion.so/77b0813b5742485ba01a710a084aa9aa" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">仍然使用前面的update语句来做例子。假设当前ID=2的行,字段c的值是0,再假设执行update语句过程中在写完第一个日志后,第二个日志还没有写完期间发生了crash,会出现什么情况呢?</span></span></p></div><ol class="NumberedListWrapper"><li id="https://www.notion.so/83f3305b9975416db0a7cc21996b76bd" class="NumberedList" value="1"><span class="SemanticStringArray"><span class="SemanticString">先写redo log后写binlog。假设在redo log写完,binlog还没有写完的时候,MySQL进程异常重启。由于前面说过的,redo log写完之后,系统即使崩溃,仍然能够把数据恢复回来,所以恢复后这一行c的值是1。
但是由于binlog没写完就crash了,这时候binlog里面就没有记录这个语句。因此,之后备份日志的时候,存起来的binlog里面就没有这条语句。
然后就会发现,如果需要用这个binlog来恢复临时库的话,由于这个语句的binlog丢失,这个临时库就会少了这一次更新,恢复出来的这一行c的值就是0,与原库的值不同。</span></span></li><li id="https://www.notion.so/d074ac8de9dc44e5875af773bfe1299d" class="NumberedList" value="2"><span class="SemanticStringArray"><span class="SemanticString">先写binlog后写redo log。如果在binlog写完之后crash,由于redo log还没写,崩溃恢复以后这个事务无效,所以这一行c的值是0。但是binlog里面已经记录了“把c从0改成1”这个日志。所以,在之后用binlog来恢复的时候就多了一个事务出来,恢复出来的这一行c的值就是1,与原库的值不同。</span></span></li></ol><div id="https://www.notion.so/471a2da64ec54bc7a806e2f571b9c746" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">可以看到,如果不使用“两阶段提交”,那么数据库的状态就有可能和用它的日志恢复出来的库的状态不一致。</span></span></p></div><div id="https://www.notion.so/33efecf4dda2400d8e2d3d5b550bd062" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">不只是误操作后需要用这个过程来恢复数据。当需要扩容的时候,也就是需要再多搭建一些备库来增加系统的读能力的时候,现在常见的做法也是用全量备份加上应用binlog来实现的,这个“不一致”就会导致线上出现主从数据库不一致的情况。</span></span></p></div><div id="https://www.notion.so/828985a46c464e7ba0b50fea3ba5784d" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">简单说,redo log和binlog都可以用于表示事务的提交状态,而两阶段提交就是让这两个状态保持逻辑上的一致。</span></span></p></div><h3 id="https://www.notion.so/90b09ea066534798969b8a6d4b125e60" class="ColorfulBlock ColorfulBlock--ColorDefault Heading Heading--3"><a class="Anchor" href="#https://www.notion.so/90b09ea066534798969b8a6d4b125e60"><svg width="16" height="16" viewBox="0 0 16 16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a><span class="SemanticStringArray"><span class="SemanticString">小结</span></span></h3><div id="https://www.notion.so/6c8ef5b97ff84d76beba4463927ec27d" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">本节介绍了MySQL里面最重要的两个日志,即物理日志redo log和逻辑日志binlog。</span></span></p></div><div id="https://www.notion.so/094731fba9f344b2bdc37a203ead5dbc" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">redo log用于保证crash-safe能力。</span><span class="SemanticString"><code class="SemanticString__Fragment SemanticString__Fragment--Code">innodb_flush_log_ar_trx_commit</code></span><span class="SemanticString">这个参数设置成1的时候,表示每次事务的redo log都直接持久化到磁盘。这个参数建议设置成1,这样可以保证MySQL异常重启之后数据不丢失。</span></span></p></div><div id="https://www.notion.so/e8a1b336b83646889428930eeb8c6a6a" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString"><code class="SemanticString__Fragment SemanticString__Fragment--Code">sync_binlog</code></span><span class="SemanticString">这个参数设置成1的时候,表示每次事务的binlog都持久化到磁盘。这个参数也建议设置成1,这样可以保证MySQL异常重启之后binlog不丢失。</span></span></p></div><div id="https://www.notion.so/45b4f3253be64de8b826151a349f0406" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">还介绍了与MySQL日志系统密切相关的“两阶段提交”。两阶段提交是跨系统维持数据逻辑一致性时常用的一个方案,即使不做数据库内核开发,日常开发中也有可能会用到。</span></span></p></div><div id="https://www.notion.so/a5cc5db427d346b5bfef2bacc62860b3" class="Divider"></div><div id="https://www.notion.so/c08ffe0eaa4c44dfb4e2da570fdedab6" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">思考题:定期全量备份的周期“取决于系统重要性,有的是一天一备,有的是一周一备”。那么在什么场景下,一天一备会比一周一备更有优势呢?或者说,它影响了数据库系统的哪个指标?</span></span></p></div><div id="https://www.notion.so/594cb2d74382495db4630c2caa9031c7" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">好处是“最长恢复时间”更短。</span></span></p></div><div id="https://www.notion.so/54f37338cce342cea7306a4f51d09d1d" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">在一天一备的模式里,最坏情况下需要应用一天的binlog。比如,每天0点做一次全量备份,而要恢复出一个到昨天晚上23点的备份。</span></span></p></div><div id="https://www.notion.so/1a971fcaef2e4d318309fcaa8c628c40" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">一周一备最坏情况就要应用一周的binlog了。</span></span></p></div><div id="https://www.notion.so/cd712b97d57b4ff18936abc7c02861df" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">系统的对应指标就是RTO(恢复目标时间)。</span></span></p></div><div id="https://www.notion.so/e70980509b5b43d7b3e108e220fd95ec" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">当然这个是有成本的,因为更频繁全量备份需要消耗更多存储空间,所以这个RTO是成本换来的,就需要根据业务重要性来评估了。</span></span></p></div></article>
<footer class="Footer">
<div>© Patrick’s Blog 2024</div>
<div>·</div>
<div>Powered by <a href="https://github.com/dragonman225/notablog" target="_blank"
rel="noopener noreferrer">Notablog</a>.
</div>
</footer>
</body>
</html>