编者按:最好的学习是总结和分享。

MySQL 8.0 OCP (1Z0-908) 考点精析-性能优化考点1:sys.statement_analysis视图
MySQL 8.0 OCP (1Z0-908) 考点精析-性能优化考点2:系统变量的确认
EXPLAIN ANALYZE介绍
-预估的执行成本
-预估的返回行数
-实际返回第一条的时间 (ms)
-实际返回所有行的时间 (ms)
-实际迭代器返回的行数 (rows)
-实际循环次数 loops复制
{EXPLAIN | DESCRIBE | DESC} ANALYZE [FORMAT = TREE] select_statement
复制
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的特性
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)复制
EXPLAIN 和EXPLAIN ANALYZE的结果对比
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>复制
-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.复制
例题解析
参考
文章转载自SQL和数据库技术,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
【专家有话说第五期】在不同年龄段,DBA应该怎样规划自己的职业发展?
墨天轮编辑部
1295次阅读
2025-03-13 11:40:53
【专家观点】罗敏:从理论到真实SQL,感受DeepSeek如何做性能优化
墨天轮编辑部
1279次阅读
2025-03-06 16:45:38
MySQL8.0统计信息总结
闫建(Rock Yan)
484次阅读
2025-03-17 16:04:03
2月“墨力原创作者计划”获奖名单公布
墨天轮编辑部
456次阅读
2025-03-13 14:38:19
Ogg23ai高手必看-MySQL Innodb Cluster跟oracle的亲密接触
曹海峰
454次阅读
2025-03-04 21:56:13
SQL优化 - explain查看SQL执行计划(一)
金同学
388次阅读
2025-03-13 16:04:22
SQLE 4.0 正式版发布,新增 SQL 重写、SQL 性能追踪、语法知识图谱等功能
爱可生开源社区
358次阅读
2025-03-07 10:30:00
MySQL突然崩溃?教你用gdb解剖core文件,快速锁定“元凶”!
szrsu
349次阅读
2025-03-13 00:29:43
MySQL生产实战优化(利用Index skip scan优化性能提升257倍)
chengang
327次阅读
2025-03-17 10:36:40
MySQL数据库当前和历史事务分析
听见风的声音
260次阅读
2025-04-01 08:47:17
热门文章
手把手教你在Windows 10安装Oracle 19c(详细图文附踩坑指南)
2020-11-06 47817浏览
供收藏:Oracle固定SQL执行计划的方法总结
2021-04-03 6130浏览
执行计划之表连接1:连接树(Join Trees)
2021-06-21 4851浏览
2020年了,还有必要学习Oracle数据库技术么?
2020-06-18 4344浏览
Oracle数据库性能优化之Enq: TM – contention
2021-04-04 4000浏览
最新文章
跟着论文学习数据库7:向量数据库
1天前 3浏览
4月12日 TiDB 社区活动在南京!传统技术栈替换和 AI 浪潮正当时,面向未来的国产数据库如何选择?
6天前 9浏览
国产化数据库替换最新实践!金融、跨境电商大咖分享,TiDB 社区活动(深圳站)3月1日线下开启!
2025-02-24 9浏览
12月28日,TiDB 社区活动(上海站)走进哔哩哔哩,一起探索国产数据库替换下简化技术栈的收益和实践,限量版 B 站周边等你领
2024-12-13 93浏览
【第八届 TiDB Hackathon】一起来用 TiDB 构建未来的 AI 创新应用, 瓜分超 ¥210,000 奖金池!
2024-07-24 87浏览