当前位置: 移动技术网 > IT编程>数据库>Mysql > MySQL的LEFT JOIN表连接的进阶学习教程

MySQL的LEFT JOIN表连接的进阶学习教程

2017年12月12日  | 移动技术网IT编程  | 我要评论

left join的主表

这里所说的主表是指在连接查询里mysql以哪个表为主进行查询。比如说在left join查询里,一般来说左表就是主表,但这只是经验之谈,很多时候经验主义是靠不住的,为了说明问题,先来个例子,建两个演示用的表categories和posts:

create table if not exists `categories` (
`id` int(10) unsigned not null auto_increment,
`name` varchar(15) not null,
`created` datetime not null,
primary key (`id`)
);

create table if not exists `posts` (
`id` int(10) unsigned not null auto_increment,
`category_id` int(10) unsigned not null,
`title` varchar(100) not null,
`content` varchar(200) not null,
`created` datetime not null,
primary key (`id`),
key `category_id` (`category_id`)
);

先注意一下每个表的索引情况,以后会用到,记得随便插入一点测试数据,不用太多,但怎么也得两行以上,然后执行以下sql:

explain select *
from posts
left join categories on posts.category_id = categories.id
where categories.id = ‘一个已经存在的id'
order by posts.created desc

table   key     extra
categories primary   using filesort
posts   category_id using where

在explain的结果中,第一行表示的表就是主表,所以说在此查询里categories是主表,而在我们的经验里,left join查询里,左表(posts表)才应该是主表,这产生一个根本的矛盾,mysql之所以这样处理,是因为在我们的where部分,查询条件是按照categories表的字段来进行筛选的,而恰恰categories表存在合适的索引,所以在查询时把categories表作为主表更有利于缩小结果集。

那explain结果中的using filesort又是为什么呢?这是因为主表是categories表,从表是posts表,而我们使用从表的字段去order by,这通常不是一个好选择,最好改成主表字段,如果鉴于需求所限,无法改成主表的字段,那么可以尝试添加如下索引:

alter table `posts` add index ( `category_id` , `created` );

再运行sql时就不会有using filesort了,这是因为主表categories在通过category_id连接从表posts时,可以进而通过索引直接得到排序后的posts结果。

主观上一旦搞错了主表,可能怎么调整索引都得不到高效的sql,所以在写sql时,比如说在写left join查询时,如果希望左表是主表,那么就要保证在where语句里的查询条件尽可能多的使用左表字段,进而,一旦确定了主表,也最好只通过主表字段去order by。

left join查询效率分析
user表:

id | name
---------
1 | libk
2 | zyfon
3 | daodao

user_action表:

user_id | action
---------------
1 | jump
1 | kick
1 | jump
2 | run
4 | swim

sql:

select id, name, action from user as u
left join user_action a on u.id = a.user_id

result:
id | name | action
--------------------------------
1 | libk | jump ①
1 | libk | kick ②
1 | libk | jump ③
2 | zyfon | run ④
3 | daodao | null ⑤

分析:
注意到user_action中还有一个user_id=4, action=swim的纪录,但是没有在结果中出现,
而user表中的id=3, name=daodao的用户在user_action中没有相应的纪录,但是却出现在了结果集中
因为现在是left join,所有的工作以left为准.
结果1,2,3,4都是既在左表又在右表的纪录,5是只在左表,不在右表的纪录


结论:
我们可以想象left join 是这样工作的
从左表读出一条,选出所有与on匹配的右表纪录(n条)进行连接,形成n条纪录(包括重复的行,如:结果1和结果3),
如果右边没有与on条件匹配的表,那连接的字段都是null.
然后继续读下一条。

引申:
我们可以用右表没有on匹配则显示null的规律, 来找出所有在左表,不在右表的纪录, 注意用来判断的那列必须声明为not null的。
如:

select id, name, action from user as u
left join user_action a on u.id = a.user_id
where a.user_id is null

(注意:1.列值为null应该用is null 而不能用=null
2.这里a.user_id 列必须声明为 not null 的)

result:
id | name | action
--------------------------
3 | daodao | null

--------------------------------------------------------------------------------

tips:
1. on a.c1 = b.c1 等同于 using(c1)
2. inner join 和 , (逗号) 在语义上是等同的
3. 当 mysql 在从一个表中检索信息时,你可以提示它选择了哪一个索引。
如果 explain 显示 mysql 使用了可能的索引列表中错误的索引,这个特性将是很有用的。
通过指定 use index (key_list),你可以告诉 mysql 使用可能的索引中最合适的一个索引在表中查找记录行。
可选的二选一句法 ignore index (key_list) 可被用于告诉 mysql 不使用特定的索引。
4. 一些例子:

mysql> select * from table1,table2 where table1.id=table2.id;
mysql> select * from table1 left join table2 on table1.id=table2.id;
mysql> select * from table1 left join table2 using (id);
mysql> select * from table1 left join table2 on table1.id=table2.id
-> left join table3 on table2.id=table3.id;
mysql> select * from table1 use index (key1,key2)
-> where key1=1 and key2=2 and key3=3;
mysql> select * from table1 ignore index (key3)
-> where key1=1 and key2=2 and key3=3;

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网