为了账号安全,请及时绑定邮箱和手机立即绑定

为什么mysql在where条件后出现了or索引就失效了,怎么改?

为什么mysql在where条件后出现了or索引就失效了,怎么改?

KevenHuang 2017-10-23 11:11:03
我的sql语句中where条件是这样的,where p1.status=1 or p3.status=3这时候p1表的字段status就没有使用索引,但是去掉or改成p1.status=1或者p1.status=3索引有效,但是我的业务是需要状态值为1和3的记录的,我想到用in,测试也是不使用索引,所以不知道要怎么改这个条件才能用p1的status用到索引。请各位不吝赐教,谢谢。
查看完整描述

2 回答

已采纳
?
是王小二呀

TA贡献88条经验 获得超19个赞

看一下union

查看完整回答
反对 回复 2017-10-23
  • KevenHuang
    KevenHuang
    EXPLAIN select p1.*,p2.username,p8.portion_wait,p8.account_min,p8.recover_wait,p4.name as type_name,p4.title as type_title,p5.name as style_name,p5.title as style_title,p9.name as qudao_name,p10.ord_status,p10.verify_type from `diyou_borrow` as p1 left join diyou_borrow_type as p4 on p1.borrow_type=p4.nid left join diyou_borrow_style as p5 on p1.borrow_style=p5.nid left join diyou_users as p2 on p1.user_id=p2.user_id left join diyou_borrow_roam as p8 on p1.borrow_nid=p8.borrow_nid left join diyou_linkages as p9 on p1.qudao_type=p9.value left join diyou_bank_loan as p10 on p1.borrow_nid=p10.borrow_nid && p10.inner_status=0 where 1=1 and (p1.status=1 or p1.status=3) and p9.type_id=96具体语句是这样的,因为or的存在,所以没用索引
  • 是王小二呀
    是王小二呀
    要么做成子查询,要么用union all替换or
?
孔明_雨3221376

TA贡献2条经验 获得超1个赞

把or 换成union

查看完整回答
反对 回复 2017-11-13
  • 2 回答
  • 0 关注
  • 4255 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信