MySQL5.6 Semi join优化策略之LooseScan 策略

8月 24, 2014 |

LooseScan 策略

LooseScan 策略是semi join子查询的一种执行策略

比如:

查找拥有卫星的国家(为了简化,不考虑多个国家共同拥有一个卫星),

假设Satellite. country_code有索引,那么我们可以按照国家编码的顺序获取卫星数据:如图:

loosescan-1

LooseScan策略不是真的需要排序,它需要的是分组,在上面的图中,卫星以国家分组,比如澳大利亚拥有的卫星在一块,没有混合其他国家的卫星,这使得从一个组中选择一颗卫星更容易,你可以用其join 国家而不会重复:

loosescan-2

explain select * from Country where Country.code in (select country_code from Satellite);
+---+------------+---------+--------+--------------+-------------+--------+-----------------------+-----+-----------------------+
| id| select_type| table | type | possible_keys| key | key_len| ref | rows| Extra |
+---+------------+---------+--------+--------------+-------------+--------+-----------------------------+-----------------------+
| 1 | PRIMARY |Satellite| index | country_code | country_code| 9 | NULL | 932 | Using index; LooseScan|
| 1 | PRIMARY | Country | eq_ref | PRIMARY | PRIMARY | 3 | Satellite.country_code| 1 | Using index condition |
+---+------------+---------+--------+--------------+-------------+--------+-----------------------+-----+-----------------------+

当然为了显示,我删除了一些列,

looseScan 可以通过设置optimizer_switch变量的loosescan=off标识来关闭loosescan优化
LooseScan 和FirstMatch策略的区别:LooseScan 先扫描内表(In条件中对应表),然后和外表进行join

对于格式为:
SELECT ... FROM outer_tables WHERE expr IN (SELECT ... FROM inner_tables ...)的semi join
如果对该sql 执行Pullout消除子查询后,不能转换为simple 查询类型,如果全扫描out_tables的代价小于全扫描inner_tables,那么本次就使用FirstMatch策略,反之,使用LooseScan策略

参考地址
https://mariadb.com/kb/en/mariadb/mariadb-documentation/optimization-and-tuning/query-optimizations/optimization-strategies/loosescan-strategy/

Posted in: MySQL practise | Tags: , , ,

Comments are closed.