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

  • 相关阅读:
    洛谷P3569 [POI2014]KAR-Cards(线段树)
    洛谷P3295 [SCOI2016]萌萌哒(倍增+并查集)
    GFS分布式文件系统脚本
    源码安装apache脚本
    python如何安装cv2
    从npz文件中读取图片并显示的小例子
    读取npz,并显示图像
    Python中.npz文件的读取
    导出MNIST的数据集
    Keras下载的数据集以及预训练模型保存在哪里
  • 原文地址:https://www.cnblogs.com/wzihan/p/14777063.html
Copyright © 2011-2022 走看看