网站怎么做百度优化,专业移动微网站建设,贵阳网,启迪网站建设介绍
MySQL 8.0.16 引入一个实验特性#xff1a;explain formattree #xff0c;树状的输出执行过程#xff0c;以及预估成本和预估返 回行数。在 MySQL 8.0.18 又引入了 EXPLAIN ANALYZE#xff0c;在 formattree 基础上#xff0c;使用时#xff0c;会执行 SQL #…介绍
MySQL 8.0.16 引入一个实验特性explain formattree 树状的输出执行过程以及预估成本和预估返 回行数。在 MySQL 8.0.18 又引入了 EXPLAIN ANALYZE在 formattree 基础上使用时会执行 SQL 并输出迭代器感觉这里用“算子”更容易理解相关的实际信息比如执行成本、返回行数、 执行时间循环次数。
文档链接https://dev.mysql.com/doc/refman/8.0/en/explain.html#explain-analyze
示例
mysql explain formattree SELECT * FROM t1 WHERE t1.a IN (SELECT t2.b FROM t2 WHERE id10);
*************************** 1. row ***************************
- Nested loop inner join (cost4.95 rows9)
- Filter: (subquery2.b is not null) (cost2.83..1.80 rows9)
- Table scan on subquery2 (cost0.29..2.61 rows9)
- Materialize with deduplication (cost3.25..5.58 rows9)
- Filter: (t2.b is not null) (cost2.06 rows9)
- Filter: (t2.id 10) (cost2.06 rows9)
- Index range scan on t2 using PRIMARY (cost2.06 rows9)
- Index lookup on t1 using a (asubquery2.b) (cost2.35 rows1)
1 row in set
mysql explain analyze SELECT * FROM t1 WHERE t1.a IN (SELECT t2.b FROM t2 WHERE id
10)\G
*************************** 1. row ***************************
- Nested loop inner join (cost4.95 rows9) (actual time0.153..0.200 rows9 loops1)
- Filter: (subquery2.b is not null) (cost2.83..1.80 rows9) (actual
time0.097..0.100 rows9 loops1)
- Table scan on subquery2 (cost0.29..2.61 rows9) (actual time0.001..0.002 rows9
loops1)
- Materialize with deduplication (cost3.25..5.58 rows9) (actual time0.090..0.092
rows9 loops1)
- Filter: (t2.b is not null) (cost2.06 rows9) (actual time0.037..0.042 rows9
loops1)
- Filter: (t2.id 10) (cost2.06 rows9) (actual time0.036..0.040 rows9 loops1)
- Index range scan on t2 using PRIMARY (cost2.06 rows9) (actual time0.035..0.038
rows9 loops1)
- Index lookup on t1 using a (asubquery2.b) (cost2.35 rows1) (actual
time0.010..0.010 rows1 loops9)
1 row in set (0.01 sec)
可以看出 explain formattree 与传统的执行计划相比展示了比较清晰的执行过程。而 explain analyze 则会在此基础上多输出实际的执行时间、返回行数和循环次数。
阅读顺序
1从右到左没有遇到并列的迭代器之前都是从右边开始执行
2从上到下遇到并列的迭代器都是上边的先开始执行
上述示例阅读顺序如下图注意最好不要\G 输出否则第一行的缩进不准确SQL 的执行顺序为
1使用 Nested loop inner join 算法
2t2 先取数据Index range scan、筛选Filter、物化成临时表Materialize作为驱动表
3将驱动表数据带入到 t1 进行查询Index lookup on t1循环执行 9 次 重要信息
以下面为例
Index lookup on t1 using a (a.b) (cost2.35 rows1)
(actual time0.015..0.017 rows1 loops9)
cost 预估的成本信息计算比较复杂。如果想了解可以查看explain formatjson 详解
rows 第一个 rows 是预估值第二个 rows 是实际返回行数。
actual time “0.015..0.017”注意这里有两个值第一个值是获取第一行的实际时间第二个值获取所有行的时间如果循环了多次就是平均时间单位毫秒。
loops 因为这里使用了 Nested loop inner join 算法按照阅读顺序t2 是驱动表先进行查询被物化成临时 表t1 表做为被驱动表循环查询的次数是 9 次即 loops9