《Mysql實(shí)例LIKE查詢與索引的不解之謎》要點(diǎn):
本文介紹了Mysql實(shí)例LIKE查詢與索引的不解之謎,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
導(dǎo)讀:like %keyword 索引失效,使用全表掃描.但可以通過翻轉(zhuǎn)函數(shù)+like前模糊查詢+建立翻轉(zhuǎn)函數(shù)索引=走翻轉(zhuǎn)函數(shù)索引,不走全表掃描.like key...
like %keyword 索引失效,使用全表掃描.但可以通過翻轉(zhuǎn)函數(shù)+like前模糊查詢+建立翻轉(zhuǎn)函數(shù)索引=走翻轉(zhuǎn)函數(shù)索引,不走全表掃描.
like keyword% 索引有效.
like %keyword% 索引失效,也無法使用反向索引.
====================================================================
查詢%xx的記錄
??? select count(c.c_ply_no) as COUNT
??? from Policy_Data_All c, Item_Data_All i
??? where c.c_ply_no = i.c_ply_no
??? and i.C_LCN_NO like ’%245′
??? 在執(zhí)行的時(shí)候,執(zhí)行計(jì)劃顯示,消耗值,io值,cpu值均非常大,原因是like后面前模糊查詢導(dǎo)致索引失效,進(jìn)行全表掃描
**解決方法**: 這種只有前模糊的sql可以改造如下寫法
??? select count(c.c_ply_no) as COUNT
??? from Policy_Data_All c, Item_Data_All i
??? where c.c_ply_no = i.c_ply_no
??? and reverse(i.C_LCN_NO) like reverse(‘%245′)
??? 使用翻轉(zhuǎn)函數(shù)+like前模糊查詢+建立翻轉(zhuǎn)函數(shù)索引=走翻轉(zhuǎn)函數(shù)索引,不走全掃描.有效降低消耗值,io值,cpu值這三個(gè)指標(biāo),尤其是io值的降低.
轉(zhuǎn)載請(qǐng)注明本頁網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/5813.html