Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.server -> Re: Function Based Index

Re: Function Based Index

From: Thomas Kyte <tkyte_at_us.oracle.com>
Date: Sun, 14 Nov 1999 21:05:24 -0500
Message-ID: <02kvOE0HgqJIyQQKbzuu1Q0o6Qul@4ax.com>


A copy of this was sent to Martin Haltmayer <Martin.Haltmayer_at_0800-einwahl.de> (if that email address didn't require changing) On Mon, 15 Nov 1999 01:28:42 +0200, you wrote:

please try to post in text- not html. most "real" newsreaders don't run in a browser.

The issue is a recursive dependency between the function test_func and the table you are indexing... the following does not work because the function is dependent on the table.....

tkyte_at_ORACLE> create table test_tab (tststr varchar2 (30)); Table created.

tkyte_at_ORACLE> create or replace function test_func (   2 s in test_tab.tststr%type
  3 ) return test_tab.tststr%type deterministic is   4 begin
  5 return upper (trim (s));
  6 end test_func;
  7 /
Function created.

And then this index on that table would be dependent on the function...

tkyte_at_ORACLE> create index test_tab_test_func_1b   2 on test_tab (substr (test_func (tststr), 1, 30)); on test_tab (substr (test_func (tststr), 1, 30))

                                            *
ERROR at line 2:
ORA-04020: deadlock detected while trying to lock object TKYTE.TEST_TAB

we break the dependency:

tkyte_at_ORACLE> create or replace function test_func ( s in varchar2) return varchar2
  2 deterministic
  3 is
  4 begin
  5 return upper (trim (s));
  6 end test_func;
  7 /

Function created.

tkyte_at_ORACLE> create index test_tab_test_func_1b   2 on test_tab (substr (test_func (tststr), 1, 30));

Index created.

and it works.

><!doctype html public "-//w3c//dtd html 4.0 transitional//en">
><html>
>Hi all,
><p>I have a strange behaviour with function based indexes in Oracle 8i
>on NT 4.0. I want to use a function-based index on a user function. It
>works nicely with internal functions. Here is what I get:
><p>SQL>
><br>SQL> drop table test_tab;
><p>Table dropped.
><p>SQL> create table test_tab (tststr varchar2 (30));
><p>Table created.
><p>SQL>
><br>SQL> create or replace function test_func (
><br>&nbsp; 2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s in test_tab.tststr%type
><br>&nbsp; 3&nbsp; ) return test_tab.tststr%type deterministic is
><br>&nbsp; 4&nbsp; begin
><br>&nbsp; 5&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return upper (trim
>(s));
><br>&nbsp; 6&nbsp; end test_func;
><br>&nbsp; 7&nbsp; /
><p>Function created.
><p>SQL> show errors
><br>No errors.
><br>SQL>
><br>SQL> describe test_func
><br>FUNCTION test_func RETURNS VARCHAR2(30)
><br>&nbsp;Argument Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
>Type&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
>In/Out Default?
><br>&nbsp;------------------------------ ----------------------- ------
>--------
><br>&nbsp;S&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
>VARCHAR2(30)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
>IN
><p>SQL>
><br>SQL> -- We know that test_tab_test_func_1a is not working according
>to the docs, but we try anyhow. But we get ORA-01450.
><br>SQL>
><br>SQL> create index test_tab_test_func_1a
><br>&nbsp; 2&nbsp; on test_tab (test_func (tststr));
><br>on test_tab (test_func (tststr))
><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
>*
><br>ERROR at line 2:
><br>ORA-01450: maximum key length (3218) exceeded
><br>&nbsp;
><p>SQL>
><br>SQL> -- According to the Oracle8i examples test_tab_test_func_1b should
>work. But we get ORA-04020.
><br>SQL>
><br>SQL> create index test_tab_test_func_1b
><br>&nbsp; 2&nbsp; on test_tab (substr (test_func (tststr), 1, 30));
><br>on test_tab (substr (test_func (tststr), 1, 30))
><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
>*
><br>ERROR at line 2:
><br>ORA-04020: deadlock detected while trying to lock object
><br>TEST_MATCH.TEST_TAB
><br>&nbsp;
><p>SQL>
><br>SQL> -- Surprise, surprise - test_tab_test_func_2 works although the
>function is merely the same.
><br>SQL>
><br>SQL> create index test_tab_test_func_2
><br>&nbsp; 2&nbsp; on test_tab (upper (trim (tststr)));
><p>Index created.
><p>SQL>
><br>SQL> spool off
><p>Can somebody please shed some light on this? I appreciate any help.
><p>Martin</html>
>

--
See http://osi.oracle.com/~tkyte/ for my columns 'Digging-in to Oracle8i'... Current article is "Part I of V, Autonomous Transactions" updated June 21'st  

Thomas Kyte                   tkyte_at_us.oracle.com
Oracle Service Industries     Reston, VA   USA

Opinions are mine and do not necessarily reflect those of Oracle Corporation Received on Sun Nov 14 1999 - 20:05:24 CST

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US