【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 warningsG
*************************** 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以连接形式执行查询,而不是子查询。

 

原文地址:https://www.cnblogs.com/sxdcgaq8080/p/12605539.html