zoukankan      html  css  js  c++  java
  • oracle instr函数(oracle 用instr 来代替 like)

    oracle instr函数
    对于instr函数,我们经常这样使用:从一个字符串中查找指定子串的位置。例如:
    
    SQL> select instr('Oracle','or') position from dual;
    
    POSITION
    
    ----------
    
            1
    从字符串'oracle'的第一个位置开始,向后查找第一个出现子串'or'出现的位置。
    
    其实instr共有4个参数,格式为“instr(string, substring, startposition, occurrence)”。可实现子串的如下搜索:
    
    1.从指定位置开始搜索子串
    
    2.指定搜索第几次出现的子串的位置
    
    3.从后向前搜索
    
    --1.从第3个字符开始搜索
    
    SQL> select instr('oracleor','or', 3) position from dual;
    
    POSITION
    
    ----------
    
            7
    
    --2.从第1个字符开始,搜索第2次出现子串的位置
    
    SQL> select instr('oracleor','or', 1, 2) position from dual;
    
    POSITION
    
    ----------
    
            7
    
    --3.从倒数第1个字符开始,搜索第1次出现子串的位置
    
    SQL> select instr('oracleor','or', -1, 1) position from dual;
    
    POSITION
    
    ----------
    
            7
    
    --3.从倒数第1个字符开始,搜索第2次出现子串的位置
    
    SQL> select instr('oracleor','or', -1, 2) position from dual;
    
    POSITION
    
    ----------
    
            1
    
    
    oracle用instr代替like
    
     
    
    表中将近有100万数据,很多时候,我们要进行字符串匹配,在SQL语句中,我们通常使用like来达到我们搜索的目标。但经过实际测试发现,like的效率与instr函数差别相当大。下面是一些测试结果:
    
    SQL> set timing on
    SQL> select count(*) from t where instr(title,’oracle’)>0;
    
    COUNT(*)
    ———-
    5478
    
    Elapsed: 00:00:11.04
    SQL> select count(*) from t where title like%oracle%’;
    
    COUNT(*)
    ———-
    5478
    
    Elapsed: 00:00:31.47
    SQL> select count(*) from t where instr(title,’oracle’)=0;
    
    COUNT(*)
    ———-
    994530
    
    Elapsed: 00:00:11.31
    SQL> select count(*) from t where title not like%oracle%’;
    
    COUNT(*)
    ———-
    994530
    
    注:
    
    instr(title,'oracle’)>0 相当于like
    
    instr(title,'oracle’)=0 相当于not like

    原文地址:http://blog.csdn.net/hzhsan/article/details/9186637

  • 相关阅读:
    了解Android_09之GridView(网格视图)
    了解Android_08之ListView(列表视图)
    了解Android_07之ImageView与使用glide第三方库加载网络图片
    了解Android_06之CheckBox
    了解Android_05之RadioButton
    了解Android_04之EditText标签
    了解Android_03之Button标签
    了解Android_02之TextView标签
    了解Android_01之HelloWorld
    02_vue本地应用(v-text,v-html,v-on,v-show,v-if,v-bind,v-for,v-model)
  • 原文地址:https://www.cnblogs.com/JulesHello/p/6170654.html
Copyright © 2011-2022 走看看