oracle自定义函数创建函数索引


ORACLE 允许建立函数索引,默认情况下只能使用系统函数。如果要建立基于用户自定义函数的索引。那么就需要在函数里加上关键字“deterministic”。

但是用户仍然可以在今后需要时修改函数,但是并不会造成索引失效,修改后请一定要执行重建索引命令。

创建表:

-- Create table
create table T1
(
  id NUMBER not null,
  c1 NUMBER,
  c2 NUMBER
)
tablespace LSDB
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );
-- Create/Recreate indexes 
create index ID_T1_C1 on T1 ("LS"."F1"(C1))
  tablespace LSDB
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );
-- Create/Recreate primary, unique and foreign key constraints 
alter table T1
  add constraint ID primary key (ID)
  using index 
  tablespace LSDB
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );

 

创建函数索引
例:

create or replace function F1(c integer) return integer deterministic is
FunctionResult integer;
begin
FunctionResult := c * 2;
return(FunctionResult);
end F1;

  

重建索引

ALTER INDEX 索引名称 REBUILD ;

例:

ALTER INDEX ID_T1_C1 REBUILD ;

  

使用方法

SELECT F1(5) FROM DUAL ;
SELECT C1, F1(C1) FROM T1 WHERE F1(C1) BETWEEN 101 AND 120;

  

总结
1,索引修改后,立即生效,
2, 但是SELECT 的表上相关字段如果建立了函数索引,且索引未重建,那么,对于该列计算出的值仍是旧函数值。-----其实ORACLE根本就没有计算,而是走索引得出来的。
3,重建索引后,计算数值恢复正确值 。
4, 使用自定义函数索引,在维护时需要小心。

 

http://www.itpub.net/thread-1303236-1-1.html


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM