Call now: 252-767-6166  
Oracle Training Oracle Support Development Oracle Apps

 E-mail Us
 Oracle Articles

 Oracle Training
 Oracle Tips

 Oracle Forum
 Class Catalog

 Remote DBA
 Oracle Tuning
 Emergency 911
 RAC Support
 Apps Support
 Oracle Support

 SQL Tuning

 Oracle UNIX
 Oracle Linux
 Remote s
 Remote plans
 Application Server

 Oracle Forms
 Oracle Portal
 App Upgrades
 SQL Server
 Oracle Concepts
 Software Support

 Remote S


 Consulting Staff
 Consulting Prices
 Help Wanted!


 Oracle Posters
 Oracle Books

 Oracle Scripts

Don Burleson Blog 







How to index with a SQL CASE statement

Oracle Tips by Burleson Consulting
December 29,  2008

Question:  I have a SQL statement using a CASE statement and it does not use an index.  How can I make an index that can be used by a CASE statement?  Here is my SQL statement which causes a full-table scan, but only when the CASE statement is included:

select ...
from TABLE_1
where ....
when 'PO' then PO_ID
when 'VOUCHER' then voucher_id
ELSE journal_id
end = '0000000001'

The table has regular indexes defined, based on PO, VOUCHER & Journal. I've found that, without the CASE statement in the SQL, the SOURCE_TRAN index is used, but the index is not used when I add the CASE statement to the SQL. How can I use a CASE expression to index this query?

Answer:  Using function-based indexes (FBI) you can create an index on any built-in function, including a CASE expression.  Here we use CASE within the create index syntax:

create index
case_index as
when 'PO' then PO_ID
when 'VOUCHER' then voucher_id
ELSE journal_id
end = '0000000001'

Once created, you need to create CBO statistics, but beware that there are numerous bugs and issues when analyzing a function-based index.  See these important notes on statistics and function-based indexes:

EXEC DBMS_STATS.gather_index_stats('OWNER', 'CASE_INDEX');

exec dbms_stats.gather_table_stats(
   tabname=> ‘CASE_TAB,
   method_opt=> ‘FOR ALL HIDDEN COLUMNS SIZE 1′

exec dbms_stats.gather_table_stats(
ownname => 'OWNER',
tabname => 'CASE_TAB',
cascade => TRUE);

As a final step, run the execution plan for the query and ensure that your SQL with CASE is using the appropriate index.
Followup:  Thanks B, FBI works great!!!
Get the Complete
Oracle SQL Tuning Information 

The landmark book "Advanced Oracle SQL Tuning  The Definitive Reference"  is filled with valuable information on Oracle SQL Tuning. This book includes scripts and tools to hypercharge Oracle 11g performance and you can buy it for 30% off directly from the publisher.

See these additional notes on using function-based indexes here.
  1. Oracle SQL tips

  2. Dictionary query to display function-based index function

  3. Oracle function based indexes

  4. Oracle - Function Based Indexes (FBI)

  5. Oracle Concepts - Function-based indexes





oracle dba poster

Oracle performance tuning software 
Oracle Linux poster


Burleson is the American Team

Note: This Oracle documentation was created as a support and Oracle training reference for use by our DBA performance tuning consulting professionals.  Feel free to ask questions on our Oracle forum.

Verify experience! Anyone considering using the services of an Oracle support expert should independently investigate their credentials and experience, and not rely on advertisements and self-proclaimed expertise. All legitimate Oracle experts publish their Oracle qualifications.

Errata?  Oracle technology is changing and we strive to update our BC Oracle support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:  and include the URL for the page.


Burleson Consulting

The Oracle of Database Support

Oracle Performance Tuning

Remote DBA Services


Copyright ? 1996 -  2014

All rights reserved by Burleson

Oracle ? is the registered trademark of Oracle Corporation.