zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    HDU1875——畅通工程再续(最小生成树:Kruskal算法)
    CodeForces114E——Double Happiness(素数二次筛选)
    POJ3083——Children of the Candy Corn(DFS+BFS)
    POJ3687——Labeling Balls(反向建图+拓扑排序)
    SDUT2157——Greatest Number(STL二分查找)
    UVA548——Tree(中后序建树+DFS)
    HDU1312——Red and Black(DFS)
    生活碎碎念
    SQL基础四(例子)
    Linux系统中的一些重要的目录
  • 原文地址:https://www.cnblogs.com/wzihan/p/14777063.html
Copyright © 2011-2022 走看看