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

Free Oracle Tips

HTML Text

 Home
 E-mail Us
 Oracle Articles


 Oracle Training
 Oracle News

 Oracle Forum
 Class Catalog


 Our Staff
 Our Prices
 Help Wanted!

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


 SQL Tuning
 Security

 UNIX
 Oracle UNIX
 Linux
 Oracle Linux
 Monitoring
 Remote help

 Remote plans
 Remote
services
 Oracle C++
 Oracle Java
 Apache
 JDeveloper
 App Server

 Applications
 Oracle Forms
 Oracle Portal
 11i Upgrades
 SQL Server
 Oracle Concepts
 HTML-DB Tips
 Software Help

 Remote Help  
 Development  

 Implementation


 Financials Training
 Oracle 11i
 Oracle Apps 11i
 Oracle Workflow
 Oracle AR 11i Class
 Oracle AP 11i class
 Oracle GL 11i class
 Oracle HR 11i class
 Oracle FA 11i class
 11i Project Mgt
 11i procurement
 11i collections


 Oracle Posters
 Oracle Books

 Oracle Tuning Book
 Oracle RAC Book
 Oracle Security
 Easy Oracle Books
 Oracle Scripts
 SQL Server DBA
 SQL Design Patterns
 Ion
 Excel-DB   


 BC Oracle News


 Rednecks!
 Dress code
 Arabian Stallion

 Burleson Arabians
 Guide Horses
 Don Burleson Blog
 Golf & Travel


 Privacy Policy
 

 

 
 

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 ....
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!!!



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 cruise
 
 
 
Oracle performance tuning software
 
 

Oracle performance tuning book

 

 
 
 
Oracle performance Tuning 10g reference poster
 
 
 
Oracle training in Linux commands
 
Oracle training Excel
 
Oracle training & performance tuning books
 

 

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 -  2009 by Burleson Enterprises, Inc. All rights reserved.

Oracle ? is the registered trademark of Oracle Corporation.