理解 MySQL join 语句的执行过程

    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 过程用上了这个索引,因此这个语句的执行流程是这样的:

<

相关推荐

  1. MySQL内部机制:SQL语句执行过程浅析

    2024-03-16 05:00:08       20 阅读
  2. Clickhouse查询语句执行过程

    2024-03-16 05:00:08       37 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-16 05:00:08       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-16 05:00:08       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-16 05:00:08       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-16 05:00:08       18 阅读

热门阅读

  1. kubernetes实战(1)之虚拟机centos搭建k8s集群

    2024-03-16 05:00:08       16 阅读
  2. Spring Cloud Gateway如何实现灰度发布

    2024-03-16 05:00:08       16 阅读
  3. C语言中volatile关键字的用法

    2024-03-16 05:00:08       19 阅读
  4. c++排序算法

    2024-03-16 05:00:08       19 阅读
  5. rsync+inotify-tools文件传输

    2024-03-16 05:00:08       18 阅读
  6. 蓝桥杯---棋盘(典型的二维差分问题)

    2024-03-16 05:00:08       23 阅读
  7. 【kubernetes-kafka】使用kafka client测试

    2024-03-16 05:00:08       21 阅读