代码改变世界

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

2019-05-20 22:30  abce  阅读(1604)  评论(0编辑  收藏  举报

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执行将如下所示:

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

注意,在应用了“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/