【mysql】extra 中的 FirstMatch()解讀


 

Explain  查看mysql執行計划,對於Extra中的 FirstMatch()的解讀:

 

 

參考地址:https://www.cnblogs.com/micrari/p/6921806.html

FirstMatch優化,這也是在處理半連接子查詢時可能會用到的一種優化策略。

 

Demo:

create table department (id int primary key auto_increment); create table employee (id int primary key auto_increment, dep_id int, key(dep_id)); mysql> explain select * from department where id in (select dep_id from employee)\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: department partitions: NULL type: index possible_keys: PRIMARY
          key: PRIMARY key_len: 4 ref: NULL rows: 1 filtered: 100.00 Extra: Using index
*************************** 2. row *************************** id: 1 select_type: SIMPLE table: employee partitions: NULL type: ref possible_keys: dep_id key: dep_id key_len: 5 ref: test.department.id rows: 1 filtered: 100.00 Extra: Using index; FirstMatch(department) 2 rows in set, 1 warning (0.00 sec) mysql> show warnings\G *************************** 1. row ***************************
  Level: Note Code: 1003 Message: /* select#1 */ select `test`.`department`.`id` AS `id` from `test`.`department` semi join (`test`.`employee`) where (`test`.`employee`.`dep_id` = `test`.`department`.`id`) 1 row in set (0.00 sec)

 

我們可以看到上面查詢計划中,兩個id都為1,且extra中列可以看到FirstMatch(department)。MySQL使用了連接來處理此查詢,對於department表的行,只要能在employee表中找到1條滿足即可以不必再檢索employee表。從語義角度來看,和IN-to-EXIST策略轉換為Exist子句是相似的,區別就是FirstMath以連接形式執行查詢,而不是子查詢。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM