天天看點

LooseScan Strategy

Losse_scan/松散掃描:在執行連接配接的時候,半連接配接的表S (R semi-join S)其元組需要有序(a in select b from t,b 上存在索引,其元組的順序按照b成分組狀,則使用b上可用的索引讀取元組的時候,可以按序引序把相同的值的元組有序讀到),此時,根據索引拿出每組重複元組中的第一個元組(其他重複元組被讀到後跳過,是以要求S的元組有序),與R表進行連接配接。[LosseScan:使用索引掃描,基于索引進行分組隻取分組的第一條記錄與外部表進行比對;在EXPLAIN的extra字段顯示LooseScan(m,n)]

select * from Country  
where 
  Country.code in (select country_code from Satellite)           

假設在Satellite.country_code上有一個索引。

LooseScan Strategy

LooseScan政策并不需要排序,它需要的是分組。 在上圖中,衛星按國家分組。 并獲得沒有重複的國家清單:

LooseScan Strategy
[world]> 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 where; Using index; LooseScan |
|  1 | PRIMARY     | Country   | eq_ref | PRIMARY       | PRIMARY      | 3       | world.Satellite.country_code |    1 | Using index condition               |
+----+-------------+-----------+--------+---------------+--------------+---------+------------------------------+------+-------------------------------------+           

LooseScan通過首先放置子查詢表并使用其索引從多個重複項中選擇一個記錄來避免重複記錄組合的産生

是以,為了使LooseScan适用,子查詢應該如下所示:

expr IN (SELECT tbl.keypart1 FROM tbl ...)

expr IN (SELECT tbl.keypart2 FROM tbl WHERE tbl.keypart1=const AND ...)

LooseScan可以處理相關的子查詢

LooseScan可以通過設定optimizer_switch變量中的loosescan = off标志來關閉。

繼續閱讀