再谈Oracle like优化

查询%xx%的记录

1.使用instr
select count(*) from table t where instr(t.column,'xx')> 0
这种查询效果很好,速度很快


查询%xx的记录

selectcount(c.c_ply_no)asCOUNT

fromPolicy_Data_Allc,Item_Data_Alli

wherec.c_ply_no=i.c_ply_no

andi.C_LCN_NOlike'%245'

在执行的时候,执行计划显示,消耗值,io值,cpu值均非常大,原因是like后面前模糊查询导致索引失效,进行全表扫描。

解决方法:这种只有前模糊的sql可以改造如下写法

selectcount(c.c_ply_no)asCOUNT

fromPolicy_Data_Allc,Item_Data_Alli

wherec.c_ply_no=i.c_ply_no

andreverse(i.C_LCN_NO)likereverse('%245')

Item_Data_All表的C_LCN_NO字段进行前模糊匹配的情况都可以这样处理。例如这段:

select*

from(selectc.c_ply_noasc67_0_,

c.c_insrnt_cnmasc68_1432_0_,

i.C_LCN_NOasC83_1432_0_,

TO_CHAR(c.T_INSRNC_BGN_TM,'yyyy-mm-dd')asT84_1432_0_,

c.c_edr_typeasc85_1432_0_,

c.C_prod_noasC86_1432_0_,

c.C_INTER_CDEasCInterCde1432_0_

fromPolicy_Data_Allc,Item_Data_Alli

wherec.c_ply_no=i.c_ply_no

andreverse(i.C_LCN_NO)likereverse('%434')

orderbyc.c_ply_noDESC)

使用翻转函数+like前模糊查询+建立翻转函数索引=走翻转函数索引,不走全扫描。有效降低消耗值,io值,cpu值这三个指标,尤其是io值的降低。

编者附:

Simply speaking,

1) JAVA%: can use index

2)%JAVA%:starting and ending with %,no way to use index

3)%JAVA: cannot use index, but can change touse reverse index, e.g

select * from book b where reverse(b.name) like reverse('%JAVA')

Hope that helps.

你可能感兴趣的:(oracle)