使用场景:
当一个查询运行很慢。通过检查where子句,发现其中的一列应用了sql lower函数,lower函数阻止使用该列上现有的索引。你想要创建一个基于函数索引来支持这个查询,如下
sql> select index_name,column_name from user_ind_columns where table_name='t1';
index_name column_name
------------------------- ------------------------------
t1_pk object_id
sql> set autotrace trace explain;
sql> select * from t1 where lower(object_name)='i_undo1';
执行计划
----------------------------------------------------------
plan hash value: 3617692013
--------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time |
--------------------------------------------------------------------------
| 0 | select statement | | 908 | 101k| 436 (1)| 00:00:01 |
|* 1 | table access full| t1 | 908 | 101k| 436 (1)| 00:00:01 |
--------------------------------------------------------------------------
predicate information (identified by operation id):
---------------------------------------------------
1 - filter(lower(object_name)='i_undo1')
从以上可以看出即使该表中有索引也没有使用。
解决方案
1、创建一个基于函数的索引
2、如果使用oracle database 11g或更高版本,创建一个索引虚拟列
下面实现基于函数的索引
创建索引可以通过以下方式来估计索引所使用空间以及所需要分配的空间
sql> set serveroutput on
sql> var used_bytes number;
sql> var allo_bytes number;
sql> exec dbms_space.create_index_cost('create index t1_object_name on t1(lower(object_name))',:used_bytes,:allo_bytes);
pl/sql procedure successfully completed
used_bytes
---------
2269350
allo_bytes
---------
4194304
sql>create index idx_lower on t1(lower(object_name)) tablespace index_nocompress;
sql> select * from t1 where lower(object_name)='i_undo1';
执行计划
----------------------------------------------------------
plan hash value: 2274688371
-------------------------------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time |
-------------------------------------------------------------------------------------------------
| 0 | select statement | | 908 | 101k| 193 (0)| 00:00:01 |
| 1 | table access by index rowid batched| t1 | 908 | 101k| 193 (0)| 00:00:01 |
|* 2 | index range scan | idx_lower | 363 | | 3 (0)| 00:00:01 |
-------------------------------------------------------------------------------------------------
predicate information (identified by operation id):
---------------------------------------------------
2 - access(lower(object_name)='i_undo1')
注意:不能直接修改一个创建了基于函数索引的列。需要先删除索引,,然后修改列,最后再重建索引。不然会报ora-30556错误(在要修改的列上已定义函数索引或位图联接索引)
查看基于函数的索引定义dba/all/user_ind_expressions
sql> select index_name,column_expression from user_ind_expressions;
index_name column_expression
------------------------- --------------------------------------------------
idx_lower lower(object_name)
接着实现在虚拟列创建一个索引
使用场景
现在正使用一个基于函数的索引,但想要获得更好的性能,想将基于函数的索引替换为一个虚拟列,然后在虚拟列上创建索引(需要11g环境或更高版本)。
sql>alter table t1 add(lower_object_name generated always as (lower(object_name)) virtual);
sql>create index idx_lower on t1(lower_object_name) tablespace index_nocompress;
由oracle索引来理解arcsde索引
oracle索引技术之如何建立最佳索引
oracle索引列null值引发执行计划该表的测试示例
oracle索引 主键影响查询速度
oracle索引扫描
本文永久更新链接地址:
