关于mysql联表的内嵌循环操作nested loop join中on和where执行顺序问题

譬如语句
select * from table_a join table_b on table_a.id=table_b.a_id and table_a.name='smith' join table_c where table_a.priority=3 and table_b.seq=8 and table_c.sales=1998;

在联表内嵌循环操作中,假如table_a是驱动表

1) table_a 根据on条件中的table_a.name='smith'过滤后的结果集做第一层循环集,依次类推,等到a,b,c联表后生成的临时表再针对where语句中的条件进行过滤
2) table_a 根据on条件中的table_a.name='smith'以及where条件中的table_a.priority=3过滤后的结果集第一层循环集,依次类推

其实就是理解on和where的执行顺序
哪种是对的?

阅读 2.2k
1 个回答

mysql的理论依据没找到,个人理解是先执行where的过滤条件,先关联再过滤明显做的是无用功。

oracle中倒是能在执行计划中看到,先执行的是过滤条件(下面代码中最后一行)。

explain plan for SELECT * FROM tmp_t2 t2 LEFT JOIN tmp_t1  t1 ON t2.id = t1.id AND t1.good_id = 'A'

-----------------------------------------------------------------------------
| Id  | Operation          | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |        |     3 |   147 |     6   (0)| 00:00:01 |
|*  1 |  HASH JOIN         |        |     3 |   147 |     6   (0)| 00:00:01 |
|   2 |   TABLE ACCESS FULL| TMP_T2 |     3 |    60 |     3   (0)| 00:00:01 |
|*  3 |   TABLE ACCESS FULL| TMP_T1 |     3 |    87 |     3   (0)| 00:00:01 |
-----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - access("T2"."ID"="T1"."ID")
   3 - filter("T1"."GOOD_ID"='A')
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题