mysql match_MySQL子查询优化 -- FirstMatch策略

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

20190520224700756104.png

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

20190520224700862542.png

注意,在应用了“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设置的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值