LIKE查詢與索引的不解之謎

  1. like %keyword 索引失效,使用全表掃描。但能夠經過翻轉函數+like前模糊查詢+創建翻轉函數索引=走翻轉函數索引,不走全表掃描。sql

  2. like keyword% 索引有效。函數

  3. like %keyword% 索引失效,也沒法使用反向索引。code

====================================================================索引

查詢%xx的記錄io

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′

    在執行的時候,執行計劃顯示,消耗值,io值,cpu值均很是大,緣由是like後面前模糊查詢致使索引失效,進行全表掃描


**解決方法**: 這種只有前模糊的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′)

    使用翻轉函數+like前模糊查詢+創建翻轉函數索引=走翻轉函數索引,不走全掃描。有效下降消耗值,io值,cpu值這三個指標,尤爲是io值的下降。
相關文章
相關標籤/搜索