天萃荷净

分享一篇运维DBA需求,创建含sysdate的函数index案例

1.模拟Oracle数据库环境

创建表插入数据库

[oracle@node1 ~]$ sqlplus chf/oracleplus

SQL*Plus: Release 11.2.0.3.0 Production on Mon Jan 9 16:27:19 2012

Copyright (c) 1982, 2011, Oracle. All rights reserved.

Connected to:

Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production

With the Partitioning, Oracle Label Security, OLAP, Data Mining,

Oracle Database Vault and Real Application Testing options

SQL> create table t_oracleplus(id number,intime date);

Table created.

SQL> DECLARE

2 i NUMBER;

3 BEGIN

4 FOR i IN 1..1000 LOOP

5 INSERT INTO t_oracleplus VALUES(i,SYSDATE-i);

6 END LOOP;

7 COMMIT;

8 END;

9 /

PL/SQL procedure successfully completed.

SQL> select count(*) from t_oracleplus;

COUNT(*)

----------

1000

SQL> exec dbms_stats.gather_table_stats(USER,'T_oracleplus',cascade => TRUE);

PL/SQL procedure successfully completed.

2.无index查询

SQL> set autot trace exp stat

Execution Plan

----------------------------------------------------------

Plan hash value: 548923532

--------------------------------------------------------------------------------

| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |

--------------------------------------------------------------------------------

| 0 | SELECT STATEMENT | | 10 | 120 | 3 (0)| 00:00:01 |

|* 1 | TABLE ACCESS FULL| T_oracleplus | 10 | 120 | 3 (0)| 00:00:01 |

--------------------------------------------------------------------------------

Predicate Information (identified by operation id):

---------------------------------------------------

1 - filter(NVL("INTIME",SYSDATE@!)>=TO_DATE(' 2011-12-31 00:00:00',

'syyyy-mm-dd hh24:mi:ss'))

Statistics

----------------------------------------------------------

0 recursive calls

0 db block gets

7 consistent gets

0 physical reads

0 redo size

770 bytes sent via SQL*Net to client

519 bytes received via SQL*Net from client

2 SQL*Net roundtrips to/from client

0 sorts (memory)

0 sorts (disk)

9 rows processed

SQL> set autot off

这里只是做了一个简单的查询,因为这个nvl(intime,sysdate)的条件,无法使用正常的index,所以没有建立intime索引的测试。

3.尝试创建index

SQL> create index in_t_oracleplus on t_oracleplus (nvl(intime,sysdate)) online nologging;

create index in_t_oracleplus on t_oracleplus (nvl(intime,sysdate)) online nologging

*

ERROR at line 1:

ORA-01743: only pure functions can be indexed

SQL> !oerr ora 1743

01743, 00000, "only pure functions can be indexed"

// *Cause: The indexed function uses SYSDATE or the user environment.

// *Action: PL/SQL functions must be pure (RNDS, RNPS, WNDS, WNPS). SQL

// expressions must not use SYSDATE, USER, USERENV(), or anything

// else dependent on the session state. NLS-dependent functions

// are OK.

--因为含有sysdate创建函数index失败

SQL> CREATE OR REPLACE FUNCTION f_oracleplus (itime DATE)

2 RETURN DATE

3 IS

4 otime DATE;

5 BEGIN

6 otime:=NVL(itime,SYSDATE);

7 RETURN otime;

8 END;

9 /

Function created.

--想采用自定义函数屏蔽掉sysdate在创建index时候的影响

SQL> create index in_t_oracleplus on t_oracleplus (f_oracleplus(intime)) online nologging;

create index in_t_oracleplus on t_oracleplus (f_oracleplus(intime)) online nologging

*

ERROR at line 1:

ORA-30553: The function is not deterministic

SQL> !oerr ora 30553

30553, 00000, "The function is not deterministic"

// *Cause: The function on which the index is defined is not deterministic

// *Action: If the function is deterministic, mark it DETERMINISTIC. If it

// is not deterministic (it depends on package state, database state,

// current time, or anything other than the function inputs) then

// do not create the index. The values returned by a deterministic

// function should not change even when the function is rewritten or

// recompiled.

--因为函数缺少deterministic不能使用于index上

SQL> CREATE OR REPLACE FUNCTION f_oracleplus (itime DATE)

2 RETURN DATE deterministic

3 IS

4 otime DATE;

5 BEGIN

6 otime:=NVL(itime,SYSDATE);

7 RETURN otime;

8 END;

9 /

Function created.

SQL> create index in_t_oracleplus on t_oracleplus (f_oracleplus(intime)) online nologging;

Index created.

--创建函数index成功

SQL> exec dbms_stats.gather_table_stats(USER,'T_oracleplus',cascade => TRUE);

PL/SQL procedure successfully completed.

4.再次查询

确定已经使用函数index,达到在index中使用sysdate函数index的目的。

SQL> set autot on exp stat

SQL> select * from t_oracleplus where f_oracleplus(intime)>=to_date('2011-12-31','yyyy-mm-dd');

Execution Plan

----------------------------------------------------------

Plan hash value: 2005404611

---------------------------------------------------------------------------------------------

| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |

---------------------------------------------------------------------------------------------

| 0 | SELECT STATEMENT | | 10 | 200 | 3 (0)| 00:00:01 |

| 1 | TABLE ACCESS BY INDEX ROWID| T_oracleplus | 10 | 200 | 3 (0)| 00:00:01 |

|* 2 | INDEX RANGE SCAN | IN_T_oracleplus | 10 | | 2 (0)| 00:00:01 |

---------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):

---------------------------------------------------

2 - access("CHF"."F_oracleplus"("INTIME")>=TO_DATE(' 2011-12-31 00:00:00',

'syyyy-mm-dd hh24:mi:ss'))

