ORACLE中LIKE语句优化

来源:互联网 发布:java中格式化时间毫秒 编辑:程序博客网 时间:2024/04/19 09:48

 

 

 

1。尽量不要使用 like '%%'

    2。对于 like '%' (不以 % 开头)Oracle可以应用 colunm上的index

3。对于 like '%' (不以 % 结尾),可以利用reverse + function index 的形式,变化成 like '%'

 

建测试表和Index,注意,重点在于带reversefunction index。同时,一定要使用CBO才行

 

create table test_like as select object_id,object_name from dba_objects;

-------建立测试表

create index test_like__name on test_like(object_name)

------建立索引

create index test_like__name_reverse on test_like(reverse(object_name))

------建立反向索引

analyze table test_like compute statistics for table for all indexes;

------对表进行分析

都过SQLPLUS连接到数据,一定是SQLPLUS,因为下面有写命令在PLSQL的命令行中不被支持;

 

set autotrace trace exp

-----设定SQL跟踪

set linesize 2000

-------设定输出宽度

select * from test_like where object_name like 'AS%';

使用了索引

 

select * from test_like where object_name like '%S';

 

 

未使用索引

 

select * from test_like where reverse(object_name)like reverse('%AS')

使用了索引

 

 

 

 

原创粉丝点击