Mysql 关于not exists一例

  场景:

  业务上需要做一个查询,因为是Web及时响应,所以对响应时间有要求,原业务场景是需要从无库存订单中剔除绑定闲置库存,因单条sql查询实现复杂,故考虑用差集方式:

select a.col1, a.col2
from a
where a.id = ?
and not exists (
 select b.id 
from b left join c on b.id = c.id
where b.id = ?
and b.id = a.id)
order by a.id  

   数据量: a,b,c皆在百万数据量级,排除其他非必要过滤字段,id皆有btree索引

   运行:2s左右

     环境:阿里云(最基本线上服务性能,数据库运行状态保持在10个以上链接)

  分析:

   原sql其实用的是not in,参考了文章1,在同等数据量时not in 会走多次全表查询(因为!=无对应索引),而not exists会走子查询索引,所以not exists更快。故先用not exists替换了not in(语法有差异,替换时需要做b.id = a.id的关联)。在参考文章2以后,尝试用left join进行优化(其中关于mysql子查询优化器说法待考量,后分析文章3),改成如下方式:

select a.col1, a.col2
from a
left join ( select b.id 
from b left join c on b.id = c.id
where b.id = ?
) as r on a.id = r.id
where b.id = ?
and r.id is null
order by a.id

   思路便是先将子查询符合的行通过left join查询到,然后通过is null条件得到剩下的部分(即满足需求的记录)。最终运行时间在0.7s。

 

 

ref:

1.  https://www.cnblogs.com/beijingstruggle/p/5885137.html

2. https://blog.csdn.net/zyz511919766/article/details/49335647

3. https://www.cnblogs.com/wxw16/p/6105624.html?utm_source=itdadao&utm_medium=referral

 

posted @ 2018-11-29 00:21  Splace  阅读(625)  评论(0编辑  收藏  举报