当前位置:网站首页>L01_一条SQL查询语句是如何执行的?
L01_一条SQL查询语句是如何执行的?
2022-06-24 07:54:00 【jackaroo2020】
导读
本篇主要通过一条SQL查询语句,让我们初步了解MYSQL基本架构。

1、MYSQL逻辑架构图
mysql> select * from T where ID=10;
下面是 MySQL 的基本架构示意图,从中你可以清楚地看到 SQL 语句在 MySQL 的各个功能模块中的执行过程。

2、MySQL的框架有几个组件, 各是什么作用?
连接器
负责和mysql客户端建立连接,获取用户权限,维持和管理连接。连接命令一般是这么写的:
mysql>mysql -h$ip -P$port -u$user -p
连接完成后,如果你没有后续的动作,这个连接就处于空闲状态,你可以在 show processlist 命令中看到它。如图所示,其中的 Command 列显示为“Sleep”的这一行,就表示现在系统里面有一个空闲连接。客户端如果太长时间没动静,连接器就会自动将它断开。这个时间是由参数 wait_timeout 控制的,默认值是 8 小时。命令为:
mysql>show processlist;
mysql>show variables like 'wait_timeout';

查询缓存
查询请求先访问缓存(key 是查询的语句,value 是查询的结果)。命中直接返回。不推荐使用缓存,更新会把缓存清除(关闭缓存:参数 query_cache_type 设置成 DEMAND)。而对于你确定要使用查询缓存的语句,可以用 SQL_CACHE 显式指定,像下面这个语句一样:
mysql>select SQL_CACHE * from T where ID=10;
需要注意的是,MySQL 8.0 版本直接将查询缓存的整块功能删掉了,也就是说 8.0 开始彻底没有这个功能了。
分析器
分析sql语句是否存在错误,词法分析和语法分析。
优化器
决定使用哪个索引,多表关联(join)的时候,决定各个表的连接顺序。
执行器
执行语句,先判断用户有无查询权限,使用表定义的存储引擎。
mysql> select * from T where ID=10;
比如我们这个例子中的表 T 中,ID 字段没有索引,那么执行器的执行流程是这样的:
(1) 调用 InnoDB 引擎接口取这个表的第一行,判断 ID 值是不是 10,如果不是则跳过,如果是则将这行存在结果集中;
(2) 调用引擎接口取“下一行”,重复相同的判断逻辑,直到取到这个表的最后一行。
(3) 执行器将上述遍历过程中所有满足条件的行组成的记录集作为结果集返回给客户端。
3、Server层和存储引擎层各是什么作用?
大体来说,MySQL 可以分为 Server 层和存储引擎层两部分。
Server 层包括连接器、查询缓存、分析器、优化器、执行器等,涵盖 MySQL 的大多数核心服务功能,以及所有的内置函数(如日期、时间、数学和加密函数等),所有跨存储引擎的功能都在这一层实现,比如存储过程、触发器、视图等。
存储引擎层负责数据的存储和提取。其架构模式是插件式的,支持 InnoDB、MyISAM、Memory 等多个存储引擎。现在最常用的存储引擎是 InnoDB,它从 MySQL 5.5.5 版本开始成为了默认存储引擎。
笔记主要来源参考于林晓斌老师的<<MYSQL实战45讲>>课程,主要用作个人学习笔记记录,如果本文侵犯了您的权益,请联系本人,本人将会在第一时间删除侵权文章!
边栏推荐
- How does the tunnel mobile inspection track robot monitor 24 hours to ensure the safety of tunnel construction?
- 深入解析 Apache BookKeeper 系列:第三篇——读取原理
- GradScaler MaxClipGradScaler
- 【Redis實現秒殺業務①】秒殺流程概述|基本業務實現
- 216. combined summation III enumeration method
- [ES6 breakthrough] promise is comparable to native custom encapsulation (10000 words)
- leetcode——错误的集合
- 2021-05-20computed and watch applications and differences
- The printed object is [object object]. Solution
- I heard that you are still spending money to buy ppt templates from the Internet?
猜你喜欢

嵌入式 | 硬件转软件的几条建议

The native applet uses canvas to make posters, which are scaled to the same scale. It is similar to the uniapp, but the writing method is a little different

【LeetCode】415. 字符串相加

uniapp 开发多端项目如何配置环境变量以及区分环境打包

4275. Dijkstra sequence

Spark - LeftOuterJoin 结果条数与左表条数不一致

Every (), map (), forearch () methods. There are objects in the array
![[ES6 breakthrough] promise is comparable to native custom encapsulation (10000 words)](/img/b3/b156d75c7b4f03580c449f8499cd74.png)
[ES6 breakthrough] promise is comparable to native custom encapsulation (10000 words)

小白学习MySQL - 增量统计SQL的需求
![[Niuke] convert string to integer](/img/56/3e491b3d0eea0d89a04d0b871501d7.png)
[Niuke] convert string to integer
随机推荐
[noi simulation] pendulum (linear algebra, Du Jiao sieve)
4274. suffix expression
Double pointer analog
Data middle office: middle office practice and summary
【牛客】HJ1 字符串最后一个单词的长度
How to import MDF and LDF files into MySQL workbench
Digital cloud released the 2022 white paper on digital operation of global consumers in the beauty industry: global growth solves marketing problems
Ebanb B1 Bracelet brush firmware abnormal interrupt handling
Target detection series fast r-cnn
PM2 deploy nuxt3 JS project
[quantitative investment] discrete Fourier transform to calculate array period
Every (), map (), forearch () methods. There are objects in the array
2020 China's provinces and cities, three-level linkage data, data agencies (data from the official website of the National Bureau of Statistics)
Pytoch read data set (two modes: typical data set and user-defined data set)
荐书丨《好奇心的秘密》:一个针尖上可以站多少跳舞的小天使?
Epidemic situation, unemployment, 2022, we shouted to lie down!
【LeetCode】387. 字符串中的第一个唯一字符
Alibaba Senior Software Testing Engineer recommends testers to learn -- Introduction to security testing
Data middle office: overview of data governance
从华为WeAutomate数字机器人论坛,看政企领域的“政务新智理”