?当我们对列使用了函数运算之后,如果此列没有函数索引,那么普通索引是无效的。比如where substr(name,1,3)=abc;如果建立了create index idx_t on t(name); ? 那么谓词是无法使用此索引做范围扫描的。在oracle中允许定义函数索引(function based index,
?当我们对列使用了函数运算之后,如果此列没有函数索引,那么普通索引是无效的。比如where substr(name,1,3)=’abc’;如果建立了create index idx_t on t(name);
? 那么谓词是无法使用此索引做范围扫描的。在oracle中允许定义函数索引(function based index,简称fbi),函数索引可以是基于内置函数的,也可以是自定义函数的,
? 本文主要讲述基于自定义函数的索引用法及其注意点。
? ? ? 当需要对列进行复杂的运算,复杂的规则需要自定义函数的时候,如果需要走索引,那么必须建立自定义函数的索引。建立自定义函数索引有几点要注意:
? ? ?1.自定义函数必须加deterministic关键字,让oracle知道此函数对于每个入参的返回结果都是确定的唯一的。
? ? ? ? 道理很明显,如果一样的入参,结果不同,那么查询的结果必然有问题,必须要用这个关键字告诉oracle,此函数索引是可以信任的。但是有个问题得注意:因为自定义 ? ? ? 函数是一系列逻辑规则,就算定义的函数对每个入参返回的值不唯一(比如用了sysdate,random等运算),但是使用了deterministic关键字,让oracle相信唯 ? ? ? ? ? 一,事实不唯一,那么使用函数索引查询的结果必然也是有问题的。所以使用函数索引要注意:必须从逻辑上确定对于一样的入参返回的结果是一样的,因为oracle不会 ? ? ? ? 检查你的逻辑。
? ?
? ? ? ?2.一旦改变函数定义,必须rebuild对应的函数索引
? ? ? ? ?很显然,函数索引中存储的是表中的列或表达式作为自定义函数的参数的运算结果,如果函数改变,oracle不会自动rebuild函数索引对应的值,这样如果继续使用函数 ? ? ? ? 索引,必然结果可能出错。
? ? ?下面分别对上面的内容举例说明:
针对第1点的例子:
–使用自定义函数索引,必须加deterministic,并且实际对应一样的输入参数,返回的结果就是一样的,否则会导致错误
dingjun123@oradb> create or replace function get_date(param_in varchar2)
? 2 ?return date deterministic
? 3 ?as
? 4 ?begin
? 5 ? return?to_date(param_in,’yyyy’);
? 6 ?end;
? 7 ?/function created.
dingjun123@oradb> drop table t;
table dropped.
dingjun123@oradb> create table t(a varchar2(10));
table created.
dingjun123@oradb> create index idx_t on t(get_date(a));
index created.
–2013-年5月份插入
dingjun123@oradb> insert into t values(’2013′);
1 row created.
dingjun123@oradb> commit;
commit complete.
dingjun123@oradb> select * from t where get_date(a)=date’2013-5-1′;
a
———-
2013
1 row selected.
? of course,现在的结果是没有问题的,但是本身这个自定义函数中的to_date(param,’yyyy’)针对不同月份的插入结果返回的都是当月的第一天,如果我是6月插入:
–2013年6月份插入
dingjun123@oradb> insert into t values(’2013′);
1 row created.dingjun123@oradb> commit;
commit complete.
dingjun123@oradb> alter session set nls_date_format=’yyyy-mm-dd hh24:mi:ss’;
session altered.
dingjun123@oradb> select a from t;
a
———-
2013
2013
2 rows selected.
?现在是查询:
dingjun123@oradb> select * from t where get_date(a)=date’2013-5-1′;
a
———-
20131 row selected.
execution plan
———————————————————-
plan hash value: 1594971208
————————————————————————————-
| id ?| operation ? ? ? ? ? ? ? ? ? | name ?| rows ?| bytes | cost (%cpu)| time ? ? |
————————————————————————————-
| ? 0 | select statement ? ? ? ? ? ?| ? ? ? | ? ? 1 | ? ?16 | ? ? 1 ? (0)| 00:00:01 |
| ? 1 | ?table access by index rowid| t ? ? | ? ? 1 | ? ?16 | ? ? 1 ? (0)| 00:00:01 |
|* ?2 | ? index range scan ? ? ? ? ?| idx_t | ? ? 1 | ? ? ? | ? ? 1 ? (0)| 00:00:01 |
————————————————————————————-
predicate information (identified by operation id):
—————————————————
? ?2 – access(“dingjun123″.”get_date”(“a”)=to_date(‘ 2013-05-01 00:00:00′,
? ? ? ? ? ? ? ‘syyyy-mm-dd hh24:mi:ss’))
? ?上面的结果是令人迷惑的,因为表里存储的有2行2013,但是最终结果却只查询出一行。究其原因,就是自定义函数虽然使用了deterministic关键字,但是oracle只管有没有这关键字,而不会管你的函数逻辑是否真的对每个相同的输入,有一样的输出,这里我们使用deterministic关键字,欺骗了oracle。很显然,虽然在表里存储的2行都是2013,但是一个5月份插入的,一个6月份插入的,通过函数运算,一个索引中存储的是2013-5-1,一个是2013-6-1,所以使用2013-5-1里查询的时候,只返回1行。如果自定义中有类似于dbms_random,sys_guid等不确定或随时间变化值不同的,那么也会产生此混乱结果。
? 另外很多书上说函数索引必须:
? ? ? oracle使用函数索引,会进行查询重写,要求下面两个参数开启:? ? ? ?
? ? query_rewrite_enabled=true
? ? ? ? query_rewrite_integrity=trusted
? 经过测试,发现在本环境11g下无影响,后来看了yangtingkun大师的文章,原来早就没有影响了。http://space.itpub.net/4227/viewspace-68620
针对第2点的例子:
? ?函数索引的函数定义不能随便改变,改变就必须rebuild函数索引(or删除重建),因为函数索引中会存储对应函数运算的结果,然后在使用函数索引访问的时候,不用再调用函数,so,函数改变,oracle不会级联rebuild其函数索引,所以,改变函数逻辑不手动rebuild,必然是危险的。
?走全表扫描,函数会对每行都调用1次(当然determinstic函数是可以有缓存效果的,以后再说明):
dingjun123@oradb> drop table tt;
table dropped.dingjun123@oradb> create table ?tt(name varchar2(10));
table created.
dingjun123@oradb> insert into tt
? 2 ? ? ?select level from dual connect by level
999 rows created.
–dbms_application_info包监控函数的调用次数
dingjun123@oradb> create or replace function func_tt(x in varchar2)
? 2 ?return varchar2 deterministic as
? 3 ?begin
? 4 ? ? ?dbms_application_info.set_client_info(userenv(‘client_info’)+1 );
? 5 ? ? ?return ‘o’ || x;
? 6 ?end;
? 7 ?/
function created.
dingjun123@oradb> exec dbms_application_info.set_client_info(0);
pl/sql procedure successfully completed.
dingjun123@oradb> select * from tt where func_tt(name)
no rows selected
dingjun123@oradb> select userenv(‘client_info’) from dual;
userenv(‘client_info’)
—————————————————————-
999
1 row selected.
? ?无函数索引,全表扫描,访问对每行都调用函数,一条sql访问函数999次。如果使用函数索引,那么必然在创建(dml)的时候,会自动调用函数,索引中存储对应的key与函数运算结果值,所以,再使用到函数索引的时候,不用再调用函数,而且索引访问还提高效率,达到多种提高效率的效果。
–重置计数器
dingjun123@oradb> exec dbms_application_info.set_client_info(0);
pl/sql procedure successfully completed.dingjun123@oradb> create index ?idx_tt on tt(func_tt(name));
index created.
–创建索引的时候就调用函数了
dingjun123@oradb> select userenv(‘client_info’) from dual;
userenv(‘client_info’)
—————————————————————-
999
1 row selected.
dingjun123@oradb> set autotrace traceonly
–使用的时候不再调用函数,因为已经调用过函数,函数运算的结果已经存储到索引中了
dingjun123@oradb> select * from tt where func_tt(name) = ‘o1′;
1 row selected.
execution plan
———————————————————-
plan hash value: 6977672
————————————————————————————–
| id ?| operation ? ? ? ? ? ? ? ? ? | name ? | rows ?| bytes | cost (%cpu)| time ? ? |
————————————————————————————–
| ? 0 | select statement ? ? ? ? ? ?| ? ? ? ?| ? ?10 | 20090 | ? ? 2 ? (0)| 00:00:01 |
| ? 1 | ?table access by index rowid| tt ? ? | ? ?10 | 20090 | ? ? 2 ? (0)| 00:00:01 |
|* ?2 | ? index range scan ? ? ? ? ?| idx_tt | ? ? 4 | ? ? ? | ? ? 1 ? (0)| 00:00:01 |
————————————————————————————–
predicate information (identified by operation id):
—————————————————
? ?2 – access(“dingjun123″.”func_tt”(“name”)=’o1′)
note
—–
? ?- dynamic sampling used for this statement (level=2)
statistics
———————————————————-
? ? ? ? ?24 ?recursive calls
? ? ? ? ? 0 ?db block gets
? ? ? ? ?14 ?consistent gets
? ? ? ? ? 0 ?physical reads
? ? ? ? ? 0 ?redo size
? ? ? ? 417 ?bytes sent via sql*net to client
? ? ? ? 416 ?bytes received via sql*net from client
? ? ? ? ? 2 ?sql*net roundtrips to/from client
? ? ? ? ? 0 ?sorts (memory)
? ? ? ? ? 0 ?sorts (disk)
? ? ? ? ? 1 ?rows processed
dingjun123@oradb> select userenv(‘client_info’) from dual;
userenv(‘client_info’)
—————————————————————-
999
1 row selected.
? ? 使用自定义函数索引是危险的,如果修改函数定义,没有rebuild或删除重建函数索引,那么函数索引中存储的还是旧的函数运算结果,这样会导致错误:
dingjun123@oradb> create or replace function func_tt(x in varchar2)
? 2 ?return varchar2 deterministic as
? 3 ?begin
? 4 ? ? ?dbms_application_info.set_client_info(userenv(‘client_info’)+1 );
? 5 ? ?return ‘a’ || x;
? 6 ?end;
? 7 ?/
function created.–查询不对,函数应该运算结果’o1′应该没有行,但是因为索引没有被rebuild
dingjun123@oradb> select * from tt where func_tt(name) = ‘o1′;
name
———-
1
1 row selected.
–强制全表扫描,正确
dingjun123@oradb> select/*+full(tt)*/ * from tt where func_tt(name) = ‘o1′;
no rows selected
–rebuild索引后也正确
dingjun123@oradb> alter index idx_tt rebuild;
index altered.
dingjun123@oradb> set autotrace traceonly
dingjun123@oradb> select * from tt where func_tt(name) = ‘o1′;
no rows selected
execution plan
———————————————————-
plan hash value: 6977672
————————————————————————————–
| id ?| operation ? ? ? ? ? ? ? ? ? | name ? | rows ?| bytes | cost (%cpu)| time ? ? |
————————————————————————————–
| ? 0 | select statement ? ? ? ? ? ?| ? ? ? ?| ? ?10 | 20090 | ? ? 2 ? (0)| 00:00:01 |
| ? 1 | ?table access by index rowid| tt ? ? | ? ?10 | 20090 | ? ? 2 ? (0)| 00:00:01 |
|* ?2 | ? index range scan ? ? ? ? ?| idx_tt | ? ? 4 | ? ? ? | ? ? 1 ? (0)| 00:00:01 |
————————————————————————————–
predicate information (identified by operation id):
—————————————————
? ?2 – access(“dingjun123″.”func_tt”(“name”)=’o1′)
? ? 在不得不使用函数索引来提高效率的时候,别忘记了,随时准备维护函数索引,而且别弄出奇奇怪怪的函数索引,导致乱七八糟的问题,那样就不好了!
原文地址:自定义函数索引使用及其注意点, 感谢原作者分享。