打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
SQL 使用like '%ABC' 和 like '%ABC%'的优

编辑手记: 今天继续学习SQL优化的技巧感谢刘永甫专家,本文授权转自“老虎刘谈SQL优化”。

一般情况下,sql中使用col_name like 'ABC%‘的情况才能使用到col_name字段上的索引。那么如果是col_name like '%ABC%'的情况,能否使用索引呢?

答案是:可以使用索引,但是需要改写SQL并创建reverse函数索引。

具体如何实现?听专家为你揭晓。

一、col_name like '%ABC'时的优化方法

Test case:

    Create table t1 as select * from dba_objects;

    Create index idx_t1_objectname1 on t1(object_name);

在正常情况下,百分号在后面,可以使用索引:

select object_name from t1 where object_name like ‘DBA%';

百分号在前面,不能使用索引:

select object_name from t1 where object_name like '%LIB';

解决方法

create index idx_t1_objectname2 on t1(reverse(object_name));

select object_name from t1 where reverse(object_name) like reverse('%LIB');

我们看执行计划:


改写后SQL走了索引。

二、col_name like '%ABC%'时的优化方法

一般认为这种情况是不能使用索引的,但还是有一些优化方法可以使用。

有三种情况:

1、ABC始终从字符串开始的某个固定位置出现,可以创建函数索引进行优化

2、ABC始终从字符串结尾的某个固定位置出现,可以创建函数组合索引进行优化

3、ABC在字符串中位置不固定,可以通过改写SQL进行优化

情况1、先创建substr函数索引,再使用like ‘ABC%’。

假如ABC从字符串第五位出现:

Test Case:

create index idx_substr_t1_objname on t1 (substr(object_name,5,30));

select object_id,object_type,object_name from t1

where substr(object_name,5,30) like 'TAB%';

情况2、先创建reverse+substr组合函数索引,再使用like reverse‘%ABC’。

假如ABC从字符串倒数第五位出现:

Test Case:

Create index idx_t1_reverse2 on t1(reverse(substr(object_name,1,length(object_name)-4)));

select object_id,object_name,object_type from t1 

where reverse(substr(object_name,1,length(object_name)-4)) like reverse('%TAB_COL');

情况3、这种情况需要like的字段上存在普通索引,主要在SQL的写法上做改进。

原来的SQL是这样写的:

Select object_id,object_type,object_name from t1

where object_name like '%ABC%‘;

改写后的SQL是这样的:

Select object_id ,object_type,object_name from t1 

Where object_name in

(select object_name from t1 where object_name like ‘%ABC%’);

Test Case:

create index idx_t1_object_name on t1 (object_name);

Select object_id,object_type,object_name from t1

where object_name like '%TABCOL%';

此时SQL的执行计划是t1 表做全表扫描。

Select object_id,object_type,object_name from t1

Where object_name in

(select object_name from t1 where object_name like '%TABCOL%');

改写后的SQL执行计划是索引全扫描加索引回表操作:


优化原理

用索引全扫描取代表的全扫描。因为索引全扫描的代价是全表扫描的1/N (即索引块数与数据块数的比例),表越大,优化效果越明显。

改写后SQL的执行计划,根据索引再回表的代价要看符合条件的记录数多少:如果in子查询返回的记录数很少,那么优化的效果就相当于效率提高了N倍;如果in子查询返回的记录数较多,两种SQL的性能区别就不是很明显了。

加入"云和恩墨大讲堂"微信群,参与讨论学习

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
sys.sysobjects、sys.all
Oralce 检查表和索引的并行度 DOP 脚本
如何在PowerDesigner里面去掉统计信息
oracle中如何将一个表从一个表空间中移动到另一个表空间
如何监控oracle的索引是否使用 ? 半瓶
Python list列表的常用操作方法
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服