MySQL 8.0 OCP (1Z0-908) 考点精析-性能优化考点3:EXPLAIN ANALYZE

news2024/10/2 14:37:41

文章目录

    • MySQL 8.0 OCP (1Z0-908) 考点精析-性能优化考点3:EXPLAIN ANALYZE
      • EXPLAIN ANALYZE介绍
      • EXPLAIN ANALYZE的特性
      • EXPLAIN 和EXPLAIN ANALYZE的结果对比
      • 例题
      • 例题解析
      • 参考

MySQL 8.0 OCP (1Z0-908) 考点精析-性能优化考点3:EXPLAIN ANALYZE

EXPLAIN ANALYZE介绍

EXPLAIN ANALYZE是MySQL 8.0.18版本以后引入的新功能,用于对EXPLAIN命令的扩展。
其输出结果类似EXPLAIN ( format=tree )一样输出查询计划和估计成本之外,还会输出执行计划中各个迭代器的实际成本和执行时间等信息,帮助用户分析和了解慢SQL的瓶颈。

-预估的执行成本
-预估的返回行数
-实际返回第一条的时间 (ms)
-实际返回所有行的时间 (ms)
-实际迭代器返回的行数 (rows) 
-实际循环次数  loops

EXPLAIN ANALYZE具体语法如下。

{EXPLAIN | DESCRIBE | DESC} ANALYZE [FORMAT = TREE] select_statement

虽然我们常常使用DESCRIBE获取表结构的信息,EXPLAIN用于获取查询执行计划,实际上EXPLAIN和DESCRIBE 语句其实是同义词也就是功能相同。

例:

mysql> desc t1;
+------------+-------------------+------+-----+---------+-------+
| Field      | Type              | Null | Key | Default | Extra |
+------------+-------------------+------+-----+---------+-------+
| actor_id   | smallint unsigned | NO   |     | 0       |       |
| first_name | varchar(45)       | NO   |     | NULL    |       |
| last_name  | varchar(45)       | NO   |     | NULL    |       |
| film_info  | text              | YES  |     | NULL    |       |
+------------+-------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> explain t1;
+------------+-------------------+------+-----+---------+-------+
| Field      | Type              | Null | Key | Default | Extra |
+------------+-------------------+------+-----+---------+-------+
| actor_id   | smallint unsigned | NO   |     | 0       |       |
| first_name | varchar(45)       | NO   |     | NULL    |       |
| last_name  | varchar(45)       | NO   |     | NULL    |       |
| film_info  | text              | YES  |     | NULL    |       |
+------------+-------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

EXPLAIN ANALYZE的特性

  1. EXPLAIN ANALYZE 不仅可以分析SELECT语句,也可以分析UPDATE和DELETE语句,MySQL 8.0.19开始还支持TABLE语句。
  2. 通过EXPLAIN ANALYZE分析SQL语句的时候,会执行SQL但是不返回结果,同时收集执行信息。

例:使用 EXPLAIN ANALYZEDELETE语句

mysql> select count(*) from t1;
+----------+
| count(*) |
+----------+
|      200 |
+----------+
1 row in set (0.02 sec)

