再谈Oracle like优化

本文介绍了一种通过使用翻转函数和建立翻转索引来优化SQL查询的方法,特别是针对含有前模糊匹配的情况,能够显著减少全表扫描带来的资源消耗。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 查询%xx%的记录

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


查询%xx的记录

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')

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

select *

  from (select c.c_ply_no as c67_0_,

               c.c_insrnt_cnm as c68_1432_0_,

               i.C_LCN_NO as C83_1432_0_,

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

               c.c_edr_type as c85_1432_0_,

               c.C_prod_no as C86_1432_0_,

               c.C_INTER_CDE as CInterCde1432_0_

          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('%434')

         order by c.c_ply_no DESC)

使用翻转函数+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 to  use reverse index, e.g

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

Hope that helps.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值