Three ways to join
Simple Nested-Loop Join
The least efficient mysql is not used
Block Nested-Loop Join
show variables like '%join_buffer%'
Index Nested-Loop Join
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)
select * from t1 straight_join t2 on (t1.a=t2.b);
Assume that the number of rows in the drive table is M, so it is necessary to scan N rows of the drive table
The number of rows in the driven table is N. Each time a row of data is looked up in the driven table, the index a is searched first, and then the primary key index is searched. The approximate complexity of each search for a tree is the logarithm of N base 2, so the time complexity of looking up a row in the driven table is 2*
Each row of data in the driving table must be searched on the driven table once, and the approximate complexity of the entire execution process is M + M ∗ 2 ∗ log 2 NM + M*2*log2^NM+M∗2∗log2N
Obviously M has a greater impact on the number of scanned rows, so a small table should be used as the driving table. Of course, the premise of this conclusion is that the index of the driven table can be used
In the explain result, does the word "Block Nested Loop" appear in the extra field?
Small table as driving table
Increase the size of join_buffer_size
Reference blog
[1]