mysql>  explain analyze delete from t1 where t1.actor_id in (select actor_id from actor_info);
+--------------------------------------------------------------------------------------------------------
| EXPLAIN                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
+--------------------------------------------------------------------------------------------------------
| -> Delete from t1 (immediate)  (cost=23642.12 rows=235996) (actual time=848.799..848.799 rows=0 loops=1)
    -> Nested loop inner join  (cost=23642.12 rows=235996) (actual time=848.443..848.790 rows=200 loops=1)
        -> Table scan on t1  (cost=22.50 rows=200) (actual time=0.042..0.273 rows=200 loops=1)
        -> Single-row index lookup on <subquery2> using <auto_distinct_key> (actor_id=t1.actor_id)  (cost=5240.57..5240.57 rows=1) (actual time=4.242..4.242 rows=1 loops=200)
            -> Materialize with deduplication  (cost=5240.57..5240.57 rows=1180) (actual time=848.395..848.395 rows=200 loops=1)
                -> Table scan on actor_info  (cost=5105.35..5122.57 rows=1180) (actual time=848.265..848.305 rows=200 loops=1)
                    -> Materialize  (cost=5105.33..5105.33 rows=1180) (actual time=848.262..848.262 rows=200 loops=1)
                        -> Group aggregate: group_concat(distinct concat(c.`name`,': ',(select #4)) order by c.`name` ASC separator '; ')  (cost=4987.33 rows=1180) (actual time=2.783..847.454 rows=200 loops=1)
                            -> Nested loop left join  (cost=4441.13 rows=5462) (actual time=0.415..39.124 rows=5462 loops=1)
                                -> Nested loop left join  (cost=2529.43 rows=5462) (actual time=0.406..28.275 rows=5462 loops=1)
                                    -> Nested loop left join  (cost=617.73 rows=5462) (actual time=0.393..7.094 rows=5462 loops=1)
                                        -> Sort: a.actor_id, a.first_name, a.last_name  (cost=20.25 rows=200) (actual time=0.374..0.460 rows=200 loops=1)
                                            -> Table scan on a  (cost=20.25 rows=200) (actual time=0.009..0.274 rows=200 loops=1)
                                        -> Covering index lookup on fa using PRIMARY (actor_id=a.actor_id)  (cost=0.27 rows=27) (actual time=0.003..0.031 rows=27 loops=200)
                                    -> Covering index lookup on fc using PRIMARY (film_id=fa.film_id)  (cost=0.25 rows=1) (actual time=0.003..0.004 rows=1 loops=5462)
                                -> Single-row index lookup on c using PRIMARY (category_id=fc.category_id)  (cost=0.25 rows=1) (actual time=0.002..0.002 rows=1 loops=5462)
                        -> Select #4 (subquery in projection; dependent)
                            -> Aggregate: group_concat(f.title order by f.title ASC separator ', ')  (cost=24.84 rows=1) (actual time=0.144..0.144 rows=1 loops=5462)
                                -> Nested loop inner join  (cost=22.10 rows=27) (actual time=0.046..0.142 rows=3 loops=5462)
                                    -> Nested loop inner join  (cost=12.55 rows=27) (actual time=0.005..0.074 rows=28 loops=5462)
                                        -> Covering index lookup on fa using PRIMARY (actor_id=a.actor_id)  (cost=2.99 rows=27) (actual time=0.002..0.028 rows=28 loops=5462)
                                        -> Single-row index lookup on f using PRIMARY (film_id=fa.film_id)  (cost=0.25 rows=1) (actual time=0.001..0.001 rows=1 loops=154076)
                                    -> Single-row covering index lookup on fc using PRIMARY (film_id=fa.film_id, category_id=c.category_id)  (cost=0.25 rows=1) (actual time=0.002..0.002 rows=0 loops=154076)
 |
+--------------------------------------------------------------------------------------------------------
1 row in set, 2 warnings (0.86 sec)

mysql>
mysql> select count(*) from t1;
+----------+
| count(*) |
+----------+
|      200 |
+----------+
1 row in set (0.02 sec)

通过上面的输出,我们可以看到delete的执行过程和具体执行信息,但是不会实际删除数据。

EXPLAIN 和EXPLAIN ANALYZE的结果对比

下面我们分别通过explain format=tree 和EXPLAIN ANALYZE对SQL语句进行分析。

mysql> explain format=tree select count(*) from actor_info where actor_id in (select actor_id from actor);
+----------------------------------------------------------------------------------------
| EXPLAIN                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
+----------------------------------------------------------------------------------------
| -> Aggregate: count(0)  (cost=48584.74 rows=1)
    -> Nested loop inner join  (cost=24985.12 rows=235996)
        -> Index scan on actor using idx_actor_last_name  (cost=20.25 rows=200)
        -> Index lookup on actor_info using <auto_key0> (actor_id=actor.actor_id)  (cost=5105.58..5112.17 rows=27)
            -> Materialize  (cost=5105.33..5105.33 rows=1180)
                -> Group aggregate: group_concat(distinct concat(c.`name`,': ',(select #4)) order by c.`name` ASC separator '; ')  (cost=4987.33 rows=1180)
                    -> Nested loop left join  (cost=4441.13 rows=5462)
                        -> Nested loop left join  (cost=2529.43 rows=5462)
                            -> Nested loop left join  (cost=617.73 rows=5462)
                                -> Sort: a.actor_id, a.first_name, a.last_name  (cost=20.25 rows=200)
                                    -> Table scan on a  (cost=20.25 rows=200)
                                -> Covering index lookup on fa using PRIMARY (actor_id=a.actor_id)  (cost=0.27 rows=27)
                            -> Covering index lookup on fc using PRIMARY (film_id=fa.film_id)  (cost=0.25 rows=1)
                        -> Single-row index lookup on c using PRIMARY (category_id=fc.category_id)  (cost=0.25 rows=1)
                -> Select #4 (subquery in projection; dependent)
                    -> Aggregate: group_concat(f.title order by f.title ASC separator ', ')  (cost=24.84 rows=1)
                        -> Nested loop inner join  (cost=22.10 rows=27)
                            -> Nested loop inner join  (cost=12.55 rows=27)
                                -> Covering index lookup on fa using PRIMARY (actor_id=a.actor_id)  (cost=2.99 rows=27)
                                -> Single-row index lookup on f using PRIMARY (film_id=fa.film_id)  (cost=0.25 rows=1)
                            -> Single-row covering index lookup on fc using PRIMARY (film_id=fa.film_id, category_id=c.category_id)  (cost=0.25 rows=1)
 |
+----------------------------------------------------------------------------------------
1 row in set, 2 warnings (0.00 sec)

mysql>
mysql> explain analyze select count(*) from actor_info where actor_id in (select actor_id from actor);
+----------------------------------------------------------------------------------------
| EXPLAIN                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
+----------------------------------------------------------------------------------------
| -> Aggregate: count(0)  (cost=48584.74 rows=1) (actual time=677.721..677.722 rows=1 loops=1)
    -> Nested loop inner join  (cost=24985.12 rows=235996) (actual time=677.413..677.696 rows=200 loops=1)
        -> Covering index scan on actor using idx_actor_last_name  (cost=20.25 rows=200) (actual time=0.051..0.093 rows=200 loops=1)
        -> Index lookup on actor_info using <auto_key0> (actor_id=actor.actor_id)  (cost=5105.58..5112.17 rows=27) (actual time=3.388..3.388 rows=1 loops=200)
            -> Materialize  (cost=5105.33..5105.33 rows=1180) (actual time=677.355..677.355 rows=200 loops=1)
                -> Group aggregate: group_concat(distinct concat(c.`name`,': ',(select #4)) order by c.`name` ASC separator '; ')  (cost=4987.33 rows=1180) (actual time=2.301..676.262 rows=200 loops=1)
                    -> Nested loop left join  (cost=4441.13 rows=5462) (actual time=0.236..31.592 rows=5462 loops=1)
                        -> Nested loop left join  (cost=2529.43 rows=5462) (actual time=0.227..22.852 rows=5462 loops=1)
                            -> Nested loop left join  (cost=617.73 rows=5462) (actual time=0.211..3.611 rows=5462 loops=1)
                                -> Sort: a.actor_id, a.first_name, a.last_name  (cost=20.25 rows=200) (actual time=0.186..0.297 rows=200 loops=1)
                                    -> Table scan on a  (cost=20.25 rows=200) (actual time=0.033..0.088 rows=200 loops=1)
                                -> Covering index lookup on fa using PRIMARY (actor_id=a.actor_id)  (cost=0.27 rows=27) (actual time=0.007..0.014 rows=27 loops=200)
                            -> Covering index lookup on fc using PRIMARY (film_id=fa.film_id)  (cost=0.25 rows=1) (actual time=0.003..0.003 rows=1 loops=5462)
                        -> Single-row index lookup on c using PRIMARY (category_id=fc.category_id)  (cost=0.25 rows=1) (actual time=0.001..0.001 rows=1 loops=5462)
                -> Select #4 (subquery in projection; dependent)
                    -> Aggregate: group_concat(f.title order by f.title ASC separator ', ')  (cost=24.84 rows=1) (actual time=0.114..0.114 rows=1 loops=5462)
                        -> Nested loop inner join  (cost=22.10 rows=27) (actual time=0.041..0.112 rows=3 loops=5462)
                            -> Nested loop inner join  (cost=12.55 rows=27) (actual time=0.009..0.053 rows=28 loops=5462)
                                -> Covering index lookup on fa using PRIMARY (actor_id=a.actor_id)  (cost=2.99 rows=27) (actual time=0.007..0.012 rows=28 loops=5462)
                                -> Single-row index lookup on f using PRIMARY (film_id=fa.film_id)  (cost=0.25 rows=1) (actual time=0.001..0.001 rows=1 loops=154076)
                            -> Single-row covering index lookup on fc using PRIMARY (film_id=fa.film_id, category_id=c.category_id)  (cost=0.25 rows=1) (actual time=0.002..0.002 rows=0 loops=154076)
 |
+----------------------------------------------------------------------------------------
1 row in set, 2 warnings (0.68 sec)

mysql>

我们可以看到explain analyze 和explain format=tree的结果的基础上多了(actual time=XXX…XXX rows=XXX loops=XXX)的内容。
也就是上面介绍的如下内容:

-actual time:实际返回第一条的时间 (ms)-
-实际返回所有行的时间 (ms)
-实际迭代器返回的行数 (rows) 
-loops:实际循环次数  loops

例题

Choose two. Examine this query and output:

mysql> EXPLAIN ANALYZE
               SELECT city.CountryCode, country.Name AS Country_Name , city.Name, city.District, city.Population
               FROM world.city
                           INNER JOIN world.country ON country.Code = city.CountryCode
               WHERE country.Continent = ' Asia ' AND city.Population > 1000000
               ORDER BY city.Population DESC\G

在这里插入代码片

Which two statements are true?
A) The country table is accessed as the first table, and then joined to the city table. 
B) 35 rows from the city table are included in the result. 
C) The optimizer estimates that 51 rows in the country table have Continent = ' Asia '. 
D) It takes more than 8 milliseconds to sort the rows. 
E) The query returns exactly 125 rows.

例题解析

  • A 正确:我们可以看到使用的是Nest loop方式进行结合(inner join),所以首先会访问驱动表(也就是第一个表)country,然后针对驱动表country的每条记录,利用country.Code = city.CountryCode这个条件,扫描一次内部表city表。
  • B 错误:每次通过索引扫描city时返回行数是rows =35,但是 通过Filter后返回行数是rows=2。
  • C 错误:优化器预估country表中满足条件(country.Continent = ’ Asia ')的记录为rows =34,实际行数为actual … rows=51。
  • D 错误:sort消耗时间是8.431 - 8.033
  • E 查询实际返回行数 actual … rows=125。
  • country表中满足条件的记录为rows=51,所以会对city循环执行51次 (loops=51)

参考答案:AE

参考

https://dev.mysql.com/doc/refman/8.0/en/explain.html
https://dev.mysql.com/doc/refman/8.0/en/explain.html#explain-analyze

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/436906.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

部门来了个测试人,听说是00后,上来一顿操作给我看呆了...

今天上班开早会就是新人见面仪式&#xff0c;听说来了个很厉害的大佬&#xff0c;年纪还不大&#xff0c;是上家公司离职过来的&#xff0c;薪资已经达到中高等水平&#xff0c;很多人都好奇不已&#xff0c;能拿到这个薪资应该人不简单&#xff0c;果然&#xff0c;自我介绍的…

网络原理(TCP/UDP)

目录 一. 网络基础 1. IP地址 2. 端口号 3. 协议 4. OSI七层模型 二. UDP协议 2.1 UDP的协议端格式&#xff1a; 2.2 UDP的特点 三. TCP协议 3.1 TCP协议段格式 3.2 TCP原理 &#xff08;1&#xff09;确认应答机制 &#xff08;2&#xff09;超时重传机制 &#xff…

CmBacktrace库在工程中的添加和应用

CmBacktrace 介绍在工程中添加CmBacktrace断言打印全局变量的值循环输出错误信息串口处理看门狗处理 介绍 CmBacktrace下载 CmBacktrace &#xff08;Cortex Microcontroller Backtrace&#xff09;是一款针对 ARM Cortex-M 系列 MCU 的错误代码自动追踪、定位&#xff0c;错…

Javaee Spring template实现查询数据库表内容 基于半xml半注解

昨天用基于xml配置实现template查询数据库&#xff0c;今天基于半xml半注解方式实现,使用注解需要导入spring-aop-5.3.8.jar 导入jar包 项目结构&#xff1a; 其他代码在&#xff0c;先前上一篇文章已经给出 AccountServiceImpl package wwx.dao;import org.springframework…

定时器中断实验

实现内容 利用TIM3的定时器中断来控制DS1的翻转&#xff0c;在主函数用DS0 的翻转来提示程序正在运行。 定时器介绍 定时器可以认为是一个计数器&#xff1b;给定计数器一个初值&#xff0c;每当计数一次&#xff0c;就会走过一个固定的时间&#xff0c;当达到我们给定的初值…

强大的图像处理:ImageKit10.E ActiveX Crack

强大的图像处理&#xff01; ImageKit10 ActiveX 是一个组件&#xff0c;允许您快速轻松地向应用程序添加图像处理功能。使用 ImageKit10 ActiveX&#xff0c;您可以编写从 TWAIN 扫描仪和数码相机检索图像的应用程序;加载和保存图像文件并将图像从一种格式转换为另一种格式;编…

数字电路和模拟电路-半导体三极管

目录 1 什么是三极管&#xff1f; 1.1 放大状态时的偏执条件 1.1.1发射结加正向电压&#xff0c;扩散运动形成发射极电流IE 1.1.2扩散到基区的自由电子与空穴的复合运动形成基极电流IB 1.1.3集电结加反向电压&#xff0c;漂移运动形成集电极电流Ⅰc 2 三极管工作原理 2.…

5.图论(0x3f:从周赛中学算法 2022下)

来自0x3f【从周赛中学算法 - 2022 年周赛题目总结&#xff08;下篇&#xff09;】&#xff1a;https://leetcode.cn/circle/discuss/WR1MJP/ 周赛中的图论题目比较少&#xff0c;除了下面选的 DFS、BFS、拓扑排序、基环树、二分图判定等&#xff0c;还有最短路、DFS 时间戳等&a…

CloudCompare二次开发之如何设计界面ui与功能实现?

文章目录 0.引言1.创建界面ui相关文件2.添加界面ui相关文件到CloudCompare工程3.修改工程相关文件4.结果展示 0.引言 CloudCompare源代码编译成功后&#xff0c;即可进行二次开发&#xff0c;可以通过修改源码实现二次开发&#xff0c;二次开发基础功能见&#xff08;CloudComp…

什么是文件共享软件?文件传输软件如何共享?

它是一个文件共享软件应用程序&#xff0c;可让强大的数据保护层下将任何大小的文件发送到世界上的任何地方。以光速发送和共享无限数量的文件。可以提交门户并使用语言&#xff0c;品牌&#xff0c;存储等自定义门户。可以选择一个存储点&#xff0c;例如文件传输软件&#xf…

[入门必看]数据结构4.2:串的模式匹配

[入门必看]数据结构4.2&#xff1a;串的模式匹配 第四章 串4.2 串的模式匹配知识总览4.2.1_朴素模式匹配算法4.2.2_1_KMP算法4.2.2_2_求next数组4.2.3_KMP算法的进一步优化 4.2.1_朴素模式匹配算法什么是字符串的模式匹配朴素模式匹配算法通过数组下标实现朴素模式匹配算法代码…

http(1)

主要介绍http 1.0 我们在浏览器中输入一个网址&#xff0c;稍等片刻就看见了网页 客户端会发送一个http请求&#xff0c;要求返回cn.bing.com这个网址&#xff0c;服务器收到请求后就会返回一个html页面 &#xff08;服务器根据请求找到客户端想要的资源&#xff0c;然后把这个…

[LeetCode]路径总和

给你二叉树的根节点 root 和一个表示目标和的整数 targetSum 。判断该树中是否存在 根节点到叶子节点 的路径&#xff0c;这条路径上所有节点值相加等于目标和 targetSum 。如果存在&#xff0c;返回 true &#xff1b;否则&#xff0c;返回 false 。 叶子节点 是指没有子节点…

【并发编程】线程池的原理和源码分析

线程使用上可能的问题 我们一般通过new Thread().start();来创建和运行一个线程&#xff0c;如果在业务过程中有大量场景需要使用多线程来并发&#xff0c;那么就会有以下问题 需要频繁的创建和销毁线程 &#xff0c;需要消耗CPU资源如果创建和销毁的线程的数量过多(大于CPU核…

CMOS图像传感器——从传感器冗余说起

在这先抛出一个概念,什么是成像圈?众所周知,相机的镜头近似于圆柱体,光线透过圆筒子投射出的大都是圆形。我们可以拿一个镜头演示一下,当这个圆圈投在传感器所在焦平面时,我们称之为像场。像场的边界我们称之为成像圈,成像圈是圆的,但是传感器是矩形,天圆地方的怎么放…

Lombok插件下载与离线安装

Lombok插件下载与离线安装 首先你既然搜要离线安装或下载&#xff0c;那么肯定也是在IDEA工具里面&#xff0c;无法搜索到&#xff0c;或者自动下载安装失败吧&#xff1f; 安装包下载地址 记得和 idea版本一样&#xff0c; 如果不知道啥版本看下面

CleanMyMac X4.15重大更新 新功能菜单发布

CleanMyMac&#xff0c;一款电脑清理软件&#xff0c;可以帮助你清理垃圾文件、优化系统性能、管理应用程序等。它就像你的电脑管家&#xff0c;让你的电脑始终保持最佳状态。无论是手机还是电脑&#xff0c;在使用一段时间之后都可能会发生卡顿的现象&#xff0c;很多小伙伴会…

C++ 高级数据结构————[ 单调栈 ]

每周一篇的算法文章来了 今天讲解的是高级数据结构中的——单调栈 单调栈&#xff0c;顾名思义&#xff0c;就是升级版的栈&#xff08;&#xff09; 先回顾一下栈把 栈&#xff0c;是一种线性表&#xff0c;它的特点是只能从一边进出&#xff0c;并且先进后出&#xff0c;后进…

Windows入门篇一之MSDN手册的使用和第一个窗口程序

Windows入门篇之MSDN手册的使用和第一个窗口程序 MSDN手册MSDN手册是什么MSDN手册的下载和安装MSDN手册的使用 第一个窗口程序项目的创建第一个简单的窗口程序 MSDN手册 MSDN手册是什么 MSDN手册是VS中的一个帮助手册&#xff0c;帮助初学者学习Windows编程&#xff0c;来查找…

opencv实现卡尔曼滤波

卡尔曼滤波是一种递归的估计&#xff0c;即只要获知上一时刻状态的估计值以及当前状态的观测值就可以计算出当前状态的估计值&#xff0c;因此不需要记录观测或者估计的历史信息。 卡尔曼滤波器分为两个阶段&#xff1a;预测与更新。在预测阶段&#xff0c;滤波器使用上一状态…