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