[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"article-detail-ssr-43787587676012555":3},{"id":4,"title":5,"tag":6,"createTime":7,"updateTime":8,"renderedHtml":9,"description":10,"ogImageUrl":11},"43787587676012555","MySQL逻辑架构","MySQL","2023-10-30","2024-06-14","\u003Ch2 id=\"h2-0-\">逻辑架构剖析\u003C\u002Fh2>\n\u003Cimg class=\"article-img article-img-zoomable\" src=\"https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131639848.png\" alt=\"image-20231013163930689\" style=\"zoom:50%;\" \u002F loading=\"lazy\" decoding=\"async\" srcset=\"https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131639848.png?imageMogr2\u002Fthumbnail\u002F400x\u002Fformat\u002Fwebp\u002Fquality\u002F85 400w, https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131639848.png?imageMogr2\u002Fthumbnail\u002F800x\u002Fformat\u002Fwebp\u002Fquality\u002F85 800w, https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131639848.png?imageMogr2\u002Fthumbnail\u002F1200x\u002Fformat\u002Fwebp\u002Fquality\u002F85 1200w, https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131639848.png?imageMogr2\u002Fthumbnail\u002F1920x\u002Fformat\u002Fwebp\u002Fquality\u002F85 1920w\" sizes=\"(max-width: 576px) 100vw, (max-width: 992px) 90vw, 720px\">\n\u003Ch3 id=\"h3-1-1-\">1. 连接层\u003C\u002Fh3>\n\u003Cp>系统（客户端）访问 MySQL 服务器前，做的第一件事就是建立 TCP 连接。经过三次握手建立连接成功后， MySQL 服务器对 TCP 传输过来的账号密码做身份认证、权限获取。\u003C\u002Fp>\n\u003Cul>\n\u003Cli>用户名或密码不对，会收到一个 Access denied for user 错误，客户端程序结束执行。\u003C\u002Fli>\n\u003Cli>用户名密码认证通过，会从权限表查出账号拥有的权限与连接关联，之后的权限判断逻辑，都将依赖于此时读到的权限。\u003C\u002Fli>\n\u003C\u002Ful>\n\u003Cp>TCP 连接收到请求后，必须要分配给一个线程专门与这个客户端的交互。但是当并发连接数增加时，线程数量也会相应增加，这会导致大量的系统资源被消耗在线程的创建和销毁上，而不是真正的数据库处理任务上。为了解决这些问题，MySQL 引入了线程池。线程池通过预创建一定数量的线程，并复用这些线程来处理客户端的请求，从而避免了频繁创建和销毁线程所带来的资源消耗，减少了线程数量，降低了上下文切换和锁竞争的开销，提高了系统的性能。\u003C\u002Fp>\n\u003Ch3 id=\"h3-2-2-\">2. 服务层\u003C\u002Fh3>\n\u003Cul>\n\u003Cli>\n\u003Cp>SQL Interface：SQL 接口\u003C\u002Fp>\n\u003Cul>\n\u003Cli>\u003Ccode>接收用户的SQL命令，并且返回用户需要查询的结果\u003C\u002Fcode>。比如 SELECT … FROM 就是调用 SQLInterface。\u003C\u002Fli>\n\u003Cli>MySQL 支持 DML（数据操作语言）、DDL（数据定义语言）、存储过程、视图、触发器、自定义函数等多种 SQL 语言接口。\u003C\u002Fli>\n\u003C\u002Ful>\n\u003C\u002Fli>\n\u003Cli>\n\u003Cp>Parser：解析器\u003C\u002Fp>\n\u003Cul>\n\u003Cli>在解析器中\u003Ccode>对 SQL 语句进行语法分析、语义分析\u003C\u002Fcode>。将 SQL 语句分解成数据结构，并将这个结构传递到后续步骤，以后 SQL 语句的传递和处理就是基于这个结构的。如果在分解构成中遇到错误，那么就说明这个 SQL 语句是不合理的。\u003C\u002Fli>\n\u003Cli>在 SQL 命令传递到解析器的时候会被解析器验证和解析，并为其创建语法树，并根据数据字典丰富查询语法树，会验证该客户端是否具有执行该查询的权限 。创建好语法树后，MySQL 还会对 SQL 查询进行语法上的优化，进行查询重写。\u003C\u002Fli>\n\u003C\u002Ful>\n\u003C\u002Fli>\n\u003Cli>\n\u003Cp>Optimizer：查询优化器\u003C\u002Fp>\n\u003Cul>\n\u003Cli>\n\u003Cp>SQL 语句在语法解析之后、查询之前会使用查询优化器确定 SQL 语句的执行路径，生成一个执行计划。\u003C\u002Fp>\n\u003C\u002Fli>\n\u003Cli>\n\u003Cp>这个执行计划表明应该使用哪些索引进行查询（全表检索还是使用索引检索），表之间的连接顺序如何，最后会按照执行计划中的步骤调用存储引擎提供的方法来真正的执行查询，并将查询结果返回给用户。\u003C\u002Fp>\n\u003C\u002Fli>\n\u003Cli>\n\u003Cp>它使用“选取-投影-连接”策略进行查询。例如：\u003C\u002Fp>\n\u003Cdiv class=\"ssr-code-block\" data-lang=\"sql\">\u003Cdiv class=\"ssr-code-head\">\u003Cspan class=\"ssr-code-lang\">sql\u003C\u002Fspan>\u003Cdiv class=\"ssr-code-actions\">\u003Cbutton class=\"ssr-code-fullscreen ssr-code-iconbtn\" type=\"button\" aria-label=\"全屏查看代码\" title=\"全屏\">\u003Csvg viewBox=\"0 0 24 24\" width=\"17\" height=\"17\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M5 5h6v2H7v4H5V5zm14 0v6h-2V7h-4V5h6zM5 19v-6h2v4h4v2H5zm14 0h-6v-2h4v-4h2v6z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003Cbutton class=\"ssr-code-copy ssr-code-iconbtn\" type=\"button\" aria-label=\"复制代码\" title=\"复制\">\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--default\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--success\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--error\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"ssr-code-body\">\u003Cdiv class=\"ssr-code-pane\">\u003Cdiv class=\"ssr-code-gutter\" aria-hidden=\"true\">\u003Cspan class=\"ssr-code-num\">1\u003C\u002Fspan>\u003C\u002Fdiv>\u003Cpre class=\"ssr-code-pre\">\u003Ccode class=\"hljs language-sql\">\u003Cspan class=\"ssr-code-line\">\u003Cspan style=\"color:#F97583\">SELECT\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> id,\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">name\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">FROM\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> student \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">WHERE\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> gender \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">=\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> \u003C\u002Fspan>\u003Cspan style=\"color:#9ECBFF\">&#39;男&#39;\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">;\u003C\u002Fspan>\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fpre>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>\n\u003Cp>这个 SELECT 查询先根据 WHERE 语句进行选取，而不是将表全部查询出来以后再进行 gender 过滤。 这个 SELECT 查询先根据 id 和 name 进行属性投影，而不是将属性全部取出以后再进行过滤，将这两个查询条件连接起来生成最终查询结果。\u003C\u002Fp>\n\u003C\u002Fli>\n\u003C\u002Ful>\n\u003C\u002Fli>\n\u003Cli>\n\u003Cp>Caches &amp; Buffers：查询缓存组件（MySQL5.7 之前有，在 MySQL8.0 中被删除）\u003C\u002Fp>\n\u003Cp>MySQL 内部维持着一些 Cache 和 Buffer，比如 Query Cache 用来缓存一条 SELECT 语句的执行结果，如果能够在其中找到对应的查询结果，那么就不必再进行查询解析、优化和执行的整个过程了，直接将结果反馈给客户端。这个缓存机制是由一系列小缓存组成的。比如表缓存，记录缓存，key 缓存，权限缓存等 。这个查询缓存可以在 不同客户端之间共享 。\u003C\u002Fp>\n\u003C\u002Fli>\n\u003C\u002Ful>\n\u003Ch3 id=\"h3-3-3-\">3. 引擎层\u003C\u002Fh3>\n\u003Cp>插件式存储引擎层（ Storage Engines），真正的负责了 MySQL 中数据的存储和提取，对物理服务器级别维护的底层数据执行操作，服务器通过 API 与存储引擎进行通信。不同的存储引擎具有的功能不同，这样我们可以根据自己的实际需要进行选取。\u003C\u002Fp>\n\u003Cp>MySQL 8.0.34 支持的存储引擎如下：\u003C\u002Fp>\n\u003Cdiv class=\"ssr-code-block\" data-lang=\"mysql\">\u003Cdiv class=\"ssr-code-head\">\u003Cspan class=\"ssr-code-lang\">mysql\u003C\u002Fspan>\u003Cdiv class=\"ssr-code-actions\">\u003Cbutton class=\"ssr-code-fullscreen ssr-code-iconbtn\" type=\"button\" aria-label=\"全屏查看代码\" title=\"全屏\">\u003Csvg viewBox=\"0 0 24 24\" width=\"17\" height=\"17\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M5 5h6v2H7v4H5V5zm14 0v6h-2V7h-4V5h6zM5 19v-6h2v4h4v2H5zm14 0h-6v-2h4v-4h2v6z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003Cbutton class=\"ssr-code-copy ssr-code-iconbtn\" type=\"button\" aria-label=\"复制代码\" title=\"复制\">\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--default\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--success\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--error\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"ssr-code-body\">\u003Cdiv class=\"ssr-code-pane\">\u003Cdiv class=\"ssr-code-gutter\" aria-hidden=\"true\">\u003Cspan class=\"ssr-code-num\">1\u003C\u002Fspan>\u003C\u002Fdiv>\u003Cpre class=\"ssr-code-pre\">\u003Ccode class=\"hljs language-mysql\">\u003Cspan class=\"ssr-code-line\">show engines;\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fpre>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>\n\u003Cimg class=\"article-img article-img-zoomable\" src=\"https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131717966.png\" alt=\"image-20231013171700895\"  \u002F loading=\"lazy\" decoding=\"async\" srcset=\"https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131717966.png?imageMogr2\u002Fthumbnail\u002F400x\u002Fformat\u002Fwebp\u002Fquality\u002F85 400w, https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131717966.png?imageMogr2\u002Fthumbnail\u002F800x\u002Fformat\u002Fwebp\u002Fquality\u002F85 800w, https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131717966.png?imageMogr2\u002Fthumbnail\u002F1200x\u002Fformat\u002Fwebp\u002Fquality\u002F85 1200w, https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131717966.png?imageMogr2\u002Fthumbnail\u002F1920x\u002Fformat\u002Fwebp\u002Fquality\u002F85 1920w\" sizes=\"(max-width: 576px) 100vw, (max-width: 992px) 90vw, 720px\">\n\u003Ch3 id=\"h3-4-4-\">4. 存储层\u003C\u002Fh3>\n\u003Cp>所有的数据，数据库、表的定义，表的每一行的内容，索引，都是存在文件系统上，以文件的方式存在的，并完成与存储引擎的交互。当然有些存储引擎比如 InnoDB，也支持不使用文件系统直接管理裸设备，但现代文件系统的实现使得这样做没有必要了。在文件系统之下，可以使用本地磁盘，可以使用 DAS、NAS、SAN 等各种存储系统。\u003C\u002Fp>\n\u003Ch2 id=\"h2-5-sql-\">SQL 执行流程\u003C\u002Fh2>\n\u003Ch3 id=\"h3-6-1-sql-\">1. SQL 执行流程（理论）\u003C\u002Fh3>\n\u003Cp>下图是 MySQL 中的 SQL 执行流程：\u003C\u002Fp>\n\u003Cimg class=\"article-img article-img-zoomable\" src=\"https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131746099.png\" alt=\"image-20231013174637019\" style=\"zoom:67%;\" \u002F loading=\"lazy\" decoding=\"async\" srcset=\"https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131746099.png?imageMogr2\u002Fthumbnail\u002F400x\u002Fformat\u002Fwebp\u002Fquality\u002F85 400w, https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131746099.png?imageMogr2\u002Fthumbnail\u002F800x\u002Fformat\u002Fwebp\u002Fquality\u002F85 800w, https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131746099.png?imageMogr2\u002Fthumbnail\u002F1200x\u002Fformat\u002Fwebp\u002Fquality\u002F85 1200w, https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131746099.png?imageMogr2\u002Fthumbnail\u002F1920x\u002Fformat\u002Fwebp\u002Fquality\u002F85 1920w\" sizes=\"(max-width: 576px) 100vw, (max-width: 992px) 90vw, 720px\">\n\u003Col>\n\u003Cli>\n\u003Cp>查询缓存：Server 如果在查询缓存中发现了这条 SQL 语句，就会直接将结果返回给客户端；如果没有，就进入到解析器阶段。需要说明的是，因为查询缓存往往效率不高，所以\u003Ccode>在MySQL8.0之后就抛弃了这个功能\u003C\u002Fcode>。\u003Ccode>大多数情况查询缓存就是个鸡肋，为什么呢？\u003C\u002Fcode>\u003C\u002Fp>\n\u003Cul>\n\u003Cli>如果两个查询请求在任何字符上的不同（例如：空格、注释、大小写），都会导致缓存不会命中。这是 MySQL 的查询缓存命中率非常低的第一个原因 。\u003C\u002Fli>\n\u003Cli>如果查询请求中包含某些系统函数、用户自定义变量和函数、一些系统表，如 mysql 、information_schema、 performance_schema 数据库中的表，那这个请求就不会被缓存。为什么不会被缓存？举个例子：比如系统函数：\u003Ccode>NOW\u003C\u002Fcode>，每次调用都会产生最新的当前时间，如果在一个查询请求中调用了这个函数，那即使查询请求的文本信息都一样，那不同时间的两次查询也应该得到不同的结果，如果在第一次查询时就缓存了，那第二次查询的时候直接使用第一次查询的结果就是错误的。这是 MySQL 的查询缓存命中率非常低的第二个原因 。\u003C\u002Fli>\n\u003Cli>既然是缓存，那就有它缓存失效的时候 。MySQL 的缓存系统会监测涉及到的每张表，只要该表的结构或者数据被修改，如对该表使用了\u003Ccode>INSERT\u003C\u002Fcode>、\u003Ccode>UPDATE\u003C\u002Fcode>、\u003Ccode>DELETE\u003C\u002Fcode>、\u003Ccode>TRUNCATE TABLE\u003C\u002Fcode>、\u003Ccode>ALTER TABLE\u003C\u002Fcode>、\u003Ccode>DROP TABLE\u003C\u002Fcode>或\u003Ccode>DROP DATABASE\u003C\u002Fcode>语句，那使用该表的所有高速缓存查询都将变为无效并从高速缓存中删除。所以对于更新压力大的数据库来说，查询缓存的命中率会非常低。这是 MySQL 的查询缓存命中率非常低的第三个原因 。\u003C\u002Fli>\n\u003C\u002Ful>\n\u003C\u002Fli>\n\u003Cli>\n\u003Cp>解析器：在解析器中对 SQL 语句进行语法分析、语义分析。解析器先做“ 词法分析 ”。你输入的是由多个字符串和空格组成的一条 SQL 语句，MySQL 需要识别出里面的字符串分别是什么，代表什么。 MySQL 从你输入的&quot;select&quot;这个关键字识别出来，这是一个查询语句。它也要把字符串“T”识别成“表名 T”，把字符串“ID”识别成“列 ID”。接着，要做“ 语法分析 ”。根据词法分析的结果，语法分析器（比如：Bison）会根据语法规则，判断你输入的这个 SQL 语句是否 满足 MySQL 语法 。如果 SQL 语句正确，则会生成一个这样的语法树：\u003C\u002Fp>\n\u003C\u002Fli>\n\u003C\u002Fol>\n\u003Cimg class=\"article-img article-img-zoomable\" src=\"https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131802711.png\" alt=\"image-20231013180227653\" style=\"zoom:67%;\" \u002F loading=\"lazy\" decoding=\"async\" srcset=\"https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131802711.png?imageMogr2\u002Fthumbnail\u002F400x\u002Fformat\u002Fwebp\u002Fquality\u002F85 400w, https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131802711.png?imageMogr2\u002Fthumbnail\u002F800x\u002Fformat\u002Fwebp\u002Fquality\u002F85 800w, https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131802711.png?imageMogr2\u002Fthumbnail\u002F1200x\u002Fformat\u002Fwebp\u002Fquality\u002F85 1200w, https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131802711.png?imageMogr2\u002Fthumbnail\u002F1920x\u002Fformat\u002Fwebp\u002Fquality\u002F85 1920w\" sizes=\"(max-width: 576px) 100vw, (max-width: 992px) 90vw, 720px\">\n\u003Col start=\"3\">\n\u003Cli>\n\u003Cp>优化器：在优化器中会确定 SQL 语句的执行路径，比如是根据全表检索，还是根据索引检索等。举例：如下语句是执行两个表的 join：\u003C\u002Fp>\n\u003Cdiv class=\"ssr-code-block\" data-lang=\"mysql\">\u003Cdiv class=\"ssr-code-head\">\u003Cspan class=\"ssr-code-lang\">mysql\u003C\u002Fspan>\u003Cdiv class=\"ssr-code-actions\">\u003Cbutton class=\"ssr-code-fullscreen ssr-code-iconbtn\" type=\"button\" aria-label=\"全屏查看代码\" title=\"全屏\">\u003Csvg viewBox=\"0 0 24 24\" width=\"17\" height=\"17\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M5 5h6v2H7v4H5V5zm14 0v6h-2V7h-4V5h6zM5 19v-6h2v4h4v2H5zm14 0h-6v-2h4v-4h2v6z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003Cbutton class=\"ssr-code-copy ssr-code-iconbtn\" type=\"button\" aria-label=\"复制代码\" title=\"复制\">\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--default\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--success\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--error\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"ssr-code-body\">\u003Cdiv class=\"ssr-code-pane\">\u003Cdiv class=\"ssr-code-gutter\" aria-hidden=\"true\">\u003Cspan class=\"ssr-code-num\">1\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">2\u003C\u002Fspan>\u003C\u002Fdiv>\u003Cpre class=\"ssr-code-pre\">\u003Ccode class=\"hljs language-mysql\">\u003Cspan class=\"ssr-code-line\">select * from test1 join test2 using(ID)\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">where test1.name=&#39;李华&#39; and test2.name=&#39;mysql进阶&#39;;\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fpre>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>\n\u003Cul>\n\u003Cli>\n\u003Cp>方案 1：可以先从表 test1 里面取出 name=‘李华’ 的记录的 ID 值，再根据 ID 值关联到表 test2，再判断 test2 里面 name 的值是否等于 ‘mysql 进阶’。\u003C\u002Fp>\n\u003C\u002Fli>\n\u003Cli>\n\u003Cp>方案 2：可以先从表 test2 里面取出 name=‘mysql 进阶’ 的记录的 ID 值，再根据 ID 值关联到 test1，再判断 test1 里面 name 的值是否等于 李华。\u003C\u002Fp>\n\u003C\u002Fli>\n\u003C\u002Ful>\n\u003Cp>这两种执行方法的逻辑结果是一样的，但是执行的效率会有不同，而优化器的作用就是决定选择使用哪一个方案。优化器阶段完成后，这个语句的执行方案就确定下来了，然后进入执行器阶段。\u003C\u002Fp>\n\u003Cp>在查询优化器中，可以分为\u003Ccode>逻辑查询\u003C\u002Fcode>优化阶段和\u003Ccode>物理查询\u003C\u002Fcode>优化阶段。\u003C\u002Fp>\n\u003C\u002Fli>\n\u003Cli>\n\u003Cp>执行器：截止到现在，还没有真正去读写真实的表，仅仅只是产出了一个执行计划。于是就进入了执行器阶段 。在执行之前需要判断该用户是否具备权限 。如果没有，就会返回权限错误。如果具备权限，就执行 SQL 查询并返回结果。在 MySQL8.0 以下的版本，如果设置了查询缓存，这时会将查询结果进行缓存。至此，这个语句就执行完成了。\u003C\u002Fp>\n\u003C\u002Fli>\n\u003C\u002Fol>\n\u003Cp>\u003Cimg class=\"article-img article-img-zoomable\" src=\"https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131810668.png\" alt=\"image-20231013181042608\" loading=\"lazy\" decoding=\"async\" style=\"width: 100%; height: auto; max-width: 100%; display: block;\" srcset=\"https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131810668.png?imageMogr2\u002Fthumbnail\u002F400x\u002Fformat\u002Fwebp\u002Fquality\u002F85 400w, https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131810668.png?imageMogr2\u002Fthumbnail\u002F800x\u002Fformat\u002Fwebp\u002Fquality\u002F85 800w, https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131810668.png?imageMogr2\u002Fthumbnail\u002F1200x\u002Fformat\u002Fwebp\u002Fquality\u002F85 1200w, https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131810668.png?imageMogr2\u002Fthumbnail\u002F1920x\u002Fformat\u002Fwebp\u002Fquality\u002F85 1920w\" sizes=\"(max-width: 576px) 100vw, (max-width: 992px) 90vw, 720px\">\u003C\u002Fp>\n\u003Ch3 id=\"h3-7-2-sql-\">2. SQL 执行流程（实践）\u003C\u002Fh3>\n\u003Cp>在 MySQL 中，当设置\u003Ccode>@@profiling\u003C\u002Fcode>为 1 时，表示开启了性能分析功能。启用性能分析后，MySQL 会自动记录每个查询的执行时间、查询计划、扫描行数等信息，可以通过查询 SHOW PROFILES 来查看性能分析的结果。这对于识别和优化慢查询非常有用。\u003C\u002Fp>\n\u003Ch4 id=\"h4-8--profiling-\">确认 profiling 是否开启\u003C\u002Fh4>\n\u003Cdiv class=\"ssr-code-block\" data-lang=\"mysql\">\u003Cdiv class=\"ssr-code-head\">\u003Cspan class=\"ssr-code-lang\">mysql\u003C\u002Fspan>\u003Cdiv class=\"ssr-code-actions\">\u003Cbutton class=\"ssr-code-fullscreen ssr-code-iconbtn\" type=\"button\" aria-label=\"全屏查看代码\" title=\"全屏\">\u003Csvg viewBox=\"0 0 24 24\" width=\"17\" height=\"17\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M5 5h6v2H7v4H5V5zm14 0v6h-2V7h-4V5h6zM5 19v-6h2v4h4v2H5zm14 0h-6v-2h4v-4h2v6z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003Cbutton class=\"ssr-code-copy ssr-code-iconbtn\" type=\"button\" aria-label=\"复制代码\" title=\"复制\">\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--default\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--success\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--error\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"ssr-code-body\">\u003Cdiv class=\"ssr-code-pane\">\u003Cdiv class=\"ssr-code-gutter\" aria-hidden=\"true\">\u003Cspan class=\"ssr-code-num\">1\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">2\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">3\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">4\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">5\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">6\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">7\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">8\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">9\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">10\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">11\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">12\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">13\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">14\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">15\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">16\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">17\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">18\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">19\u003C\u002Fspan>\u003C\u002Fdiv>\u003Cpre class=\"ssr-code-pre\">\u003Ccode class=\"hljs language-mysql\">\u003Cspan class=\"ssr-code-line\">mysql&gt; select @@profiling;\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">+-------------+\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| @@profiling |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">+-------------+\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">|           0 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">+-------------+\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">1 row in set, 1 warning (0.00 sec)\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">​\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">mysql&gt; set profiling=1;\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">Query OK, 0 rows affected, 1 warning (0.00 sec)\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">​\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">mysql&gt; select @@profiling;\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">+-------------+\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| @@profiling |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">+-------------+\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">|           1 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">+-------------+\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">1 row in set, 1 warning (0.00 sec)\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">​\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fpre>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>\n\u003Ch4 id=\"h4-9--sql-\">多次执行相同 SQL 查询\u003C\u002Fh4>\n\u003Cdiv class=\"ssr-code-block\" data-lang=\"mysql\">\u003Cdiv class=\"ssr-code-head\">\u003Cspan class=\"ssr-code-lang\">mysql\u003C\u002Fspan>\u003Cdiv class=\"ssr-code-actions\">\u003Cbutton class=\"ssr-code-fullscreen ssr-code-iconbtn\" type=\"button\" aria-label=\"全屏查看代码\" title=\"全屏\">\u003Csvg viewBox=\"0 0 24 24\" width=\"17\" height=\"17\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M5 5h6v2H7v4H5V5zm14 0v6h-2V7h-4V5h6zM5 19v-6h2v4h4v2H5zm14 0h-6v-2h4v-4h2v6z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003Cbutton class=\"ssr-code-copy ssr-code-iconbtn\" type=\"button\" aria-label=\"复制代码\" title=\"复制\">\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--default\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--success\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--error\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"ssr-code-body\">\u003Cdiv class=\"ssr-code-pane\">\u003Cdiv class=\"ssr-code-gutter\" aria-hidden=\"true\">\u003Cspan class=\"ssr-code-num\">1\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">2\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">3\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">4\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">5\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">6\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">7\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">8\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">9\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">10\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">11\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">12\u003C\u002Fspan>\u003C\u002Fdiv>\u003Cpre class=\"ssr-code-pre\">\u003Ccode class=\"hljs language-mysql\">\u003Cspan class=\"ssr-code-line\">mysql&gt; select * from student;\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">+----+------+-------+\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| id | name | class |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">+----+------+-------+\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">|  1 | 张三 | 一班  |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">|  3 | 李四 | 一班  |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">|  8 | 王五 | 二班  |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">+----+------+-------+\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">3 rows in set (0.00 sec)\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">​\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">## 重复上面的命令\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">    。。。。。。\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fpre>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>\n\u003Ch4 id=\"h4-10--profiles\">查看 profiles\u003C\u002Fh4>\n\u003Cp>查看当前会话所产生的所有 profiles：\u003C\u002Fp>\n\u003Cdiv class=\"ssr-code-block\" data-lang=\"mysql\">\u003Cdiv class=\"ssr-code-head\">\u003Cspan class=\"ssr-code-lang\">mysql\u003C\u002Fspan>\u003Cdiv class=\"ssr-code-actions\">\u003Cbutton class=\"ssr-code-fullscreen ssr-code-iconbtn\" type=\"button\" aria-label=\"全屏查看代码\" title=\"全屏\">\u003Csvg viewBox=\"0 0 24 24\" width=\"17\" height=\"17\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M5 5h6v2H7v4H5V5zm14 0v6h-2V7h-4V5h6zM5 19v-6h2v4h4v2H5zm14 0h-6v-2h4v-4h2v6z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003Cbutton class=\"ssr-code-copy ssr-code-iconbtn\" type=\"button\" aria-label=\"复制代码\" title=\"复制\">\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--default\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--success\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--error\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"ssr-code-body\">\u003Cdiv class=\"ssr-code-pane\">\u003Cdiv class=\"ssr-code-gutter\" aria-hidden=\"true\">\u003Cspan class=\"ssr-code-num\">1\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">2\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">3\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">4\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">5\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">6\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">7\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">8\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">9\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">10\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">11\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">12\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">13\u003C\u002Fspan>\u003C\u002Fdiv>\u003Cpre class=\"ssr-code-pre\">\u003Ccode class=\"hljs language-mysql\">\u003Cspan class=\"ssr-code-line\">mysql&gt; show profiles;\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">+----------+------------+-----------------------+\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| Query_ID | Duration   | Query                 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">+----------+------------+-----------------------+\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">|        1 | 0.00029200 | select @@profiling    |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">|        2 | 0.00076275 | select * from student |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">|        3 | 0.00037500 | select * from student |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">|        4 | 0.00027150 | select * from student |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">|        5 | 0.00037725 | select * from student |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">|        6 | 0.00023375 | select * from student |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">|        7 | 0.00037700 | select * from student |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">+----------+------------+-----------------------+\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">7 rows in set, 1 warning (0.00 sec)\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fpre>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>\n\u003Ch4 id=\"h4-11--profile\">查看 profile\u003C\u002Fh4>\n\u003Cp>显示执行计划，查看程序的执行步骤：\u003C\u002Fp>\n\u003Cdiv class=\"ssr-code-block\" data-lang=\"mysql\">\u003Cdiv class=\"ssr-code-head\">\u003Cspan class=\"ssr-code-lang\">mysql\u003C\u002Fspan>\u003Cdiv class=\"ssr-code-actions\">\u003Cbutton class=\"ssr-code-fullscreen ssr-code-iconbtn\" type=\"button\" aria-label=\"全屏查看代码\" title=\"全屏\">\u003Csvg viewBox=\"0 0 24 24\" width=\"17\" height=\"17\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M5 5h6v2H7v4H5V5zm14 0v6h-2V7h-4V5h6zM5 19v-6h2v4h4v2H5zm14 0h-6v-2h4v-4h2v6z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003Cbutton class=\"ssr-code-copy ssr-code-iconbtn\" type=\"button\" aria-label=\"复制代码\" title=\"复制\">\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--default\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--success\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--error\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"ssr-code-body\">\u003Cdiv class=\"ssr-code-pane\">\u003Cdiv class=\"ssr-code-gutter\" aria-hidden=\"true\">\u003Cspan class=\"ssr-code-num\">1\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">2\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">3\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">4\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">5\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">6\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">7\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">8\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">9\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">10\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">11\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">12\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">13\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">14\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">15\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">16\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">17\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">18\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">19\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">20\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">21\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">22\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">23\u003C\u002Fspan>\u003C\u002Fdiv>\u003Cpre class=\"ssr-code-pre\">\u003Ccode class=\"hljs language-mysql\">\u003Cspan class=\"ssr-code-line\">mysql&gt; show profile;\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">+--------------------------------+----------+\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| Status                         | Duration |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">+--------------------------------+----------+\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| starting                       | 0.000085 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| Executing hook on transaction  | 0.000005 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| starting                       | 0.000008 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| checking permissions           | 0.000007 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| Opening tables                 | 0.000040 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| init                           | 0.000006 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| System lock                    | 0.000009 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| optimizing                     | 0.000005 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| statistics                     | 0.000016 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| preparing                      | 0.000019 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| executing                      | 0.000058 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| end                            | 0.000005 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| query end                      | 0.000003 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| waiting for handler commit     | 0.000011 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| closing tables                 | 0.000010 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| freeing items                  | 0.000080 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| cleaning up                    | 0.000012 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">+--------------------------------+----------+\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">17 rows in set, 1 warning (0.00 sec)\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fpre>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>\n\u003Col>\n\u003Cli>\u003Ccode>starting\u003C\u002Fcode>: 这是查询开始的阶段，包括了初始化各种资源并准备执行查询的时间。\u003C\u002Fli>\n\u003Cli>\u003Ccode>Executing hook on transaction\u003C\u002Fcode>: 这行表示执行事务钩子的时间。\u003C\u002Fli>\n\u003Cli>\u003Ccode>starting\u003C\u002Fcode>: 这个阶段再次出现，可能是因为一些重置或重新开始的操作。\u003C\u002Fli>\n\u003Cli>\u003Ccode>checking permissions\u003C\u002Fcode>: 这是检查执行查询的用户权限的阶段。\u003C\u002Fli>\n\u003Cli>\u003Ccode>Opening tables\u003C\u002Fcode>: 这是打开需要访问的表的阶段。在这个阶段，MySQL 将锁定这些表以进行操作。\u003C\u002Fli>\n\u003Cli>\u003Ccode>init\u003C\u002Fcode>: 这是初始化查询的阶段，包括了各种准备工作。\u003C\u002Fli>\n\u003Cli>\u003Ccode>System lock\u003C\u002Fcode>: 这个阶段是数据库系统对表或者行进行锁定的时间，以防止其他查询同时修改同一数据。\u003C\u002Fli>\n\u003Cli>\u003Ccode>optimizing\u003C\u002Fcode>: 这是优化查询的阶段。在这个阶段，MySQL 会尽量优化查询，例如通过决定最佳的表连接顺序或者决定是否使用索引。\u003C\u002Fli>\n\u003Cli>\u003Ccode>statistics\u003C\u002Fcode>: 这是收集和使用统计信息以帮助优化查询计划的阶段。\u003C\u002Fli>\n\u003Cli>\u003Ccode>preparing\u003C\u002Fcode>: 这是准备查询执行的阶段，包括了从优化器接收和设置查询执行计划的时间。\u003C\u002Fli>\n\u003Cli>\u003Ccode>executing\u003C\u002Fcode>: 这是实际执行查询的阶段。\u003C\u002Fli>\n\u003Cli>\u003Ccode>end\u003C\u002Fcode>: 查询执行结束的阶段。\u003C\u002Fli>\n\u003Cli>\u003Ccode>query end\u003C\u002Fcode>: 这是完成查询并开始清理的阶段。\u003C\u002Fli>\n\u003Cli>\u003Ccode>waiting for handler commit\u003C\u002Fcode>: 这是等待存储引擎提交所有数据更改的阶段。\u003C\u002Fli>\n\u003Cli>\u003Ccode>closing tables\u003C\u002Fcode>: 这是关闭所有打开的表的阶段。\u003C\u002Fli>\n\u003Cli>\u003Ccode>freeing items\u003C\u002Fcode>: 这是释放查询使用的所有内存空间的阶段。\u003C\u002Fli>\n\u003Cli>\u003Ccode>cleaning up\u003C\u002Fcode>: 这是清理查询产生的所有剩余资源的阶段。\u003C\u002Fli>\n\u003C\u002Fol>\n\u003Cp>不指定查询 ID 时，默认显示的是当前会话中最后一个执行的查询的 Profiling 信息。当然也可以查询指定的 Query ID，比如：\u003C\u002Fp>\n\u003Cdiv class=\"ssr-code-block\" data-lang=\"mysql\">\u003Cdiv class=\"ssr-code-head\">\u003Cspan class=\"ssr-code-lang\">mysql\u003C\u002Fspan>\u003Cdiv class=\"ssr-code-actions\">\u003Cbutton class=\"ssr-code-fullscreen ssr-code-iconbtn\" type=\"button\" aria-label=\"全屏查看代码\" title=\"全屏\">\u003Csvg viewBox=\"0 0 24 24\" width=\"17\" height=\"17\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M5 5h6v2H7v4H5V5zm14 0v6h-2V7h-4V5h6zM5 19v-6h2v4h4v2H5zm14 0h-6v-2h4v-4h2v6z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003Cbutton class=\"ssr-code-copy ssr-code-iconbtn\" type=\"button\" aria-label=\"复制代码\" title=\"复制\">\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--default\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--success\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--error\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"ssr-code-body\">\u003Cdiv class=\"ssr-code-pane\">\u003Cdiv class=\"ssr-code-gutter\" aria-hidden=\"true\">\u003Cspan class=\"ssr-code-num\">1\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">2\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">3\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">4\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">5\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">6\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">7\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">8\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">9\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">10\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">11\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">12\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">13\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">14\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">15\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">16\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">17\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">18\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">19\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">20\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">21\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">22\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">23\u003C\u002Fspan>\u003C\u002Fdiv>\u003Cpre class=\"ssr-code-pre\">\u003Ccode class=\"hljs language-mysql\">\u003Cspan class=\"ssr-code-line\">mysql&gt; show profile for query 2;\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">+--------------------------------+----------+\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| Status                         | Duration |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">+--------------------------------+----------+\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| starting                       | 0.000092 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| Executing hook on transaction  | 0.000005 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| starting                       | 0.000009 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| checking permissions           | 0.000006 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| Opening tables                 | 0.000344 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| init                           | 0.000009 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| System lock                    | 0.000009 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| optimizing                     | 0.000005 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| statistics                     | 0.000022 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| preparing                      | 0.000023 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| executing                      | 0.000092 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| end                            | 0.000005 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| query end                      | 0.000003 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| waiting for handler commit     | 0.000014 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| closing tables                 | 0.000016 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| freeing items                  | 0.000089 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">| cleaning up                    | 0.000020 |\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">+--------------------------------+----------+\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">17 rows in set, 1 warning (0.00 sec)\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fpre>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>\n\u003Cp>这是第一次执行 select * from student; 这个 SQL 语句的时候的性能分析，可以看到第一次执行这个语句在大部分阶段要比第二次执行这个 SQL 语句慢。此外，还可以查询更丰富的内容：\u003C\u002Fp>\n\u003Cdiv class=\"ssr-code-block\" data-lang=\"mysql\">\u003Cdiv class=\"ssr-code-head\">\u003Cspan class=\"ssr-code-lang\">mysql\u003C\u002Fspan>\u003Cdiv class=\"ssr-code-actions\">\u003Cbutton class=\"ssr-code-fullscreen ssr-code-iconbtn\" type=\"button\" aria-label=\"全屏查看代码\" title=\"全屏\">\u003Csvg viewBox=\"0 0 24 24\" width=\"17\" height=\"17\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M5 5h6v2H7v4H5V5zm14 0v6h-2V7h-4V5h6zM5 19v-6h2v4h4v2H5zm14 0h-6v-2h4v-4h2v6z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003Cbutton class=\"ssr-code-copy ssr-code-iconbtn\" type=\"button\" aria-label=\"复制代码\" title=\"复制\">\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--default\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--success\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--error\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"ssr-code-body\">\u003Cdiv class=\"ssr-code-pane\">\u003Cdiv class=\"ssr-code-gutter\" aria-hidden=\"true\">\u003Cspan class=\"ssr-code-num\">1\u003C\u002Fspan>\u003C\u002Fdiv>\u003Cpre class=\"ssr-code-pre\">\u003Ccode class=\"hljs language-mysql\">\u003Cspan class=\"ssr-code-line\">show profile cpu,block io for query 4;\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fpre>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>\n\u003Cp>使用这个语句可以额外查询到：\u003C\u002Fp>\n\u003Col>\n\u003Cli>\u003Ccode>CPU_user\u003C\u002Fcode>：查询消耗的 CPU 时间（用户模式）。\u003C\u002Fli>\n\u003Cli>\u003Ccode>CPU_system\u003C\u002Fcode>：查询消耗的 CPU 时间（系统模式）。\u003C\u002Fli>\n\u003Cli>\u003Ccode>Block_ops_in\u003C\u002Fcode>：输入块操作的数量。\u003C\u002Fli>\n\u003Cli>\u003Ccode>Block_ops_out\u003C\u002Fcode>：输出块操作的数量。\u003C\u002Fli>\n\u003C\u002Fol>\n\u003Ch3 id=\"h3-12-3-sql-\">3. SQL 语法顺序\u003C\u002Fh3>\n\u003Cul>\n\u003Cli>\n\u003Cp>随着 Mysql 版本的更新换代，其优化器也在不断的升级。\u003C\u002Fp>\n\u003C\u002Fli>\n\u003Cli>\n\u003Cp>SQL 优化器可能会根据各种因素改变您编写的 SQL 语句的执行顺序，以提供更好的性能。优化器会考虑表的大小、索引的存在、JOIN 的顺序，以及其他因素来决定如何执行查询。\u003C\u002Fp>\n\u003C\u002Fli>\n\u003Cli>\n\u003Cp>例如，假设你有一个查询，它将两个表进行 JOIN 操作，一个表有 10 行，另一个表有 10000 行。优化器可能会决定先扫描小表，然后再扫描大表，这样可能会更高效。\u003C\u002Fp>\n\u003C\u002Fli>\n\u003C\u002Ful>\n\u003Cp>因此，尽管你可能按照特定的顺序编写 SQL 语句，但实际执行的顺序可能会根据优化器的决定而变化。\u003C\u002Fp>\n\u003Ch2 id=\"h2-13-buffer-pool\">数据库缓冲池(buffer pool)\u003C\u002Fh2>\n\u003Ch3 id=\"h3-14-1-\">1. 什么是缓冲池\u003C\u002Fh3>\n\u003Cp>首先我们需要了解在 InnoDB 存储引擎中，缓冲池都包括了哪些。在 InnoDB 存储引擎中有一部分数据会放到内存中，缓冲池则占了这部分内存的大部分，它用来存储各种数据的缓存，如下图所示：\u003C\u002Fp>\n\u003Cimg class=\"article-img article-img-zoomable\" src=\"https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131924818.png\" alt=\"image-20231013192446769\" style=\"zoom: 50%;\" \u002F loading=\"lazy\" decoding=\"async\" srcset=\"https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131924818.png?imageMogr2\u002Fthumbnail\u002F400x\u002Fformat\u002Fwebp\u002Fquality\u002F85 400w, https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131924818.png?imageMogr2\u002Fthumbnail\u002F800x\u002Fformat\u002Fwebp\u002Fquality\u002F85 800w, https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131924818.png?imageMogr2\u002Fthumbnail\u002F1200x\u002Fformat\u002Fwebp\u002Fquality\u002F85 1200w, https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131924818.png?imageMogr2\u002Fthumbnail\u002F1920x\u002Fformat\u002Fwebp\u002Fquality\u002F85 1920w\" sizes=\"(max-width: 576px) 100vw, (max-width: 992px) 90vw, 720px\">\n\u003Cp>从图中，你能看到 InnoDB 缓冲池包括了数据页、索引页、插入缓冲、锁信息、自适应 Hash 和数据字典信息等。\u003C\u002Fp>\n\u003Cp>缓存池的重要性：\u003C\u002Fp>\n\u003Col>\n\u003Cli>\n\u003Cp>缓存原则：\u003C\u002Fp>\n\u003Cp>“\u003Ccode>位置 * 频次\u003C\u002Fcode>”这个原则，可以帮我们对 I\u002FO 访问效率进行优化。\u003C\u002Fp>\n\u003Cp>首先，位置决定效率，提供缓冲池就是为了在内存中可以直接访问数据。其次，频次决定优先级顺序。因为缓冲池的大小是有限的，比如磁盘有 200G，但是内存只有 16G，缓冲池大小只有 1G，就无法将所有数据都加载到缓冲池里，这时就涉及到优先级顺序，所以要\u003Ccode>优先对使用频次高的热数据进行加载\u003C\u002Fcode> 。\u003C\u002Fp>\n\u003C\u002Fli>\n\u003Cli>\n\u003Cp>缓冲池的预读特性：\u003C\u002Fp>\n\u003Cp>了解了缓冲池的作用之后，我们还需要了解缓冲池的另一个特性:预读。\u003C\u002Fp>\n\u003Cp>缓冲池的作用就是提升 I\u002FO 效率，而我们进行读取数据的时候存在一个“局部性原理”，也就是说我们使用了一些数据，大概率还会使用它周围的一些数据，因此采用“预读”的机制提前加载，可以减少未来可能的磁盘 I\u002FО 操作。\u003C\u002Fp>\n\u003C\u002Fli>\n\u003C\u002Fol>\n\u003Ch3 id=\"h3-15-2-\">2. 缓冲池如何读取数据\u003C\u002Fh3>\n\u003Cp>缓冲池管理器会尽量将经常使用的数据保存起来，在数据库进行页面读操作的时候，首先会判断该页面是否在缓冲池中，如果存在就直接读取，如果不存在，就会通过内存或磁盘将页面存放到缓冲池中再进行读取。\u003C\u002Fp>\n\u003Cp>缓存在数据库中的结构和作用如下图所示：\u003C\u002Fp>\n\u003Cimg class=\"article-img article-img-zoomable\" src=\"https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131932025.png\" alt=\"image-20231013193204942\" style=\"zoom:67%;\" \u002F loading=\"lazy\" decoding=\"async\" srcset=\"https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131932025.png?imageMogr2\u002Fthumbnail\u002F400x\u002Fformat\u002Fwebp\u002Fquality\u002F85 400w, https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131932025.png?imageMogr2\u002Fthumbnail\u002F800x\u002Fformat\u002Fwebp\u002Fquality\u002F85 800w, https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131932025.png?imageMogr2\u002Fthumbnail\u002F1200x\u002Fformat\u002Fwebp\u002Fquality\u002F85 1200w, https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131932025.png?imageMogr2\u002Fthumbnail\u002F1920x\u002Fformat\u002Fwebp\u002Fquality\u002F85 1920w\" sizes=\"(max-width: 576px) 100vw, (max-width: 992px) 90vw, 720px\">\n\u003Cp>\u003Ccode>如果我们执行 SQL 语句的时候更新了缓存池中的数据，那么这些数据会马上同步到磁盘上吗？\u003C\u002Fcode>\u003C\u002Fp>\n\u003Cblockquote>\n\u003Col>\n\u003Cli>\n\u003Cp>实际上，当我们对数据库中的记录进行修改的时候，首先会修改缓冲池中页里面的记录信息，然后数据库会以一定的频率刷新到磁盘上。注意并不是每次发生更新操作，都会立刻进行磁盘回写。缓冲池会采用一种叫做 checkpoint 的机制将数据回写到磁盘上，这样做的好处就是提升了数据库的整体性能。\u003C\u002Fp>\n\u003C\u002Fli>\n\u003Cli>\n\u003Cp>比如，当缓冲池不够用时，需要释放掉一些不常用的页，此时就可以强行采用 checkpoint 的方式，将不常用的脏页回写到磁盘上，然后再从缓冲池中将这些页释放掉。这里脏页(dirty page)指的是缓冲池中被修改过的页，与磁盘上的数据页不一致。\u003C\u002Fp>\n\u003C\u002Fli>\n\u003C\u002Fol>\n\u003C\u002Fblockquote>\n\u003Ch3 id=\"h3-16-3-\">3. 查看\u002F设置缓冲池的大小\u003C\u002Fh3>\n\u003Cp>如果使用的是 InnoDB 存储引擎，可以通过查看 innodb_buffer_pool_size 变量来查看缓冲池的大小。命令如下：\u003C\u002Fp>\n\u003Cdiv class=\"ssr-code-block\" data-lang=\"mysql\">\u003Cdiv class=\"ssr-code-head\">\u003Cspan class=\"ssr-code-lang\">mysql\u003C\u002Fspan>\u003Cdiv class=\"ssr-code-actions\">\u003Cbutton class=\"ssr-code-fullscreen ssr-code-iconbtn\" type=\"button\" aria-label=\"全屏查看代码\" title=\"全屏\">\u003Csvg viewBox=\"0 0 24 24\" width=\"17\" height=\"17\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M5 5h6v2H7v4H5V5zm14 0v6h-2V7h-4V5h6zM5 19v-6h2v4h4v2H5zm14 0h-6v-2h4v-4h2v6z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003Cbutton class=\"ssr-code-copy ssr-code-iconbtn\" type=\"button\" aria-label=\"复制代码\" title=\"复制\">\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--default\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--success\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--error\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"ssr-code-body\">\u003Cdiv class=\"ssr-code-pane\">\u003Cdiv class=\"ssr-code-gutter\" aria-hidden=\"true\">\u003Cspan class=\"ssr-code-num\">1\u003C\u002Fspan>\u003C\u002Fdiv>\u003Cpre class=\"ssr-code-pre\">\u003Ccode class=\"hljs language-mysql\">\u003Cspan class=\"ssr-code-line\">show variables like &#39;innodb_buffer_pool_size&#39;;\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fpre>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>\n\u003Cp>你能看到此时 InnoDB 的缓冲池大小只有 134217728B\u002F1024\u002F1024=128MB。我们可以修改缓冲池大小，比如改为 256MB，方法如下：\u003C\u002Fp>\n\u003Cdiv class=\"ssr-code-block\" data-lang=\"mysql\">\u003Cdiv class=\"ssr-code-head\">\u003Cspan class=\"ssr-code-lang\">mysql\u003C\u002Fspan>\u003Cdiv class=\"ssr-code-actions\">\u003Cbutton class=\"ssr-code-fullscreen ssr-code-iconbtn\" type=\"button\" aria-label=\"全屏查看代码\" title=\"全屏\">\u003Csvg viewBox=\"0 0 24 24\" width=\"17\" height=\"17\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M5 5h6v2H7v4H5V5zm14 0v6h-2V7h-4V5h6zM5 19v-6h2v4h4v2H5zm14 0h-6v-2h4v-4h2v6z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003Cbutton class=\"ssr-code-copy ssr-code-iconbtn\" type=\"button\" aria-label=\"复制代码\" title=\"复制\">\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--default\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--success\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--error\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"ssr-code-body\">\u003Cdiv class=\"ssr-code-pane\">\u003Cdiv class=\"ssr-code-gutter\" aria-hidden=\"true\">\u003Cspan class=\"ssr-code-num\">1\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">2\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">3\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">4\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">5\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">6\u003C\u002Fspan>\u003C\u002Fdiv>\u003Cpre class=\"ssr-code-pre\">\u003Ccode class=\"hljs language-mysql\">\u003Cspan class=\"ssr-code-line\"># 这种设置方式是临时的，也就是重启服务器后该设置就会失效。\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">set global innodb_buffer_pool_size = 268435456;\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">​\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\"># 永久设置的办法就是修改配置文件，然后需要重启MySQL服务器。\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">[server]\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">innodb_buffer_pool_size = 268435456\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fpre>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>\n\u003Ch3 id=\"h3-17-4-buffer-pool-\">4. 多个 Buffer Pool 实例\u003C\u002Fh3>\n\u003Cdiv class=\"ssr-code-block\" data-lang=\"mysql\">\u003Cdiv class=\"ssr-code-head\">\u003Cspan class=\"ssr-code-lang\">mysql\u003C\u002Fspan>\u003Cdiv class=\"ssr-code-actions\">\u003Cbutton class=\"ssr-code-fullscreen ssr-code-iconbtn\" type=\"button\" aria-label=\"全屏查看代码\" title=\"全屏\">\u003Csvg viewBox=\"0 0 24 24\" width=\"17\" height=\"17\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M5 5h6v2H7v4H5V5zm14 0v6h-2V7h-4V5h6zM5 19v-6h2v4h4v2H5zm14 0h-6v-2h4v-4h2v6z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003Cbutton class=\"ssr-code-copy ssr-code-iconbtn\" type=\"button\" aria-label=\"复制代码\" title=\"复制\">\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--default\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--success\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--error\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"ssr-code-body\">\u003Cdiv class=\"ssr-code-pane\">\u003Cdiv class=\"ssr-code-gutter\" aria-hidden=\"true\">\u003Cspan class=\"ssr-code-num\">1\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">2\u003C\u002Fspan>\u003C\u002Fdiv>\u003Cpre class=\"ssr-code-pre\">\u003Ccode class=\"hljs language-mysql\">\u003Cspan class=\"ssr-code-line\">[server]\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">innodb_buffer_pool_instances = 2\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fpre>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>\n\u003Cp>这样就表明我们要创建 2 个 Buffer Pool 实例。\u003C\u002Fp>\n\u003Cp>我们看下如何查看缓冲池的个数，使用命令：\u003C\u002Fp>\n\u003Cdiv class=\"ssr-code-block\" data-lang=\"mysql\">\u003Cdiv class=\"ssr-code-head\">\u003Cspan class=\"ssr-code-lang\">mysql\u003C\u002Fspan>\u003Cdiv class=\"ssr-code-actions\">\u003Cbutton class=\"ssr-code-fullscreen ssr-code-iconbtn\" type=\"button\" aria-label=\"全屏查看代码\" title=\"全屏\">\u003Csvg viewBox=\"0 0 24 24\" width=\"17\" height=\"17\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M5 5h6v2H7v4H5V5zm14 0v6h-2V7h-4V5h6zM5 19v-6h2v4h4v2H5zm14 0h-6v-2h4v-4h2v6z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003Cbutton class=\"ssr-code-copy ssr-code-iconbtn\" type=\"button\" aria-label=\"复制代码\" title=\"复制\">\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--default\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--success\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--error\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"ssr-code-body\">\u003Cdiv class=\"ssr-code-pane\">\u003Cdiv class=\"ssr-code-gutter\" aria-hidden=\"true\">\u003Cspan class=\"ssr-code-num\">1\u003C\u002Fspan>\u003C\u002Fdiv>\u003Cpre class=\"ssr-code-pre\">\u003Ccode class=\"hljs language-mysql\">\u003Cspan class=\"ssr-code-line\">show variables like &#39;innodb_buffer_pool_instances&#39;;\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fpre>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>\n\u003Cp>那每个 Buffer Pool 实例实际占多少内存空间呢？其实使用这个公式算出来的：\u003C\u002Fp>\n\u003Cdiv class=\"ssr-code-block\" data-lang=\"text\">\u003Cdiv class=\"ssr-code-head\">\u003Cspan class=\"ssr-code-lang\">text\u003C\u002Fspan>\u003Cdiv class=\"ssr-code-actions\">\u003Cbutton class=\"ssr-code-fullscreen ssr-code-iconbtn\" type=\"button\" aria-label=\"全屏查看代码\" title=\"全屏\">\u003Csvg viewBox=\"0 0 24 24\" width=\"17\" height=\"17\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M5 5h6v2H7v4H5V5zm14 0v6h-2V7h-4V5h6zM5 19v-6h2v4h4v2H5zm14 0h-6v-2h4v-4h2v6z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003Cbutton class=\"ssr-code-copy ssr-code-iconbtn\" type=\"button\" aria-label=\"复制代码\" title=\"复制\">\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--default\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--success\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--error\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"ssr-code-body\">\u003Cdiv class=\"ssr-code-pane\">\u003Cdiv class=\"ssr-code-gutter\" aria-hidden=\"true\">\u003Cspan class=\"ssr-code-num\">1\u003C\u002Fspan>\u003C\u002Fdiv>\u003Cpre class=\"ssr-code-pre\">\u003Ccode class=\"hljs language-text\">\u003Cspan class=\"ssr-code-line\">innodb_buffer_pool_size\u002Finnodb_buffer_pool_instances\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fpre>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>\n\u003Cp>也就是总共的大小除以实例的个数，结果就是每个 Buffer Pool 实例占用的大小。\u003C\u002Fp>\n\u003Cblockquote>\n\u003Cp>不过也不是说 Buffer Pool 实例创建的越多越好，分别管理各个 Buffer Pool 也是需要性能开销，InnoDB 规定：当 innodb_buffer_pool_size 的值小于 1G 的时候设置多个实例是无效的，InnoDB 会默认把 innodb_buffer_pool_instances 的值修改为 1。而我们鼓励在 Buffer Pool 大于或等于 1G 的时候设置多个 Buffer Pool 实例。\u003C\u002Fp>\n\u003C\u002Fblockquote>\n","逻辑架构剖析 1. 连接层 系统（客户端）访问 MySQL 服务器前，做的第一件事就是建立 TCP 连接。经过三次握手建立连接成功后， MySQL 服务器对 TCP 传输过来的账号密码做身份认证、权限获取。 用户名或密码不对，会收到一个 Access denied for user 错误，客户端程序结束执行。 用户名密","https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202310131810668.png"]