not in效率低(MySQL NOT EXIST优化、Not IN优化)

  • 【版权所有,文章允许转载,但须以链接方式注明源地址,否则追究法律责任】
  • 【创作不易,点个赞就是对我最大的支持】

前言

仅作为学习笔记,供大家参考
总结的不错的话,记得点赞收藏关注哦!

原SQL使用的是NOT IN

       select a.id, b.messageId, a.sourceTypeId, a.title, a.messageTime, a.depId, a.status, b.setMark, b.msgType, b.msgRemarks
      from t_sync_data a left join t_message_seting b on a.id = b.messageId where exists (select 1 from t_sync_data where depId=136 and
        status!=4 and status!=2 and status!=5)
        <if test="messageIds != null and messageIds.size() >0"> and a.id NOT IN
            <foreach item="messageId" collection="messageIds" open="(" separator="," close=")">
                #{messageId}
            </foreach>
        </if>
       order by a.messageTime desc, a.id desc, a.status

原因分析:我用了两条sql来解决,第一条sql去查了一组id来排除数据,导致每次查询都要去遍历
建议:能用1条sql出来还是用一条会更快,尽量不要用IN 、NOT IN
1、用 EXISTS 或 NOT EXISTS 代替
2、用JOIN 代替
这里我只查子表主表都存在的数据,故用了内连接,也可以用右连接,需求是以副表为主

新SQL使用的是NOT EXISTS

这里要注意一下:not exists子查询要加一个条件:messageId=a.id 和外边的表关联起来,否则是查不到数据的

select a.id, b.messageId, a.sourceTypeId, a.title, a.messageTime, a.depId, a.status, b.setMark, b.msgType, b.msgRemarks
        from t_sync_data a inner join t_message_seting b on a.id = b.messageId where a.depId=136 and  a.status!=4 and a.status!=2 and a.status!=5
        and not EXISTS (select id
        from t_message_task WHERE  messageId=a.id
        <if test="depId != null ">and depId = #{depId}</if>
         )
        order by a.messageTime desc, a.id desc, a.status 

结果:效率提升十倍不止

创作不易,点个赞就是对我最大的支持~


wxgzh:程序员温眉

CSDN:程序员温眉

每天进步一点点的程序员

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值