bitscn.com
联表查询注意谁是驱动表&你搞不清楚谁join谁更好时请放手让mysql自行判定
写在前面的话:
不要求每个人一定理解 联表查询(join/left join/inner join等)时的mysql运算过程;
不要求每个人一定知道线上(现在或未来)哪张表数据量大,哪张表数据量小;
但把mysql客户端(如sqlyog,如heidisql)放在桌面上,时不时拿出来 explain 一把,这是一种美德!
在实例讲解之前,我们先回顾一下联表查询的基础知识。
——联表查询的基础知识——
引子:为什么第一个查询using temporary,第二个查询不用临时表呢?
下面两个查询,它们只差了一个order by,效果却迥然不同。
第一个查询:
explain extended
select ads.id
from ads, city
where
city.city_id = 8005
and ads.status = 'online'
and city.ads_id=ads.id
order by ads.id desc
执行计划为:
id select_type table type possible_keys key key_len ref rows filtered extra
------ ----------- ------ ------ -------------- ------- ------- -------------------- ------ -------- -------------------------------
1 simple city ref ads_id,city_id city_id 4 const 2838 100.00 using temporary; using filesort
1 simple ads eq_ref primary primary 4 city.ads_id 1 100.00 using where
第二个查询:
explain extended
select ads.id
from ads,city
where
city.city_id =8005
and ads.status = 'online'
and city.ads_id=ads.id
order by city.ads_id desc
执行计划里没有了using temporary:
id select_type table type possible_keys key key_len ref rows filtered extra
------ ----------- ------ ------ -------------- ------- ------- -------------------- ------ -------- ---------------------------
1 simple city ref ads_id,city_id city_id 4 const 2838 100.00 using where; using filesort
1 simple ads eq_ref primary primary 4 city.ads_id 1 100.00 using where
为什么?
dba告诉我们:
mysql 表关联的算法是 nest loop join,是通过驱动表的结果集作为循环基础数据,然后一条一条地通过该结果集中的数据作为过滤条件到下一个表中查询数据,然后合并结果。
explain 结果中,第一行出现的表就是驱动表(important!)
以上两个查询语句,驱动表都是 city,如上面的执行计划所示!
对驱动表可以直接排序,对非驱动表(的字段排序)需要对循环查询的合并结果(临时表)进行排序(important!)
因此,order by ads.id desc 时,就要先 using temporary 了!
驱动表的定义
wwh999 在 2006年总结说,当进行多表连接查询时, [驱动表] 的定义为:
1)指定了联接条件时,满足查询条件的记录行数少的表为[驱动表];
2)未指定联接条件时,行数少的表为[驱动表](important!)。
忠告:如果你搞不清楚该让谁做驱动表、谁 join 谁,请让 mysql 运行时自行判断
既然“未指定联接条件时,行数少的表为[驱动表]”了,
而且你也对自己写出的复杂的 nested loop join 不太有把握(如下面的实例所示),
就别指定谁 left/right join 谁了,
请交给 mysql优化器 运行时决定吧。
如果您对自己特别有信心,可以像火丁一样做优化。
小结果集驱动大结果集
de.cel 在2012年总结说,不管是你,还是 mysql,
优化的目标是尽可能减少join中nested loop的循环次数,
以此保证:
永远用小结果集驱动大结果集(important!)!
——实例讲解——
nested loop join慢查sql语句
先了解一下 mb 表有 千万级记录,mbei 表要少得多。慢查实例如下:
explain
select mb.id, ……
frommb left join mbei on mb.id=mbei.mb_id inner joinu on mb.uid=u.uid
where 1=1
order by mbei.apply_time desc
limit 0,10
够复杂吧。nested loop join 就是这样,
以驱动表的结果集作为循环的基础数据,然后将结果集中的数据作为过滤条件一条条地到下一个表中查询数据,最后合并结果;此时还有第三个表,则将前两个表的 join 结果集作为循环基础数据,再一次通过循环查询条件到第三个表中查询数据,如此反复。
这条语句的执行计划如下:
id select_type table type possible_keys key key_len ref rows extra
------ ----------- ------ ------ -------------- -------------- ------- ------------------- ------- --------------------------------------------
1 simple mb index userid userid 4 (null) 6060455 using index; using temporary; using filesort
1 simple mbei eq_ref mb_id mb_id 4 mb.id 1
1 simple u eq_ref primary primary 4 mb.uid 1 using index
由于动用了“left join”,所以攻城狮已经指定了驱动表,虽然这张驱动表的结果集记录数达到百万级!
.
.
如何优化?
.
.
优化第一步:left join改为join
干嘛要 left join 啊?直接 join!
explain
select mb.id……
from mb join mbei on mb.id=mbei.mb_id inner joinu on mb.uid=u.uid
where 1=1
order by mbei.apply_time desc
limit 0,10
立竿见影,驱动表立刻变为小表 mbei 了, using temporary 消失了,影响行数少多了:
id select_type table type possible_keys key key_len ref rows extra
------ ----------- ------ ------ -------------- ------- ------- ---------------------------- ------ --------------
1 simple mbei all mb_id (null) (null) (null) 13383 using filesort
1 simple mb eq_ref primary,userid primary 4 mbei.mb_id 1
1 simple u eq_ref primary primary 4 mb.uid 1 using index
优化第一步之分支1:根据驱动表的字段排序,好吗?
left join不变。干嘛要根据非驱动表的字段排序呢?我们前面说过“对驱动表可以直接排序,对非驱动表(的字段排序)需要对循环查询的合并结果(临时表)进行排序!”的。
explain
select mb.id……
from mb left join mbei on mb.id=mbei.mb_id inner joinu on mb.uid=u.uid
where 1=1
order by mb.id desc
limit 0,10
也满足业务场景,做到了rows最小:
id select_type table type possible_keys key key_len ref rows extra
------ ----------- ------ ------ -------------- -------------- ------- ------------------- ------ -----------
1 simple mb index userid primary 4 (null) 10
1 simple mbei eq_ref mb_id mb_id 4 mb.id 1 using index
1 simple u eq_ref primary primary 4 mb.uid 1 using index
优化第二步:去除所有join,让mysql自行决定!
写这么多密密麻麻的 left join/inner join 很开心吗?
explain
select mb.id……
from mb,mbei,u
where
mb.id=mbei.mb_id
and mb.uid=u.user_id
order by mbei.apply_time desc
limit 0,10
立竿见影,驱动表一样是小表 mbei:
id select_type table type possible_keys key key_len ref rows extra
------ ----------- ------ ------ -------------- ------- ------- ---------------------------- ------ --------------
1 simple mbei all mb_id (null) (null) (null) 13388 using filesort
1 simple mb eq_ref primary,userid primary 4 mbei.mb_id 1
1 simple u eq_ref primary primary 4 mb.uid 1 using index
最后的总结:
强调再强调:
不要过于相信你的运气!
不要相信你的开发环境里sql的执行速度!
请拿起 explain 武器,
如果你看到以下现象,请优化:
出现了using temporary;
rows过多,或者几乎是全表的记录数;
key 是 (null);
possible_keys 出现过多(待选)索引。
记住,explain 是一种美德!
bitscn.com