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

 
 Home
 E-mail Us
 Oracle Articles
New Oracle Articles


 Oracle Training
 Oracle Tips

 Oracle Forum
 Class Catalog


 Remote DBA
 Oracle Tuning
 Emergency 911
 RAC Support
 Apps Support
 Analysis
 Design
 Implementation
 Oracle Support


 SQL Tuning
 Security

 Oracle UNIX
 Oracle Linux
 Monitoring
 Remote s
upport
 Remote plans
 Remote
services
 Application Server

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

 Remote S
upport  
 Development  

 Implementation


 Consulting Staff
 Consulting Prices
 Help Wanted!

 


 Oracle Posters
 Oracle Books

 Oracle Scripts
 Ion
 Excel-DB  

Don Burleson Blog 


 

 

 


 

 

 
 

How to index with a SQL CASE statement

Oracle Database Tips by Donald BurlesonDecember 29,  2015

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 ....
and SOURCE_TRAN = "PO"
and
case SOURCE_TRAN
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
(case SOURCE_TRAN
when 'PO' then PO_ID
when 'VOUCHER' then voucher_id
ELSE journal_id
end = '0000000001'
END);

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(
   ownname=>null,
   tabname=> 'CASE_TAB,
   estimate_percent=>null,
   cascade=>true,
   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 Training at Sea
 
 
 
 
oracle dba poster
 

 
Follow us on Twitter 
 
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 -  2020

All rights reserved by Burleson

Oracle ® is the registered trademark of Oracle Corporation.