Statistics

----------------------------------------------------------

0 recursive calls

0 db block gets

5 consistent gets

0 physical reads

0 redo size

770 bytes sent via SQL*Net to client

519 bytes received via SQL*Net from client

2 SQL*Net roundtrips to/from client

0 sorts (memory)

0 sorts (disk)

9 rows processed

5.总结说明

5.1)通过函数屏蔽函数index的时候,不能使用sysdate

5.2)在创建函数时,需要指定deterministic关键字

--------------------------------------ORACLE-DBA----------------------------------------

最权威、专业的Oracle案例资源汇总之【学习笔记】Oracle索引 创建含sysdate的函数index案例

oracle创建索引index,【学习笔记】Oracle索引 创建含sysdate的函数index案例相关推荐

  1. oracle 创建角色 权限设置,[学习笔记] Oracle创建用户、分配权限、设置角色,

    [学习笔记] Oracle创建用户.分配权限.设置角色, 创建用户 create user student --用户名 identified by "123456" --密码 de ...

  2. oracle 最大值及其_学习笔记:Oracle优化 SQL查询最大值 最小值时的优化方法案例...

    天萃荷净 select max(id),min(id) from table优化,分享开发DBA需求,在SQL语句查询最大值.最小值数据时的优化方式案例 1.查看数据库版本 SQL> selec ...

  3. oracle常用数据统计,学习笔记:Oracle DBMS_STATS常用方法汇总 常用于收集统计oracle...

    天萃荷净 Oracle数据库中DBMS_STATS常用方法(收集oracle数据库.索引.表等信息) –收集Oracle数据库信息命令 EXEC DBMS_STATS.gather_database_ ...

  4. oracle数据库开多线程,学习笔记:Oracle表数据导入 DBA常用单线程插入 多线程插入 sql loader三种表数据导入案例...

    天萃荷净 oracle之数据导入,汇总开发DBA在向表中导入大量数据的案例,如:单线程向数据库中插入数据,多线程向数据表中插入数据,使用sql loader数据表中导入数据案例 1.Oracle数据库 ...

  5. oracle 删除awr报告,学习笔记:Oracle awr入门 深入了解AWR报告

    天萃荷净 深入了解AWR报告,ASH与AWR报告的官方说明,数据库进程和性能视图获取 1.AWR与ASH概念 1.ASH 若是一个普通的会话(我是指没有大量地耗费资源),则对于性能调整来说无足轻重.但 ...

  6. oracle rac 环境配置文件,学习笔记:Oracle RAC spfile参数文件配置案例详解

    天萃荷净 rac中的spfile探讨,记录一下Oracle RAC搭建完成后关于spfile参数文件的配置案例,与更改RAC环境中参数文件的方法 今天朋友的的rac,因为被同事做数据库升级,分别在两个 ...

  7. oracle数据变化记录,学习笔记:Oracle伪列函数ora_rowscn 记录表中行数据的修改时间...

    天萃荷净 Oracle数据库开发时使用伪列函数ora_rowscn查询出数据库表中行数据的修改时间 一.默认情况下 –创建t_orascn测试表 SQL> create table t_oras ...

  8. mysql原生建立索引_MySQL学习笔记之索引

    索引是存储引擎用于快速找到记录的一种数据结构. 索引对于良好的性能非常关键.尤其是当表中的数据量越来越大时,索引对性能的影响愈发重要.在数据量较小且负载较低时,不恰当的索引对性能的影响可能还不明显,但 ...

  9. oracle的脚本日志,学习笔记:Oracle alert日志文件巡检脚本

    天萃荷净 分享一篇Oracle alert日志文件巡检脚本 每天都检查oracle日志,所以写了一个比较完善的shell,让其自动处理,在运行程序之前,需要在该脚本目录下新建tmp目录 #!/usr/ ...

最新文章

  1. Gif(1)-加载视图-交替圆效果
  2. 【深度学习】带有 CRF-RNN 层的 U-Net模型
  3. Rails 定时任务——whenever实现周期性任务
  4. redis常用数据结构解析
  5. 在windows下使用cmd命令行对java文件进行编译和执行
  6. mysql中timestamp字段
  7. C#添加 / 创建本地数据库连接
  8. sip协议详解 系列(三)
  9. 【React】【Ant Deign】手机验证码登录效果实现
  10. php codesniffer,为你的 PHP_CodeSniffer 构建自定义规则
  11. 什么是二进制8421码?
  12. 网页游戏常见外挂原理及防御
  13. 第五人格显示连接服务器失败怎么办,第五人格提示重新连接服务器怎么办 连接服务器失败解决方法...
  14. 阿里巴巴国际站 网站和PC客户端都登录不了,其他电脑或手机可以
  15. .考试倒计时44天!来提分啦!
  16. 2016 Multi-University Training Contest 3 1010 Rower Bo
  17. Tarena - 关联查询
  18. 无线电能传输 wpt 磁耦合谐振 过零检测 matlab simulink仿真 pwm MOSFET,过零检测模块 基于二极管整流的无线电能传输设计
  19. c语言黑白棋程序设计报告,C语言游戏编程 黑白棋游戏
  20. 数据可视化之Seaborn绘图

热门文章

  1. 无线收发器中笔记本电脑的AUX_IN和USB供电同时插入时,存在环路噪声。
  2. MongoDB入门到使用
  3. php 匿名函数好处,谈谈PHP中的匿名函数与闭包
  4. 自己动手写任务调度平台
  5. 【数论】 秦九韶公式
  6. angular12项目对IE浏览器的支持
  7. 使用AI的混合工具混合
  8. 删除C++ string中的空格
  9. sqlite查询慢的问题
  10. 计算机培训服务合同,2020年最新版计算机培训合同(合同范本).pdf-汇文网