44其他问题
create table a(f1 int, f2 int, index(f1))engine=innodb;
create table b(f1 int, f2 int)engine=innodb;
insert into a values(1,1),(2,2),(3,3),(4,4),(5,5),(6,6);
insert into b values(3,3),(4,4),(5,5),(6,6),(7,7),(8,8);
select * from a left join b on(a.f1=b.f1) and (a.f2=b.f2); /Q1/ 结果集6行
select * from a left join b on(a.f1=b.f1) where (a.f2=b.f2);/Q2/ 结果集4行
where (a.f2=b.f2) 表示过滤掉为NULL的行,应为NULL跟任何职判断等值和不等结果都是NULL
这样left join语义就是找到这两个表里面,f1、f2 对应相同的行。对于表a中存在,而表b中匹配不到的行,就放弃。
这样,这条语句虽然用的是left join,但是语义跟join是一致的。因此,优化器就把这条语句的left join改写成了join,然后因为表a的f1上有索引,就把表b作为驱动表,这样就可以用上NLJ算法。
需要left join的语义,就不能把被驱动表的字段放在where条件里面做等值判断或不等值判断,必须都写在on里面。