Join,left join,right join(1)--连接原理(三十九)

2023-02-01 00:00:00 索引 查询 数据 连接 驱动

前面说了mysql优化器访问数据库的方法有const,ref,ref_or_null,range,index,all。然后又分为条件全部是索引回表查询,和条件有非索引查询,则需要回表之后,在过滤。又有intersection合并索引和union并集索引,当两个单独二级索引查询,不是联合索引查询,可能会触发这两个索引查询,用and是intersection,用or是union查询,触发有两个注重点:

二级索引必须等值匹配,联合索引必须所有值匹配。

主键索引可以范围匹配。

因为二级索引建立在主键索引等值的情况下查询的,二级索引如果是个联合索引,则是按照左原则,一个个排序的,若范围,则不能排序。主键索引可以范围排序的原因,比如查询出等值的二级索引之后,在二级索引的叶子节点直接找到其主键范围的数据,因为数据原本就是按主键排序好的

连接简介

我们先建立两个表:

mysql> create table t1(m1 int,n1 char(1));
Query OK, 0 rows affected (0.15 sec)
 
mysql> create table t2(m2 int,n2 char(1));
Query OK, 0 rows affected (0.04 sec)
 
mysql> insert into t1 values (1,'a'),(2,'b'),(3,'c');
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0
 
mysql> insert into t2 values (2,'b'),(3,'c'),(4,'d');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

相关文章