Links
Home
Oracle DBA Forum
Frequent Oracle Errors
TNS:could not resolve the connect identifier specified
Backtrace message unwound by exceptions
invalid identifier
PL/SQL compilation error
internal error
missing expression
table or view does not exist
end-of-file on communication channel
TNS:listener unknown in connect descriptor
insufficient privileges
PL/SQL: numeric or value error string
TNS:protocol adapter error
ORACLE not available
target host or object does not exist
invalid number
unable to allocate string bytes of shared memory
resource busy and acquire with NOWAIT specified
error occurred at recursive SQL level string
ORACLE initialization or shutdown in progress
archiver error. Connect internal only, until freed
snapshot too old
unable to extend temp segment by string in tablespace
Credential retrieval failed
missing or invalid option
invalid username/password; logon denied
unable to create INITIAL extent for segment
out of process memory when trying to allocate string bytes
shared memory realm does not exist
cannot insert NULL
TNS:unable to connect to destination
remote database not found'>ora-02019
exception encountered: core dump
inconsistent datatypes
no data found
TNS:operation timed out
PL/SQL: could not find program
existing state of packages has been discarded
maximum number of processes exceeded
error signaled in parallel query server
ORACLE instance terminated. Disconnection forced
TNS:packet writer failure
see ORA-12699
missing right parenthesis
name is already used by an existing object
cannot identify/lock data file
invalid file operation
quoted string not properly terminated
Function based indexes?

Function based indexes?

2005-12-06       - By David Kurtz

Reply:     1     2     3     4     5     6     7     8     9     10     >>  

While this idea looks good because you produce a much smaller index, there
is the problem of how do you get to use the index?

You will have to edit the SQL in your application such that the predicate
matches the index expression.
Alternatively, you just index the column and use a histogram to help the
optimiser use it for the rare values.

There is a greater cost in index maintenance, but the index accesses are
almost as efficient as the function based index.
The question is whether it is worth changing the code for each statement
where you want to use the function based index in order to squeeze out every
possible consistent read?  Or is that CTD?


Here's a simple test, there are some comments in-line

set autotrace off
drop table dmk;
create table dmk (a number,t varchar2(200));

insert into dmk
select 0
,
RPAD(owner||'.'||object_name||'.'||subobject_name||':'||object_type,200,'.')
from   dba_objects
where  rownum <= 1;

insert into dmk
select 1
,
RPAD(owner||'.'||object_name||'.'||subobject_name||':'||object_type,200,'.')
from   dba_objects
where  rownum <= 30000;

--function based index
create index dmk_idx1 on dmk(case a when 0 then 0 end);
analyze table dmk compute statistics;
analyze table dmk compute statistics for columns a;

select num_rows from user_tables where table_name = 'DMK';
 NUM_ROWS
-- ---- --
     7644
select num_rows from user_indexes where table_name = 'DMK';
 NUM_ROWS
-- ---- --
        1
--so I only have the non null values in my index


set autotrace on

select count(t) from dmk where a = 1;
 COUNT(T)
-- ---- --
     7643
1 row selected.

Execution Plan
-- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- -----
  0      SELECT STATEMENT Optimizer=CHOOSE (Cost=36 Card=1 Bytes=202)
  1    0   SORT (AGGREGATE)
  2    1     TABLE ACCESS (FULL) OF 'DMK' (Cost=36 Card=7643 Bytes=1543886)

--this query had no choice but to full scan the table, so this is as
expected.


select count(t) from dmk where a = 0;
 COUNT(T)
-- ---- --
        1
1 row selected.


Execution Plan
-- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- -----
  0      SELECT STATEMENT Optimizer=CHOOSE (Cost=36 Card=1 Bytes=202)
  1    0   SORT (AGGREGATE)
  2    1     TABLE ACCESS (FULL) OF 'DMK' (Cost=36 Card=1 Bytes=202)

--this didn;t use the index because the where clause didn't match the
function


select count(t) from dmk where case a when 0 then 0 end = 0;
 COUNT(T)
-- ---- --
        1

1 row selected.


Execution Plan
-- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- -----
  0      SELECT STATEMENT Optimizer=CHOOSE (Cost=2 Card=1 Bytes=202)
  1    0   SORT (AGGREGATE)
  2    1     TABLE ACCESS (BY INDEX ROWID) OF 'DMK' (Cost=2 Card=1
Bytes=202)
  3    2       INDEX (RANGE SCAN) OF 'DMK_IDX1' (NON-UNIQUE) (Cost=1
Card=1)

Statistics
-- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- -----
         2  consistent gets

--if the predicate matches the function the index is used.

set autotrace off

--normal index
drop index dmk_idx1;
create index dmk_idx1 on dmk(a);
analyze table dmk compute statistics;
analyze table dmk compute statistics for columns a;
--now lets try with a normal index, and a histogram

select num_rows from user_tables where table_name = 'DMK';
select num_rows from user_indexes where table_name = 'DMK';

set autotrace on
select count(t) from dmk where a = 0;
 COUNT(T)
-- ---- --
        1

1 row selected.


Execution Plan
-- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- -----
  0      SELECT STATEMENT Optimizer=CHOOSE (Cost=2 Card=1 Bytes=202)
  1    0   SORT (AGGREGATE)
  2    1     TABLE ACCESS (BY INDEX ROWID) OF 'DMK' (Cost=2 Card=1
Bytes=202)
  3    2       INDEX (RANGE SCAN) OF 'DMK_IDX1' (NON-UNIQUE) (Cost=1
Card=1)

Statistics
-- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- -----
         3  consistent gets

--the index scan has the same cost, but requires an additional consistent
read because the index has an extra level because it contains entries all
the rows.  But you this way you don't have to modify the code.

select count(t) from dmk where a = 1;
set autotrace off

regards
__ ____ ____ ____ ____ __
David Kurtz
Go-Faster Consultancy Ltd.
tel: +44 (0)7771 760660
fax: +44 (0)7092 348865
web: www.go-faster.co.uk
mailto:david.kurtz@(protected)
Book: PeopleSoft for the Oracle DBA: http://www.psftdba.com
PeopleSoft DBA Forum: http://groups.yahoo.com/group/psftdba


--
http://www.freelists.org/webpage/oracle-l