semi-join子查询优化 -- FirstMatch策略
2019-05-20 22:30 abce 阅读(1764) 评论(0) 收藏 举报FirstMatch执行semi-join子查询的一种策略。
类似于MySQL 5.x中如何执行in、exists子查询。
让我们以搜索拥有大城市的国家为例:
1 2 3 4 5 | select * from Country where Country.code IN ( select City.Country from City where City.Population > 1*1000*1000) and Country.continent= 'Europe' ; |
假设,我们的执行计划是找到欧洲的国家,然后,对于每个找到的国家,检查它是否有大城市。常规的inner join执行将如下所示:
由于德国有两个大城市(在该图中),所以它将被两次放入查询输出中。这是不正确的,select … from country不应产生相同的国家记录两次。First Match策略在找到第一个真正的匹配后,通过缩短执行来避免产生重复:
注意,在应用了“Using where”之后,才进行short-cutting操作。在找到Tirer之后就进行short-cutting操作肯定是错误的。
上面的查询使用First Match之后,执行计划类似如下:
1 2 3 4 5 6 7 8 | MariaDB [world]> explain select * from Country where Country.code IN ( select City.Country from City where City.Population > 1*1000*1000) and Country.continent= 'Europe' ; + ----+-------------+---------+------+--------------------+-----------+---------+--------------------+------+----------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | + ----+-------------+---------+------+--------------------+-----------+---------+--------------------+------+----------------------------------+ | 1 | PRIMARY | Country | ref | PRIMARY ,continent | continent | 17 | const | 60 | Using index condition | | 1 | PRIMARY | City | ref | Population,Country | Country | 3 | world.Country.Code | 18 | Using where ; FirstMatch(Country) | + ----+-------------+---------+------+--------------------+-----------+---------+--------------------+------+----------------------------------+ 2 rows in set (0.00 sec) |
extra列中的FirstMatch(Country)表示,一旦生成了一个匹配的记录组合,就可以short-cutting过程就执行并跳转回Country表。
FirstMatch的查询计划与MySQL中的非常相似:
1 2 3 4 5 6 7 8 | MySQL [world]> explain select * from Country where Country.code IN ( select City.Country from City where City.Population > 1*1000*1000) and Country.continent= 'Europe' ; + ----+--------------------+---------+----------------+--------------------+-----------+---------+-------+------+------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | + ----+--------------------+---------+----------------+--------------------+-----------+---------+-------+------+------------------------------------+ | 1 | PRIMARY | Country | ref | continent | continent | 17 | const | 60 | Using index condition; Using where | | 2 | DEPENDENT SUBQUERY | City | index_subquery | Population,Country | Country | 3 | func | 18 | Using where | + ----+--------------------+---------+----------------+--------------------+-----------+---------+-------+------+------------------------------------+ 2 rows in set (0.01 sec) |
FirstMatch策略背后的思想和in->exists转换思想相同。然而,FirstMatch有以下的优点:
1.等价传播可以跨越semi-join边界,但是不能跨越子查询边界。因此,使用FirstMatch将子查询转换成semi-join可以提供一个更好的执行计划
2.只有一种方式使用in->exists策略,mysql会无条件地使用。对于FirstMatch策略,优化器可以选择是否应该在子查询中使用的所有表都位于join前缀时运行FirstMatch策略,或者在稍后的某个时间点运行FirstMatch策略
FirstMatch策略意味着子查询的表必须在父查询中的表之后被引用;
FirstMatch支持相关子查询;
不能应用于子查询带有group by或聚合函数的场景;
是否开启FirstMatch是由系统变量optimizer_switch中的firstmatch=on|off设置的。
【推荐】FlashTable:表单开发界的极速跑车,让你的开发效率一路狂飙
【推荐】Flutter适配HarmonyOS 5知识地图,实战解析+高频避坑指南
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步