亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么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 關注
  • 4408 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號