join
Index Nested-Loop Join
Simple Nested-Loop Join
Block Nested-Loop Join
join_buffer_size
Multi-Range Read
read_rnd_buffer_size
set optimizer_switch="mrr_cost_based=off"
Batched Key Access
set optimizer_switch='mrr=on,mrr_cost_based=off,batched_key_access=on';
hash join
先创建两个表 t1 和 t2 来做说明
CREATE TABLE `t2` (
`id` int(11) NOT NULL,
`a` int(11) DEFAULT NULL,
`b` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `a` (`a`)
) ENGINE=InnoDB;
drop procedure idata;
delimiter ;;
create procedure idata()
begin
declare i int;
set i=1;
while(i<=1000)do
insert into t2 values(i, i, i);
set i=i+1;
end while;
end;;
delimiter ;
call idata();
create table t1 like t2;
insert into t1 (select * from t2 where id<=100)
这两个表都有一个主键索引 id 和一个索引 a,字段 b 上无索引。存储过程 idata() 往表 t2 里插入了 1000 行数据,在表 t1 里插入的是 100 行数据。
Join 的两种算法
Index Nested-Loop Join
select * from t1 straight_join t2 on (t1.a=t2.a);
这条语句的 explain 结果
在这条语句里,被驱动表 t2 的字段 a 上有索引,join 过程用上了这个索引,因此这个语句的执行流程是这样的:
<