MySQL中inner join/left join连表查询的查询速度

通过对比inner join/left join多表链接查询,分析两个语句的查询速度:

a表:5000万条(id主键)

b表:1000万条

c表:2000万条

d表:2000条

表内id均建立索引

查询结果:一致

查询速度:inner join查询时间222秒;left join 查询时间454秒

通俗原因:inner join为查询交集,查询结果较少;left join 为并集查询,查询结果较多

学艺不精,请指正!!

left join 查询优势,可利用其来进行去重处理,减少使用not in(子查询):

select *

from a left join b on a.id = b.id

where b.id is null

#代码1:
#通过inner join 链接
select d.name,COUNT(a.re_id)
from
	a 
	INNER JOIN b on a.id = b.id
	INNER JOIN c on a.id = c.id
	INNER JOIN d on concat(left(a.re_id,4),'00') = d.CODE
WHERE
	(c.r_1 > 0 or c.r_2 >0)
GROUP BY LEFT(a.re_id,4)
;


#代码2:
#通过left join 链接
select d.name,COUNT(a.re_id)
from
	a 
	LEFT JOIN b on a.z_id = b.id
	LEFT JOIN c on a.z_id = c.id
	INNER JOIN d on concat(left(a.re_id,4),'00') = d.CODE
WHERE
	b.id IS not NULL
	AND
	c.id IS not NULL
	AND
	(c.r_1 > 0 or c.r_2 >0)
GROUP BY LEFT(a.re_id,4)
;

#left join 去重代码
select *
from a left join b on a.id = b.id
where b.id is null

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值