-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql-01.html
202 lines (87 loc) · 36.5 KB
/
mysql-01.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
192
193
194
195
196
197
198
199
200
201
202
<!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 01] SQL查询语句执行流程 | Patrick’s Blog</title>
<meta property="og:type" content="blog">
<meta property="og:title" content="[MySQL 01] SQL查询语句执行流程">
<meta name="description" content="《MySQL实战45讲》01 | 基础架构:一条SQL查询语句是如何执行的?">
<meta property="og:description" content="《MySQL实战45讲》01 | 基础架构:一条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 01] SQL查询语句执行流程</h1>
<div class="DateTagBar">
<span class="DateTagBar__Item DateTagBar__Date">Posted on Wed, Mar 30, 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/ef46441ce35545beaee795c90385aa4f" class="PageRoot"><div id="https://www.notion.so/47c8ff21c82f461fb2cc34442bf22007" 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/347e61dda20d422ebf1211f6c9949d4b" 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%2F6cadaf9e-2a3f-44b0-afdb-452e25877e4f%2FUntitled.png?width=1920&table=block&id=347e61dd-a20d-422e-bf12-11f6c9949d4b"><img src="https://www.notion.so/signed/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F6cadaf9e-2a3f-44b0-afdb-452e25877e4f%2FUntitled.png?width=1920&table=block&id=347e61dd-a20d-422e-bf12-11f6c9949d4b" style="width:100%"/></a><figcaption><span class="SemanticStringArray"></span></figcaption></figure></div><div id="https://www.notion.so/46d4efcda1aa498fbb2fa2c876e1092f" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">大体来说,MySQL可以分为Server层和存储引擎层两部分。</span></span></p></div><div id="https://www.notion.so/05c94271f21649d08e710f683f23e9da" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">Server层包括连接器、查询缓存、分析器、优化器、执行器等,涵盖MySQL的大多数核心服务功能,以及所有的内置函数(如日期、时间、数学和加密函数等),所有跨存储引擎的功能都在这一层实现,比如存储过程、触发器、视图等。</span></span></p></div><div id="https://www.notion.so/b95b5a2ef94146fb9662b1ff50044a77" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">而存储引擎层负责数据的存储和提取。其架构模式是插件式的,支持InnoDB、MyISAM、Memory等多个存储引擎。现在最常用的存储引擎是InnoDB,它从MySQL 5.5.5版本开始成为了默认存储引擎。</span></span></p></div><div id="https://www.notion.so/a550c952481148d596a4c59c3a365541" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">执行</span><span class="SemanticString"><code class="SemanticString__Fragment SemanticString__Fragment--Code">create table</code></span><span class="SemanticString">建表时,如果不指定引擎类型,默认使用的就是InnoDB。也可以通过指定存储引擎的类型来选择别的引擎,比如在</span><span class="SemanticString"><code class="SemanticString__Fragment SemanticString__Fragment--Code">create table</code></span><span class="SemanticString">语句中使用</span><span class="SemanticString"><code class="SemanticString__Fragment SemanticString__Fragment--Code">engine=memory</code></span><span class="SemanticString">,来指定使用内存引擎创建表。不同存储引擎的表数据存取方式不同,支持的功能也不同。</span></span></p></div><div id="https://www.notion.so/496c72193f3a47ffaf608102be77cd98" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">不同的存储引擎共用一个Server层,也就是从连接器到执行器的部分。</span></span></p></div><div id="https://www.notion.so/ba5ecf2b894744708b64bd4cb62b7a94" class="Divider"></div><div id="https://www.notion.so/9818a470446949cc90cf1819c840897c" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">假设有一个表,表里只有一个ID字段,执行查询语句:</span></span></p></div><pre id="https://www.notion.so/8b5140785fa241e2aaeb6f6417f546cd" class="Code Code--NoWrap"><code><span class="SemanticStringArray"><span class="SemanticString"><span><span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> T <span class="token keyword">where</span> ID<span class="token operator">=</span><span class="token number">10</span><span class="token punctuation">;</span></span></span></span></code></pre><h3 id="https://www.notion.so/aa9e90c4500e440aaab93f35d8538c4f" class="ColorfulBlock ColorfulBlock--ColorDefault Heading Heading--3"><a class="Anchor" href="#https://www.notion.so/aa9e90c4500e440aaab93f35d8538c4f"><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/668fa973e54544a9a8aedb0f106c3b7e" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">第一步,连接到数据库上。连接器负责跟客户端建立连接、获取权限、维持和管理连接。连接命令一般这样写:</span></span></p></div><pre id="https://www.notion.so/3b98c1c4c4224d6bbac0aa21502b5886" class="Code Code--NoWrap"><code><span class="SemanticStringArray"><span class="SemanticString"><span>mysql -h$ip -P$port -u$user -p</span></span></span></code></pre><div id="https://www.notion.so/41f608d08a864be4bbf350d88d06f73b" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">输完命令之后,需要在交互对话中输入密码。虽然密码也可以直接在</span><span class="SemanticString"><code class="SemanticString__Fragment SemanticString__Fragment--Code">-p</code></span><span class="SemanticString">后面写在命令行中,但这样可能会导致密码泄露。</span></span></p></div><div id="https://www.notion.so/1847d9239ad04fb2984c233ccd9d8cf1" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">连接命令中的mysql是客户端工具,用来跟服务端建立连接。在完成TCP握手后,连接器就要开始认证身份,这个时候用的就是输入的用户名和密码。</span></span></p></div><ul class="BulletedListWrapper"><li id="https://www.notion.so/11c02c15b33942c68627db11eb4cab83" class="BulletedList"><span class="SemanticStringArray"><span class="SemanticString">如果用户名或密码不对,就会收到一个”Access denied for user”的错误,然后客户端程序结束执行。</span></span></li><li id="https://www.notion.so/77e59ae02f9446a5910a3b87519e56be" class="BulletedList"><span class="SemanticStringArray"><span class="SemanticString">如果用户名密码认证通过,连接器会到权限表里面查出拥有的权限。之后,这个连接里面的权限判断逻辑,都将依赖于此时读到的权限。</span></span></li></ul><div id="https://www.notion.so/11fdabf70b994a4eb7abd76a8c4b9a68" 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/12e180a5b54d488faf55f34ae618235d" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">连接完成后,如果没有后续的动作,这个连接就处于空闲状态,可以在</span><span class="SemanticString"><code class="SemanticString__Fragment SemanticString__Fragment--Code">show processlist</code></span><span class="SemanticString">命令中看到它。其中的Command列显示为”Sleep”的这一行,就表示现在系统里面有一个空闲连接。</span></span></p></div><div id="https://www.notion.so/e3c54a7f51d7419fb217b4854faf024f" 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%2F8e47adc1-596e-41bd-9d5c-f8eb6f2854eb%2FUntitled.png?width=875&table=block&id=e3c54a7f-51d7-419f-b217-b4854faf024f"><img src="https://www.notion.so/signed/https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fsecure.notion-static.com%2F8e47adc1-596e-41bd-9d5c-f8eb6f2854eb%2FUntitled.png?width=875&table=block&id=e3c54a7f-51d7-419f-b217-b4854faf024f" style="width:100%"/></a><figcaption><span class="SemanticStringArray"></span></figcaption></figure></div><div id="https://www.notion.so/e02fd7ef1a8f41cb960288b0e59e3eb7" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">客户端如果太长时间没动静,连接器就会自动将它断开。这个时间是由参数</span><span class="SemanticString"><code class="SemanticString__Fragment SemanticString__Fragment--Code">wait_timeout</code></span><span class="SemanticString">控制的,默认值是8小时。</span></span></p></div><div id="https://www.notion.so/d0f8d3d59bf346dcac2c4a436299f966" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">如果在连接被断开之后,客户端再次发送请求的话,就会收到一个错误提醒:Lost connection to MySQL server during query。这时候如果要继续,就需要重连,然后再执行请求了。</span></span></p></div><div id="https://www.notion.so/40d6ddfb2b354899853a303b7c4590eb" 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/b6a60184e1dc43939e3e146bd5ac6239" 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/dcd260b2c4bc41b1a08d4fcd2c3c832c" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">但是全部使用长连接后,有些时候MySQL占用内存涨得特别快,这是因为MySQL在执行过程中临时使用的内存是管理在连接对象里面的。这些资源会在连接断开的时候才释放。所以如果长连接累积下来,可能导致内存占用太大,被系统强行杀掉(OOM),从现象看就是MySQL异常重启了。</span></span></p></div><div id="https://www.notion.so/722b2fe14bf54dfbb443137fd37ed33b" 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/34ea1fa740414484800ccd3770e008e2" class="NumberedList" value="1"><span class="SemanticStringArray"><span class="SemanticString">定期断开长连接。使用一段时间后,或者程序里面判断执行过一个占用内存的大查询后,断开连接,之后要查询再重连。</span></span></li><li id="https://www.notion.so/6a1b11c812834fd496976d1007cdd7d4" class="NumberedList" value="2"><span class="SemanticStringArray"><span class="SemanticString">MySQL 5.7或更新版本可以在每次执行一个比较大的操作后,通过执行</span><span class="SemanticString"><code class="SemanticString__Fragment SemanticString__Fragment--Code">mysql_reset_connection</code></span><span class="SemanticString">来重新初始化连接资源。这个过程不需要重连和重新做权限验证,但是会将连接恢复到刚刚创建完时的状态。</span></span></li></ol><h3 id="https://www.notion.so/be8fc444768c4e1084750190792ff8c6" class="ColorfulBlock ColorfulBlock--ColorDefault Heading Heading--3"><a class="Anchor" href="#https://www.notion.so/be8fc444768c4e1084750190792ff8c6"><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/2b8c57d5523c4b51b466a2e2bc3698b2" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">连接建立完成后,就可以执行</span><span class="SemanticString"><code class="SemanticString__Fragment SemanticString__Fragment--Code">select</code></span><span class="SemanticString">语句了。执行逻辑就会来到第二步:查询缓存。</span></span></p></div><div id="https://www.notion.so/907e4ae69ee747b3a553dc5565cdded1" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">MySQL拿到一个查询请求后,会先到查询缓存看看,之前是不是执行过这条语句。之前执行过的语句及其结果可能会以key-value对的形式,被直接缓存在内存中。key是查询的语句,value是查询的结果。如果查询能够直接在这个缓存中找到key,那么这个value就会被直接返回给客户端。</span></span></p></div><div id="https://www.notion.so/164880635bc849e6a5ed2a6f2d7b923c" 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/bf544150313c4054a2d8126de2fb6853" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString"><strong class="SemanticString__Fragment SemanticString__Fragment--Bold">但是大多数情况下不要使用查询缓存,查询缓存往往弊大于利。</strong></span></span></p></div><div id="https://www.notion.so/1db2ccb63ff2415994a2beda13b7039c" 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/4d637dbaff7c4981bfca2d38a78389b3" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">MySQL提供了这种“按需使用”的方式。可以将参数</span><span class="SemanticString"><code class="SemanticString__Fragment SemanticString__Fragment--Code">query_cache_type</code></span><span class="SemanticString">设置成</span><span class="SemanticString"><code class="SemanticString__Fragment SemanticString__Fragment--Code">DEMAND</code></span><span class="SemanticString">,这样对于默认的SQL语句都不使用查询缓存。而对于确定要使用查询缓存的语句,可以用</span><span class="SemanticString"><code class="SemanticString__Fragment SemanticString__Fragment--Code">SQL_CACHE</code></span><span class="SemanticString">显式指定,像下面这个语句一样:</span></span></p></div><pre id="https://www.notion.so/e7c04fac20ac481caf9327d0ea05ff3d" class="Code Code--NoWrap"><code><span class="SemanticStringArray"><span class="SemanticString"><span><span class="token keyword">select</span> SQL_CACHE <span class="token operator">*</span> <span class="token keyword">from</span> T <span class="token keyword">where</span> ID<span class="token operator">=</span><span class="token number">10</span><span class="token punctuation">;</span></span></span></span></code></pre><div id="https://www.notion.so/751defca37ea41f3b32e71e953e66704" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">MySQL 8.0版本直接将查询缓存的整块功能删掉了,8.0开始彻底没有这个功能了。</span></span></p></div><h3 id="https://www.notion.so/90170133180a42c882fd89fd0a996e2e" class="ColorfulBlock ColorfulBlock--ColorDefault Heading Heading--3"><a class="Anchor" href="#https://www.notion.so/90170133180a42c882fd89fd0a996e2e"><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/35ae231c160e4f0c91363109e77d6258" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">如果没有命中查询缓存,就要开始真正执行语句了。首先,MySQL需要知道要做什么,因此需要对SQL语句做解析。</span></span></p></div><div id="https://www.notion.so/a860a6321f364ad89219d9e92a2ffe3a" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">分析器先会做“词法分析”。输入的是由多个字符串和空格组成的一条SQL语句,MySQL需要识别出里面的字符串分别是什么,代表什么。</span></span></p></div><div id="https://www.notion.so/e6a12c169e8242e5b139ddaf196988a1" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">MySQL从输入把”select”这个关键字识别出来,这是一个查询语句。它也要把字符串”T”识别成“表名T”,把字符串”ID”识别成“列ID”。</span></span></p></div><div id="https://www.notion.so/1b72a01778f14d06b898ab8fe32dc983" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">做完了这些识别以后,就要做“语法分析”。根据词法分析的结果,语法分析器会根据语法规则,判断输入的这个SQL语句是否满足MySQL语法。</span></span></p></div><div id="https://www.notion.so/240356c412f94bcc89cc83de61face88" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">如果语句不对,就会收到”You have an error in your SQL syntax”的错误提醒,比如下面的语句select少打了开头的字母”s”。</span></span></p></div><pre id="https://www.notion.so/383498934c784c5a98b6fb9a06a02b4b" class="Code Code--NoWrap"><code><span class="SemanticStringArray"><span class="SemanticString"><span>elect <span class="token operator">*</span> <span class="token keyword">from</span> t <span class="token keyword">where</span> ID<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>
ERROR <span class="token number">1064</span> <span class="token punctuation">(</span><span class="token number">42000</span><span class="token punctuation">)</span>: You have an error <span class="token operator">in</span> your <span class="token keyword">SQL</span> syntax<span class="token punctuation">;</span> <span class="token keyword">check</span> the manual that corresponds <span class="token keyword">to</span> your MySQL server version <span class="token keyword">for</span> the <span class="token keyword">right</span> syntax <span class="token keyword">to</span> <span class="token keyword">use</span> near <span class="token string">'elect * from t where ID=1'</span> at line <span class="token number">1</span></span></span></span></code></pre><div id="https://www.notion.so/856c9f4f135740dfbc59107763cd2012" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">一般语法错误会提示第一个出现错误的位置,所以要关注的是紧接”user near”的内容。</span></span></p></div><h3 id="https://www.notion.so/7476d2a196d44edeae6f4b79849b2ca3" class="ColorfulBlock ColorfulBlock--ColorDefault Heading Heading--3"><a class="Anchor" href="#https://www.notion.so/7476d2a196d44edeae6f4b79849b2ca3"><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/a519540ed364429f89f12b58dde70147" 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/92a8061dcb074bf1baae8f7ed303cc43" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">优化器是在表里面有多个索引的时候,决定使用哪个索引;或者在一个语句有多表关联(join)的时候,决定各个表的连接顺序。比如执行下面这样的语句,这个语句是执行两个表的join:</span></span></p></div><pre id="https://www.notion.so/6681a1c5b73344989cf2a906e27436ef" class="Code Code--NoWrap"><code><span class="SemanticStringArray"><span class="SemanticString"><span><span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> t1 <span class="token keyword">join</span> t2 <span class="token keyword">using</span><span class="token punctuation">(</span>ID<span class="token punctuation">)</span> <span class="token keyword">where</span> t1<span class="token punctuation">.</span>c<span class="token operator">=</span><span class="token number">10</span> <span class="token operator">and</span> t2<span class="token punctuation">.</span>d<span class="token operator">=</span><span class="token number">20</span><span class="token punctuation">;</span></span></span></span></code></pre><ul class="BulletedListWrapper"><li id="https://www.notion.so/39e5480b250c4e91a393168367fe3203" class="BulletedList"><span class="SemanticStringArray"><span class="SemanticString">既可以先从表t1里面取出c=10的记录的ID值,再根据ID值关联到表t2,再判断t2里面d的值是否等于20。</span></span></li><li id="https://www.notion.so/e9024e6c4c9740cabc81920eb1371838" class="BulletedList"><span class="SemanticStringArray"><span class="SemanticString">也可以先从表t2里面取出d=20的记录的ID值,再根据ID值关联到表t1,再判断t1里面c的值是否等于10。</span></span></li></ul><div id="https://www.notion.so/a2514d651c044c9f9ca9ad80a6d384c0" 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/f413215f73e74495b9c13ce0e91c321a" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">优化器阶段完成后,这个语句的执行方案就确定下来了,然后进入执行器阶段。</span></span></p></div><h3 id="https://www.notion.so/b95cf312066749deaa16125c60f73c3a" class="ColorfulBlock ColorfulBlock--ColorDefault Heading Heading--3"><a class="Anchor" href="#https://www.notion.so/b95cf312066749deaa16125c60f73c3a"><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/217d0f53df004c46a0bbdddcf6eb1f25" 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/196338a544434fdbb1e08115a202ab50" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">开始执行的时候,要先判断一下对这个表T有没有执行查询的权限,如果没有,就会返回没有权限的错误,如下所示。</span></span></p></div><pre id="https://www.notion.so/98f7e59dee0b4cc099284f0487df709c" class="Code Code--NoWrap"><code><span class="SemanticStringArray"><span class="SemanticString"><span><span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> T <span class="token keyword">where</span> ID<span class="token operator">=</span><span class="token number">10</span><span class="token punctuation">;</span>
ERROR <span class="token number">1142</span> <span class="token punctuation">(</span><span class="token number">42000</span><span class="token punctuation">)</span>: <span class="token keyword">SELECT</span> command denied <span class="token keyword">to</span> <span class="token keyword">user</span> <span class="token string">'b'</span><span class="token variable">@'localhost'</span> <span class="token keyword">for</span> <span class="token keyword">table</span> <span class="token string">'T'</span></span></span></span></code></pre><div id="https://www.notion.so/882a59ed5da5402a9b513845e5f6c7c2" 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/cc77bc58c246481eaa53b25fc552bd38" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">比如这个例子中的表T中,ID字段没有索引,那么执行器的执行流程是这样的:</span></span></p></div><ol class="NumberedListWrapper"><li id="https://www.notion.so/ab0b29f7071a4ac7afaf30616abc0fc0" class="NumberedList" value="1"><span class="SemanticStringArray"><span class="SemanticString">调用InnoDB引擎接口取这个表的第一行,判断ID值是不是10,如果不是则跳过,如果是则将这行存在结果集中;</span></span></li><li id="https://www.notion.so/b183e4ad9d744b55af010045ba83e52c" class="NumberedList" value="2"><span class="SemanticStringArray"><span class="SemanticString">调用引擎接口取“下一行”,重复相同的判断逻辑,直到取到这个表的最后一行。</span></span></li><li id="https://www.notion.so/9ad7f5f194c34fa3b1b4db697fc6eece" class="NumberedList" value="3"><span class="SemanticStringArray"><span class="SemanticString">执行器将上述遍历过程中所有满足条件的行组成的记录集作为结果返回给客户端。</span></span></li></ol><div id="https://www.notion.so/0992ab40f0314a1ebac77e80d2fb60d7" 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/097841badc654de3ba0e63cb6faeb9ab" 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/30b0211ca3ad4893aec0f036ebabbb59" class="ColorfulBlock ColorfulBlock--ColorDefault Text"><p class="Text__Content"><span class="SemanticStringArray"><span class="SemanticString">可以在数据库的慢查询日志中看到一个</span><span class="SemanticString"><code class="SemanticString__Fragment SemanticString__Fragment--Code">rows_examined</code></span><span class="SemanticString">的字段,表示这个语句执行过程中扫描了多少行。这个值就是在执行器每次调用引擎获取数据行的时候累加的。</span></span></p></div><div id="https://www.notion.so/526d6cd6cbd84ff48b98f86fea139258" 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">引擎扫描行数跟</strong></span><span class="SemanticString"><strong class="SemanticString__Fragment SemanticString__Fragment--Bold"><code class="SemanticString__Fragment SemanticString__Fragment--Code">rows_examined</code></strong></span><span class="SemanticString"><strong class="SemanticString__Fragment SemanticString__Fragment--Bold">并不是完全相同的</strong></span><span class="SemanticString">。</span></span></p></div></article>
<footer class="Footer">
<div>© Patrick’s Blog 2025</div>
<div>·</div>
<div>Powered by <a href="/~https://github.com/dragonman225/notablog" target="_blank"
rel="noopener noreferrer">Notablog</a>.
</div>
</footer>
</body>
</html>