首页 技术 正文
技术 2022年11月15日
0 收藏 558 点赞 4,743 浏览 2743 个字

FirstMatch执行semi-join子查询的一种策略。

类似于MySQL 5.x中如何执行in、exists子查询。

让我们以搜索拥有大城市的国家为例:

select * from Country
where Country.code IN (select City.Country
from City
where City.Population > 1*1000*1000)
and Country.continent='Europe';

假设,我们的执行计划是找到欧洲的国家,然后,对于每个找到的国家,检查它是否有大城市。常规的inner join执行将如下所示:

semi-join子查询优化 — FirstMatch策略

由于德国有两个大城市(在该图中),所以它将被两次放入查询输出中。这是不正确的,select … from country不应产生相同的国家记录两次。First Match策略在找到第一个真正的匹配后,通过缩短执行来避免产生重复:

semi-join子查询优化 — FirstMatch策略

注意,在应用了“Using where”之后,才进行short-cutting操作。在找到Tirer之后就进行short-cutting操作肯定是错误的。

上面的查询使用First Match之后,执行计划类似如下:

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中的非常相似:

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设置的。

https://mariadb.com/kb/en/library/firstmatch-strategy/

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,499
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,913
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,746
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,503
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,142
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,305