MySQL-SQL执行流程及原理

ops/2024/9/24 10:13:51/

1、SQL执行流程

在这里插入图片描述

2、查询流程

  • 查询缓存: MySQL服务器如果在查询缓存中存在该SQL语句,就直接将结果返回给客户端,没有就进入解析器解析阶段。(MySQL 8.0 删除该功能
  • 解析器:在解析器中对SQL语句进行语法及语义分析
  • 查询优化器:在优化器中确定SQL语句的执行路径,比如:根据 全表检索索引检索。优化器的作用就是找到最好的执行计划。可以分为逻辑查询优化阶段(“通过SQL等价变化”提升查询效率)物理查询优化阶段(“通过索引和表的连接方式”进行优化)
  • 执行器:在执行之前判断当前连接用户是否具备执行权限。没有则返回权限错误,有则执行SQL查询并返回结果。

3、执行原理

①MySQL8.0中SQL执行原理

  • 确认profiling是否开启,执行select @@profilingshow variables like '%prifiling%'查看是否开启计划,开启后可以收集SQL执行时所使用的资源情况。profiling=0 代表关闭,设置为profiling=1表示开启
sql>mysql> show variables like '%profiling%';
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| have_profiling         | YES   |
| profiling              | OFF   |
| profiling_history_size | 15    |
+------------------------+-------+
3 rows in set (8.27 sec)
sql>mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
|           0 |
+-------------+
1 row in set, 1 warning (0.25 sec)
sql>mysql> set profiling=1;
Query OK, 0 rows affected, 1 warning (0.08 sec)sql>mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
|           1 |
+-------------+
1 row in set, 1 warning (0.01 sec)
  • 执行相关SQL脚本
  • 查看profiles:执行 show profile 和 show profiles 语句可以展示当前会话中执行语句的资源使用情况
sql>mysql> show profiles;
+----------+------------+-----------------------------------+
| Query_ID | Duration   | Query                             |
+----------+------------+-----------------------------------+
|        1 | 0.01602275 | select @@profiling                |
|        2 | 0.59588800 | show variables like '%profiling%' |
|        3 | 2.45071500 | show databases                    |
|        4 | 0.08227075 | SELECT DATABASE()                 |
|        5 | 0.03805175 | show databases                    |
|        6 | 1.01924225 | show tables                       |
|        7 | 0.04931425 | SELECT DATABASE()                 |
|        8 | 0.00020850 | show profiling                    |
+----------+------------+-----------------------------------+
8 rows in set, 1 warning (0.05 sec)
  • 查看profile:默认查询最近一次SQL语句执行情况
sql>mysql> show profile;
+---------------+----------+
| Status        | Duration |
+---------------+----------+
| starting      | 0.000125 |
| freeing items | 0.000062 |
| cleaning up   | 0.000022 |
+---------------+----------+
3 rows in set, 1 warning (0.17 sec)

如果查看执行某一次SQL执行情况,执行:show profile for query num(num 表示上述show profiles 语句返回的 query_id),如下所示

sql>mysql> show profile for query 7;
+----------------------+----------+
| Status               | Duration |
+----------------------+----------+
| starting             | 0.000052 |
| checking permissions | 0.000007 |
| Opening tables       | 0.000011 |
| init                 | 0.000005 |
| optimizing           | 0.000008 |
| executing            | 0.000009 |
| end                  | 0.000003 |
| query end            | 0.000006 |
| closing tables       | 0.000004 |
| freeing items        | 0.000009 |
| cleaning up          | 0.049202 |
+----------------------+----------+
11 rows in set, 1 warning (0.11 sec)
  • 通过查看 show profile all 可以查看MySQL服务器在执行SQL语句时内存,cpu等相关信息。
sql>mysql> show profile all\G
*************************** 1. row ***************************Status: startingDuration: 0.140282CPU_user: 0.068184CPU_system: 0.071855Context_voluntary: 5
Context_involuntary: 2Block_ops_in: 0Block_ops_out: 0Messages_sent: 0Messages_received: 0Page_faults_major: 0Page_faults_minor: 0Swaps: 0Source_function: NULLSource_file: NULLSource_line: NULL
*************************** 2. row ***************************Status: freeing itemsDuration: 0.040331CPU_user: 0.020129CPU_system: 0.020204Context_voluntary: 0
Context_involuntary: 0Block_ops_in: 0Block_ops_out: 0Messages_sent: 0Messages_received: 0Page_faults_major: 0Page_faults_minor: 0Swaps: 0Source_function: dispatch_sql_commandSource_file: sql_parse.ccSource_line: 5042
*************************** 3. row ***************************Status: cleaning upDuration: 0.000018CPU_user: 0.000004CPU_system: 0.000006Context_voluntary: 0
Context_involuntary: 0Block_ops_in: 0Block_ops_out: 0Messages_sent: 0Messages_received: 0Page_faults_major: 0Page_faults_minor: 0Swaps: 0Source_function: dispatch_commandSource_file: sql_parse.ccSource_line: 2252
3 rows in set, 1 warning (0.00 sec)

②MySQL5.7中SQL执行原理

  • 配置文件中开启查询缓存
[root@rqtanc ~]# vim /etc/my.cnf[sql>mysqld]
query_cache_type=1[root@rqtanc ~]# systemctl restart sql>mysqld
  • 后续同 MySQL8.0中SQL执行原理

http://www.ppmy.cn/ops/30697.html

相关文章

Nginx基本使用 反向代理与负载均衡

什么是Nginx Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器。 其特点是占有内存少,并发能力强,nginx的并发能力在同类型的网页服务器中表现较好,而且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动。 …

Linux开发板 FTP 服务器移植与搭建

VSFTPD(Very Secure FTP Daemon)是一个安全、稳定且快速的FTP服务器软件,广泛用于Unix和Linux操作系统。它以其轻量级、高效和易于配置而受到赞誉。VSFTPD不仅支持标准的FTP命令和操作,还提供了额外的安全特性,如匿名F…

【SpringBoot】Spring Boot自动配置概览

目录 背景自动装配/自动配置springboot是如何实现自动配置的核心注解AutoConfigurationImportSelector 类的继承体系Spring Boot 提供的条件注解示例注意版本 背景 没有 Spring Boot 的情况下,我们引入第三方依赖之后,需要手动配置。 比如需要手动将引入…

uniapp0基础编写安卓原生插件和调用第三方jar包和编写语音播报插件之零基础编写安卓插件

前言 因为公司需要开发一款APP,还是对接第三方的安卓系统,所以需要使用到jar包,同时还要自己开发安卓插件,让uniapp调用,本篇文章将0基础讲一下uniapp如何开发安卓插件和第三方jar包如何调用和编写一个语音播报(android.speech.tts.TextToSpeech)插件 操作步骤 1.要下载…

库函数strncpy的使用及其模拟实现

一、什么是strncpy strncpy是一个C语言标准库函数,用于将一个字符串的一部分复制到另一个字符串中。它的声明通常是这样的: char *strncpy(char *dest, const char *src, size_t n); 其中: dest为目标字符串;src为源字符串&am…

事件处理模式--reactor原理与实现

文章目录 reactorapicode reactor reactor是是服务器的重要模型, 是一种事件驱动的反应堆模式 通过epoll_create() 创建句柄, epoll_ctrl()提前注册好不同的事件处理函数 , 当事件到来就由 epoll_wait () 获取同时到来的多个事件,并且根据数据的不同类型将事件分发…

Linux / Ubuntu 备份数据

Linux / Ubuntu 备份数据 需要备份的文件tar 工具备份/打包过程恢复/解包过程 流程自动化start_backup.shserver_backup.sh 同步发布在个人笔记Linux / Ubuntu 备份数据 需要备份的文件 对于我们的 linux 服务器(当然也适用于桌面端),时常进…

Pyside6详细使用教程python之GUI开发

1、首先需要安装Pyside6,终端执行命令: pip3.10 install pyside6 2、你们的一般是 pip install pyside6 2、如下代码创建一个简易程序导入必要的模块 import sys from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton,…