不走索引查詢的幾種SQL語句


# like 模糊查詢 前模糊或者 全模糊不走索引
explain select * from users u where u.name like '%mysql測試' 
 
# or 條件不走索引,只要有一個條件字段沒有添加索引,都不走,如果條件都添加的索引,也不一定,測試
的時候發(fā)現有時候走,有時候不走,可能數據庫做了處理,具體需要先測試一下
explain select * from users u where u.name = 'mysql測試' or u.password ='JspStudy'
 
# or 條件都是同一個索引字段,走索引
explain  select * from users u where u.name= 'mysql測試' or u.name='333'
 
# 使用 union all 代替 or 這樣的話有索引例的就會走索引
explain
select * from users u where u.name = 'mysql測試' 
union all
select * from users u where u.password = 'JspStudy'
 
# in 走索引
explain select * from users u where u.name in ('mysql測試','JspStudy')
 
# not in 不走索引
explain select * from users u where u.name not in ('mysql測試','JspStudy')
 
# is null 走索引
explain select * from users u where u.name is null 
 
# is not null  不走索引
explain select * from users u where u.name is not null 
 
# !=、<> 不走索引
explain select * from users u where u.name <> 'mysql測試'
 
# 隱式轉換-不走索引(name 字段為 string類型,這里123為數值類型,進行了類型轉換,所以不走索引,改為 '123' 則走索引)
explain select * from users u where u.name = 123
 
# 函數運算-不走索引
explain select *  from users u where  date_format(upTime,'%Y-%m-%d') = '2019-07-01'
# and 語句,多條件字段,最多只能用到一個索引,如果需要,可以建組合索引
explain select * from users where id='4' and username ='JspStudy' 
————————————————
版權聲明:本文為CSDN博主「Muscleheng」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權協(xié)議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/Muscleheng/article/details/94393024
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容