技术社群的这篇文章《第 55 期:树状格式的执行计划》给我们讲解了MySQL数据库树状格式的执行计划,可读性更强,对了解SQL执行路径有更好地帮助。
MySQL 也有棵树
MySQL 的执行计划有一种 树状格式,也就是 EXPLAIN FORMAT=TREE
。
这种格式类似传统商业数据库比如 Oracle 的执行计划输出结果。以树形结果分层输出、缩进展示、极简主义,只输出主要的执行计划结果,并以文字形式做概要描述。
对比传统格式以及 JSON 格式来讲,能更加直观地看到语句优化后的效果。
* 代码段中的树状格式建议在 PC 浏览器下阅读。
到底有多好用?
接下来用从几个示例来说明树形格式执行计划的便捷性(一共 8 条 SQL 示例)。
需要解析的 SQL 语句列表:
SQL 1: table t1
SQL 2: table t1 limit 10
SQL 3: select * from t1 a where r1 = 10
SQL4: selectcount(*) from t1 a where r1 in (select r1 from t1 b)
SQL5: select * from t1 a where r1 in (select r1 from t1 b where r1 in (select r1 from t1 c where r1 in (select r1 from t1 d wherer1 in (select r1 from t1 e))))
SQL6: select * from t1 leftjoin t2 using(r1)
SQL7: select a.* from t1 a, t2 b where a.r3 = b.r3
SQL8: select * from t1 where r1=10and r2 =10
SQL 1
执行计划解读:表 t1
全表扫描,成本数据,扫描总记录数一目了然。
mysql:ytt>desc format=tree table t1\G
*************************** 1. row ***************************
EXPLAIN: -> Table scan on t1 (cost=265841.68 rows=2552706)
1 row in set (0.00 sec)
SQL 2
执行计划解读:表 t1
全表扫描,仅仅返回有限的记录数。 可以看到,加 LIMIT
和不加 LIMIT
的 SQL 执行计划成本一样。
TIPS:MySQL 对于 LIMIT 检索只是在 SERVER 端返回给客户端时进行目标记录的提前截取。
mysql:ytt>desc format=tree table t1 limit 10\G
*************************** 1. row ***************************
EXPLAIN: -> Limit: 10 row(s) (cost=265841.68 rows=10)
-> Table scan on t1 (cost=265841.68 rows=2552706)
1 row in set (0.00 sec)
SQL 3
执行计划解读:简单等值过滤检索,结果显示基于索引 idx_r1
索引查找,过滤条件为 r1 = 10
,经过简单计算后,成本与全表扫占比为 1:7,扫描记录数对比为 1:52。这样的结果比之前两种格式输出来的更加直接易读。
mysql:ytt>desc format=tree select * from t1 a where r1 = 10\G
*************************** 1. row ***************************
EXPLAIN: -> Index lookup on a using idx_r1 (r1=10) (cost=36558.05 rows=48448)
1 row in set (0.00 sec)
SQL 4
执行计划解读:简单子查询语句,目的是求表 t1
总记录数。
这类 SQL 写法有很大问题,本篇不关注语句本身业务逻辑,只分析执行计划,后期会专门开篇讲如何从写法上避免 SQL 性能太差。
mysql:ytt>desc format=tree select count(*) from t1 a where r1 in (select r1 from t1 b)\G
*************************** 1.row ***************************
EXPLAIN: -> Aggregate: count(0) (cost=1303263126712.80rows=6516307922436)
-> Nestedloopinnerjoin (cost=651632334469.20rows=6516307922436)
-> Filter: (a.r1 isnotnull) (cost=265872.60rows=2552706)
-> Indexscanon a using idx_r1 (cost=265872.60rows=2552706)
-> Single-rowindex lookup on <subquery2> using <auto_distinct_key> (r1=a.r1)
-> Materialize with deduplication (cost=521143.20..521143.20rows=2552706)
-> Filter: (b.r1 isnotnull) (cost=265872.60rows=2552706)
-> Indexscanon b using idx_r1 (cost=265872.60rows=2552706)
1rowinset (0.00 sec)
总成本:1303263126712.80 总扫行数:6516307922436
可以看到,默认执行计划并非很优化。步骤为全索引扫表 b
,并且去重物化为内部临时表,主键为字段 r1
,完了再用表 a
和临时表做嵌套循环内联,最终得出聚合结果,但这样效率会非常差。
尝试关闭优化器物化再看效果。
mysql:ytt>set @@optimizer_switch='materialization=off';
Query OK, 0 rows affected (0.00 sec)
mysql:ytt>desc format=tree selectcount(*) from t1 a where r1 in (select r1 from t1 b)\G
*************************** 1.row ***************************
EXPLAIN: -> Aggregate: count(0) (cost=46794548.57rows=2552706)
-> Nestedloopinnerjoin (cost=46539277.96rows=2552706)
-> Remove duplicates frominput sorted on idx_r1 (cost=1761.28rows=100)
-> Filter: (b.r1 isnotnull) (cost=1761.28rows=100)
-> Indexscanon b using idx_r1 (cost=1761.28rows=2552706)
-> Index lookup on a using idx_r1 (r1=b.r1) (cost=46284798.78rows=25527)
1rowinset (0.01 sec)
少了物化后,基于索引扫描表 b
并且去重,r1
唯一值只有 100 行(这也就是拿这种字段做 JOIN 性能差的原因),完了和表 a
做嵌套循环内联。最终聚合成本为 46794548.57, 扫描行数为 2552706,比做物化成本要低很多。
mysql:ytt>select count(*) from t1 a where r1 in (select r1 from t1 b);
+----------+
| count(*) |
+----------+
| 2560000 |
+----------+
1 row in set (1.61 sec)
mysql:ytt>set @@optimizer_switch='materialization=off';
Query OK, 0 rows affected (0.00 sec)
mysql:ytt>selectcount(*) from t1 a where r1 in (select r1 from t1 b);
+----------+
| count(*) |
+----------+
| 2560000 |
+----------+
1 row in set (1.11 sec)
执行时间对比后,和预期一致。
SQL 5
执行计划解读: 表 t1
嵌套查询,被嵌套了 4 次。执行计划和上面例子一样,不同的是嵌套次数比较多。从执行计划里可以看出分层结构,由底向上执行,表 b
分别和表 c
、表 d
、表 e
做完内联后的记录再物化、去重,完了表 a
再和物化表内联。
每个小步骤都有对应的成本和记录数据,一目了然。不过从最终成本来看这个查询性能更差。当然这类 SQL 也可以得到很好的优化,不过前提是理解业务逻辑。
mysql:ytt>desc format=tree select * from t1 a where r1 in (select r1 from t1 b where r1 in (select r1 from t1 c where r1 in (select r1 from t1 d wherer1 in (select r1 from t1 e))))\G
*************************** 1.row ***************************
EXPLAIN: -> Nestedloopinnerjoin (cost=5419684783161168000000000.00rows=-9223372036854775808)
-> Filter: (a.r1 isnotnull) (cost=257921.10rows=2552706)
-> Tablescanon a (cost=257921.10rows=2552706)
-> Single-rowindex lookup on <subquery2> using <auto_distinct_key> (r1=a.r1)
-> Materialize with deduplication (cost=4276470460722807000.00..4276470460722807000.00rows=-9223372036854775808)
-> Filter: (b.r1 isnotnull) (cost=2153356877270122500.00rows=-9223372036854775808)
-> Nestedloopinnerjoin (cost=2153356877270122500.00rows=-9223372036854775808)
-> Nestedloopinnerjoin (cost=84355849420043.78rows=831710952208555)
-> Nestedloopinnerjoin (cost=3304501419.99rows=32581540310)
-> Filter: (b.r1 isnotnull) (cost=65142.90rows=1276353)
-> Indexrangescanon b using idx_r1 (cost=65142.90rows=1276353)
-> Index lookup on c using idx_r1 (r1=b.r1) (cost=18.13rows=25527)
-> Index lookup on d using idx_r1 (r1=b.r1) (cost=18.13rows=25527)
-> Index lookup on e using idx_r1 (r1=b.r1) (cost=18.13rows=25527)
1rowinset (0.00 sec)
SQL 6
执行计划解读:表 t1
左外连接表 t2
,执行计划显示 Nested loop left join,t1
全表扫,t2
索引扫。
mysql:ytt>desc format=tree select * from t1 leftjoin t2 using(r1)\G
*************************** 1.row ***************************
EXPLAIN: -> Nestedloopleftjoin (cost=1159286.21rows=2552706)
-> Tablescanon t1 (cost=265839.11rows=2552706)
-> Index lookup on t2 using idx_r1 (r1=t1.r1) (cost=0.25rows=1)
1rowinset (0.00 sec)
SQL 7
执行计划解读: 来看一个哈希 JOIN 的例子。表 t1
和表 t2
做内联,由于两表字段 r3
都没有索引,MySQL 选择小表 t2
做哈希表(记录数 1W 行,表 t1 记录数 255W 行),表 t1 作为哈希探测表。
mysql:ytt>desc format=tree select a.* from t1 a, t2 b where a.r3 = b.r3\G
*************************** 1.row ***************************
EXPLAIN: -> Innerhashjoin (a.r3 = b.r3) (cost=2595860374.91rows=2595846770)
-> Tablescanon a (cost=3.75rows=2552706)
-> Hash
-> Tablescanon b (cost=1025.15rows=10169)
1rowinset (0.00 sec)
SQL 8
执行计划解读: 经典索引合并计划,合并索引为 idx_r1,idx_r2(using intersect(idx_r1,idx_r2))
。
mysql:ytt>desc format=tree select * from t1 where r1=10and r2 =10\G
*************************** 1.row ***************************
EXPLAIN: -> Filter: ((t1.r2 = 10) and (t1.r1 = 10)) (cost=1635.47rows=1854)
-> Indexrangescanon t1 usingintersect(idx_r1,idx_r2) (cost=1635.47rows=1854)
1rowinset (0.00 sec)
SQL 8 这类语句最优化的还是得建立组合索引,下面是有组合索引后的执行计划:成本以及扫描行数都比合并索引要低很多,那么效率自然会高很多。
mysql:ytt>desc format=tree select * from t1 where r1=10 and r2 =10\G
*************************** 1. row ***************************
EXPLAIN: -> Index lookup on t1 using idx_u1 (r1=10, r2=10) (cost=490.62 rows=544)
1 row in set (0.00 sec)
如果您认为这篇文章有些帮助,还请不吝点下文章末尾的"点赞"和"在看",或者直接转发朋友圈,

微信公众号:@bisal的个人杂货铺 腾讯云开发者社区:@bisal的个人杂货铺 头条号:@bisal的个人杂货铺 CSDN:@bisal ITPub:@bisal 墨天轮:@bisal 51CTO:@bisal 小红书:@bisal 抖音:@bisal




