<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>惜分飞 &#187; ORA-01743</title>
	<atom:link href="https://www.xifenfei.com/tag/ora-01743/feed" rel="self" type="application/rss+xml" />
	<link>https://www.xifenfei.com</link>
	<description>提供7*24专业数据库(Oracle,SQL Server,MySQL,PostgreSQL等)恢复和技术支持@Tel:17813235971</description>
	<lastBuildDate>Wed, 04 Nov 2026 15:33:30 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	
	<item>
		<title>创建含sysdate的函数index</title>
		<link>https://www.xifenfei.com/2012/01/%e5%88%9b%e5%bb%ba%e5%90%absysdate%e7%9a%84%e5%87%bd%e6%95%b0index.html</link>
		<comments>https://www.xifenfei.com/2012/01/%e5%88%9b%e5%bb%ba%e5%90%absysdate%e7%9a%84%e5%87%bd%e6%95%b0index.html#comments</comments>
		<pubDate>Mon, 09 Jan 2012 09:07:14 +0000</pubDate>
		<dc:creator><![CDATA[惜分飞]]></dc:creator>
				<category><![CDATA[Oracle 开发]]></category>
		<category><![CDATA[ORA-01743]]></category>

		<guid isPermaLink="false">https://www.xifenfei.com/?p=2334</guid>
		<description><![CDATA[联系：手机/微信(+86 17813235971) QQ(107644445)  &#8230; <a href="https://www.xifenfei.com/2012/01/%e5%88%9b%e5%bb%ba%e5%90%absysdate%e7%9a%84%e5%87%bd%e6%95%b0index.html">继续阅读 <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<div id="xifenfeicopyright">
<p>联系：<a href="https://www.xifenfei.com/about_xifenfei" target="_blank">手机/微信(+86 17813235971)   QQ(107644445)</a><a target="_blank" href="http://wpa.qq.com/msgrd?v=3&#038;uin=107644445&#038;site=qq&#038;menu=yes"><img src="https://www.xifenfei.com/wp-content/themes/img/site_qq.jpg" alt="QQ咨询惜分飞" title="QQ咨询惜分飞"/></a></p>
<p>标题：<a href="https://www.xifenfei.com/2012/01/%e5%88%9b%e5%bb%ba%e5%90%absysdate%e7%9a%84%e5%87%bd%e6%95%b0index.html" target="_blank">创建含sysdate的函数index</a></p>
<p>作者：<a href="https://www.xifenfei.com" target="_blank">惜分飞</a>©版权所有[未经本人同意,不得以任何形式转载,否则有进一步追究法律责任的权利.]</p>
</div>
<p><strong>1.模拟环境</strong><br />
创建表插入数据库</p>
<pre class="brush: sql; title: ; notranslate">[oracle@node1 ~]$ sqlplus chf/xifenfei

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&gt; create table t_xifenfei(id number,intime date);

Table created.

SQL&gt; DECLARE
  2  i NUMBER;
  3  BEGIN
  4    FOR i IN 1..1000 LOOP
  5      INSERT INTO t_xifenfei VALUES(i,SYSDATE-i);
  6      END LOOP;
  7      COMMIT;
  8  END;
  9  /

PL/SQL procedure successfully completed.

SQL&gt; select count(*) from t_xifenfei;

  COUNT(*)
----------
      1000


SQL&gt; exec dbms_stats.gather_table_stats(USER,'T_XIFENFEI',cascade =&gt; TRUE);

PL/SQL procedure successfully completed.
</pre>
<p><strong>2.无index查询</strong></p>
<pre class="brush: sql; title: ; notranslate">
SQL&gt; 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_XIFENFEI |    10 |   120 |     3   (0)| 00:00:01 |
--------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter(NVL(&quot;INTIME&quot;,SYSDATE@!)&gt;=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&gt; set autot off
</pre>
<p>这里只是做了一个简单的查询，因为这个nvl(intime,sysdate)的条件，无法使用正常的index，所以没有建立intime索引的测试。<br />
<br />
<strong>3.尝试创建index</strong></p>
<pre class="brush: sql; title: ; notranslate">
SQL&gt;  create index in_t_xifenfei on t_xifenfei (nvl(intime,sysdate)) online nologging;
 create index in_t_xifenfei on t_xifenfei (nvl(intime,sysdate)) online nologging
                                                      *
ERROR at line 1:
ORA-01743: only pure functions can be indexed


SQL&gt; !oerr ora 1743
01743, 00000, &quot;only pure functions can be indexed&quot;
// *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&gt; CREATE OR REPLACE FUNCTION f_xifenfei (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&gt;  create index in_t_xifenfei on t_xifenfei (f_xifenfei(intime)) online nologging;
 create index in_t_xifenfei on t_xifenfei (f_xifenfei(intime)) online nologging
                                           *
ERROR at line 1:
ORA-30553: The function is not deterministic


SQL&gt; !oerr ora 30553
30553, 00000, &quot;The function is not deterministic&quot; 
// *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&gt; CREATE OR REPLACE FUNCTION f_xifenfei (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&gt; create index in_t_xifenfei on t_xifenfei (f_xifenfei(intime)) online nologging;

Index created.
--创建函数index成功

SQL&gt; exec dbms_stats.gather_table_stats(USER,'T_XIFENFEI',cascade =&gt; TRUE);

PL/SQL procedure successfully completed.
</pre>
<p><strong>4.再次查询</strong><br />
确定已经使用函数index，达到在index中使用sysdate函数index的目的。</p>
<pre class="brush: sql; title: ; notranslate">
SQL&gt; set autot on  exp stat
SQL&gt; select * from t_xifenfei where f_xifenfei(intime)&gt;=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_XIFENFEI    |    10 |   200 |     3   (0)| 00:00:01 |

|*  2 |   INDEX RANGE SCAN          | IN_T_XIFENFEI |    10 |       |     2   (0)| 00:00:01 |

---------------------------------------------------------------------------------------------


Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access(&quot;CHF&quot;.&quot;F_XIFENFEI&quot;(&quot;INTIME&quot;)&gt;=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
</pre>
<p><strong>5.总结说明</strong><br />
5.1)通过函数屏蔽函数index的时候，不能使用sysdate<br />
5.2)在创建函数时，需要指定deterministic关键字</p>
<div id="crp_related"><ul><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2012/12/%e5%88%9b%e5%bb%ba%e6%94%af%e6%8c%81null-index.html" rel="bookmark" class="crp_title">创建包含null值index</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2012/04/%e6%89%a7%e8%a1%8c%e8%ae%a1%e5%88%92%e4%b8%ad%e5%b8%b8%e8%a7%81index%e8%ae%bf%e9%97%ae%e6%96%b9%e5%bc%8f.html" rel="bookmark" class="crp_title">执行计划中常见index访问方式</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2012/04/hint%e6%8c%87%e5%ae%9aindex%e7%9a%84%e6%b7%b1%e5%85%a5%e7%90%86%e8%a7%a3.html" rel="bookmark" class="crp_title">hint指定index的深入理解</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2012/05/bind-variable-peeking-%e6%b5%8b%e8%af%95.html" rel="bookmark" class="crp_title">Bind Variable Peeking 测试</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2013/04/12c-top-n-sql%e5%ae%9e%e7%8e%b0%e5%88%86%e9%a1%b5%e5%8a%9f%e8%83%bd.html" rel="bookmark" class="crp_title">ORACLE 12C TOP N SQL实现分页功能</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2017/06/_optimizer_null_aware_antijoin-not-in.html" rel="bookmark" class="crp_title">_optimizer_null_aware_antijoin和not in效率</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2012/05/sql-profile-%e4%bd%bf%e7%94%a8.html" rel="bookmark" class="crp_title">sql profile 使用</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2011/09/oracle%e7%9b%b4%e6%96%b9%e5%9b%be%e7%90%86%e8%a7%a3%e4%b8%8e%e5%ae%9e%e9%aa%8c.html" rel="bookmark" class="crp_title">Oracle直方图理解与实验</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2014/03/%e5%90%abis-null-sql%e8%af%ad%e5%8f%a5%e4%bc%98%e5%8c%96.html" rel="bookmark" class="crp_title">含is null sql语句优化</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2014/10/11g%e4%b8%ad-connect-by-%e8%af%ad%e5%8f%a5%e6%89%a7%e8%a1%8c%e8%ae%a1%e5%88%92%e6%94%b9%e5%8f%98.html" rel="bookmark" class="crp_title">11g中 connect by 语句执行计划改变</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2011/09/10g%e6%96%b0%e5%a2%9e%e5%88%97%e6%96%b9%e5%bc%8f%e6%8c%87%e5%ae%9ahint.html" rel="bookmark" class="crp_title">10g新增列方式指定HINT</a></li><li style="list-style-type:decimal;font-size:11px;" ><a href="https://www.xifenfei.com/2011/07/select-maxamina-from-table%e4%bc%98%e5%8c%96.html" rel="bookmark" class="crp_title">select max(id),min(id) from table优化</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>https://www.xifenfei.com/2012/01/%e5%88%9b%e5%bb%ba%e5%90%absysdate%e7%9a%84%e5%87%bd%e6%95%b0index.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
