zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    451. Sort Characters By Frequency
    424. Longest Repeating Character Replacement
    68. Text Justification
    44. Wildcard Matching
    160. Intersection of Two Linked Lists
    24. Swap Nodes in Pairs
    93. 递归实现组合型枚举
    98. 分形之城
    97. 约数之和
    96. 奇怪的汉诺塔
  • 原文地址:https://www.cnblogs.com/space-place/p/10035906.html
Copyright © 2011-2022 走看看