oracle模糊搜索避免使用like,替换为instr()

oracle中instr()函数用法

 

instr(name,'张三')>0  相当于  name like '%张三%'
instr(name,'张三')=1  相当于  name like '张三%'
instr(name,'张三')=0  相当于  name not like '%张三%'

 

 

 

 

 

 

实验得出,在一个2亿多条记录的表中,同时8个并行,使用like查询很久都不出来结果,但使用instr,4分钟即完成查找,性能是相当的好。由此可得,ORACLE内建的一些函数,是经过相当程度的优化的。

 

同时总结一个特殊用法:

select  t.*  from book  t where instr('12076, 12077', id) > 0; 
  它等价于 
select  t.*  from book  t where id = 12076 or id = 12077;

针对oracle中的number类型数据可能为null,查询时使用如下方式:

select  nvl(t.total,0) total from book t

nvl()函数可以设置查询值为null是赋予默认值。

posted @ 2018-01-27 11:09  流年飞逝  阅读(388)  评论(0编辑  收藏  举报