国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

解讀Oracle中代替like進(jìn)行模糊查詢的方法instr(更高效)

瀏覽:24日期:2023-03-12 15:25:40
目錄
  • 一、簡介
  • 二、使用說明
    • 對應(yīng)參數(shù)描述
    • 我們以一些示例講解使用方法
  • 三、instr()與like比較
    • instr函數(shù)也有三種情況
    • 下面通過一個示例說明like 與 instr()的使用比較
  • 四、效率對比
    • 五、總結(jié)

      一、簡介

      相信大家都使用過like進(jìn)行模糊匹配查詢,在oracle中,instr()方法可以用來代替like進(jìn)行模糊查詢,大數(shù)據(jù)量的時候效率更高。

      本文將對instr()的基本使用方法進(jìn)行詳解以及通過示例講解與like的效率對比。

      二、使用說明

      instr(sourceString,destString,start,appearPosition) ? ??

      對應(yīng)參數(shù)描述

      instr('源字符串' , '目標(biāo)字符串' ,'開始位置','第幾次出現(xiàn)'),返回目標(biāo)字符串在源字符串中的位置。

      后面兩個參數(shù)可要可不要。

      我們以一些示例講解使用方法

      【a】從開頭開始查找第一個‘h’出現(xiàn)的位置

      --從開頭開始查找第一個‘h"出現(xiàn)的位置select instr("zhangsan", "h") as idx from dual; --2

      查詢結(jié)果:

      【b】從開頭開始查找‘an’在字符串中的位置

      --從開頭開始查找‘a(chǎn)n"在字符串中的位置select instr("zhangsan","an") idx from dual; --3

      查詢結(jié)果:

      【c】從第一個位置開始查找,返回第二次出現(xiàn)‘a’的位置

      --從第一個位置開始查找,返回第二次出現(xiàn)‘a(chǎn)"的位置select instr("zhangsan","a",1,"2") idx from dual; --7

      查詢結(jié)果:

      【d】從倒數(shù)第一個位置開始,查找第一次出現(xiàn)‘a’的位置

      --從倒數(shù)第一個位置開始,查找第一次出現(xiàn)‘a(chǎn)"的位置select instr("zhangsan","a",-1,1) idx from dual;  --7

      查詢結(jié)果:

      【e】從倒數(shù)第一個位置開始,返回第二次出現(xiàn)‘a’的位置

      --從倒數(shù)第一個位置開始,返回第二次出現(xiàn)‘a(chǎn)"的位置select instr("zhangsan","a",-1,2) idx from dual;   --3

      查詢結(jié)果:

      三、instr()與like比較

      instr函數(shù)也有三種情況

      • a. instr(字段,'關(guān)鍵字') > 0 相當(dāng)于 字段like '%關(guān)鍵字%': 表示在字符串中包含‘關(guān)鍵字’
      • b. instr(字段,'關(guān)鍵字') = 1 相當(dāng)于 字段like '關(guān)鍵字%' 表示以‘關(guān)鍵字’開頭的字符串
      • c. instr(字段,'關(guān)鍵字') = 0 相當(dāng)于 字段not like '%關(guān)鍵字%' 表示在字符串中不包含‘關(guān)鍵字’

      下面通過一個示例說明like 與 instr()的使用比較

      【a】使用like進(jìn)行模糊查詢

      with temp1 as (select "zhangsan" as name from dual),temp2 as (select "zhangsi" as name from dual),temp3 as (select "xiaoming" as name from dual),temp4 as (select "xiaohong" as name from dual),temp5 as (select "zhaoliu" as name from dual) select * from (select * from temp1 union allselect * from temp2union allselect * from temp3union allselect * from temp4union all select * from temp5) res where res.name like "%zhang%"

      查詢字符串中包含‘zhang’的結(jié)果:

      【b】使用instr()進(jìn)行模糊查詢

      (1) 查詢字符串中包含‘zhang’的結(jié)果:

      with temp1 as (select "zhangsan" as name from dual),temp2 as (select "zhangsi" as name from dual),temp3 as (select "xiaoming" as name from dual),temp4 as (select "xiaohong" as name from dual),temp5 as (select "zhaoliu" as name from dual) select * from (select * from temp1 union allselect * from temp2union allselect * from temp3union allselect * from temp4union all  select * from temp5) res where instr(res.name,"zhang") > 0;

      (2) 查詢字符串中不包含‘zhang’的結(jié)果:

      with temp1 as (select "zhangsan" as name from dual),temp2 as (select "zhangsi" as name from dual),temp3 as (select "xiaoming" as name from dual),temp4 as (select "xiaohong" as name from dual),temp5 as (select "zhaoliu" as name from dual) select * from (select * from temp1 union allselect * from temp2union allselect * from temp3union allselect * from temp4union all select * from temp5) res where instr(res.name,"zhang") = 0;

      (3) 查詢以‘zhang’開頭的字符串:

      with temp1 as (select "zhangsan" as name from dual),temp2 as (select "zhangsi" as name from dual),temp3 as (select "sizhangsan" as name from dual),temp4 as (select "xiaohong" as name from dual),temp5 as (select "zhaoliu" as name from dual) select * from (select * from temp1 union allselect * from temp2union allselect * from temp3union allselect * from temp4union all  select * from temp5) res where instr(res.name,"zhang") = 1;

      (4)instr與like特殊用法

      select id, name from users where instr("a, b", id) > 0;--等價于select id, name  from users where id = a    or id = b;--等價于select id, name from users where id in (a, b);

      四、效率對比

      【a】使用plsql創(chuàng)建一張十萬條數(shù)據(jù)測試數(shù)據(jù)表,同時為需要進(jìn)行模糊查詢的列增加索引

      --創(chuàng)建10萬條測試數(shù)據(jù)create table test_instr_like as select rownum as id,"zhangsan" as name from dualconnect by level <= 100000; --name列建立索引create index idx_tb_name on test_instr_like(name);

      【b】使用like進(jìn)行模糊查詢

      select * from TEST_INSTR_LIKE t where t.name like "%zhang%"

      總耗時: 60秒

      【c】使用instr進(jìn)行模糊查詢

      select * from TEST_INSTR_LIKE t where instr(t.name, "zhang") > 0;

      總耗時:50秒

      由圖可見,instr查詢的速度確實比like快一些,但是,看執(zhí)行計劃的話,instr卻比like耗時一點。如下圖:

      五、總結(jié)

      以上是對instr基本使用方法的講解以及通過示例對比了like與instr的效率,在進(jìn)行模糊查詢的時候,能用instr的話就盡量用instr,畢竟數(shù)據(jù)量大的時候還是有一點優(yōu)勢的,本文是筆者對like以及instr的一些總結(jié)和見解,僅供大家學(xué)習(xí)參考,也希望大家多多支持。

      標(biāo)簽: Oracle
      相關(guān)文章:
      主站蜘蛛池模板: 久久频这里精品香蕉久久 | 毛片女| 国产黄页 | 欧美日韩国产一区二区三区在线观看 | 在线观看国产精品日本不卡网 | 国产精品一区二区资源 | 久久成人国产精品 | 一级做a爱片特黄在线观看免费看 | 麻豆国产96在线 | 日韩 | 99爱在线精品视频免费观看9 | 男操女视频网站 | 欧美激情成人网 | 午夜三级国产精品理论三级 | 成人高清无遮挡免费视频软件 | 黄色三级免费网站 | 久久亚洲私人国产精品 | 久久香蕉精品视频 | shkd在线观看 | 做爰成人五级在线视频| 久色福利 | 无码免费一区二区三区免费播放 | 国产成人精品福利网站人 | 台湾三级在线播放 | 国产精品亚洲一区在线播放 | 久久精品国产亚洲麻豆 | 99久热在线精品视频播放6 | 久久99爱视频 | 美女曰皮| 91精品宅男在线观看 | 亚洲aa视频 | 欧美亚洲日本国产综合网 | 日本一级大毛片a一 | 一色屋色费精品视频在线观看 | 中文字幕成人在线 | 欧美成人一级 | 欧美日本道免费一区二区三区 | 日美三级 | 国产素人在线观看 | 九月婷婷亚洲综合在线 | 成人国产亚洲欧美成人综合网 | 久在线观看视频 |