Is there any way to an index that can be used to include the "OR condition "?

Hello I have some questions.
The test was conducted in the following procedure .
create table test
c1 varchar2(10),
c2 varchar2(10),
primary key(c1)
create index test_idx1 on test(c2);
Command> explain select * from test where c1 = 'AAAAAAAAAA' or c2 = 'AAAAAAAAAA';
Query Optimizer Plan:
  STEP:                1
  LEVEL:               1
  OPERATION:           RowLkRangeScan
  TBLNAME:             TEST
  IXNAME:              TEST
  INDEXED CONDITION:   <NULL>
  NOT INDEXED:         TEST.C2 = 'AAAAAAAAAA' OR TEST.C1 = 'AAAAAAAAAA'
Command>
Command> explain select * from test where c1 = 'AAAAAAAAAA' and c2 = 'AAAAAAAAAA'
Query Optimizer Plan:
  STEP:                1
  LEVEL:               1
  OPERATION:           RowLkRangeScan
  TBLNAME:             TEST
  IXNAME:              TEST
  INDEXED CONDITION:   TEST.C1 = 'AAAAAAAAAA'
  NOT INDEXED:         TEST.C2 = 'AAAAAAAAAA'
Command>
By including the "OR condition " in this test does not use the index.
Is there any way to an index that can be used to include the "OR condition "?
Thanks.
GooGyum.

A database cannot in general use indexes in this way for an 'or' involving two different columns. However, for this specific example one can easily rewrite the query using 'UNION' to use the relevant indexes while still giving the same (correct) result:
Command> explain select * from test t1 where t1.c1 = 'AAAAAAAAAA' union select * from test t2 where t2.c2 = 'AAAAAAAAAA';
Query Optimizer Plan:
  STEP:                1
  LEVEL:               1
  OPERATION:           RowLkRangeScan
  TBLNAME:             TEST
  IXNAME:              TEST
  INDEXED CONDITION:   T1.C1 = 'AAAAAAAAAA'
  NOT INDEXED:         <NULL>
  STEP:                2
  LEVEL:               2
  OPERATION:           RowLkRangeScan
  TBLNAME:             TEST
  IXNAME:              TEST_IDX2
  INDEXED CONDITION:   T2.C2 = 'AAAAAAAAAA'
  NOT INDEXED:         <NULL>
  STEP:                3
  LEVEL:               1
  OPERATION:           OrderBy
  TBLNAME:             <NULL>
  IXNAME:              <NULL>
  INDEXED CONDITION:   <NULL>
  NOT INDEXED:         <NULL>
  STEP:                4
  LEVEL:               2
  OPERATION:           UnionMergeSort
  TBLNAME:             <NULL>
  IXNAME:              <NULL>
  INDEXED CONDITION:   <NULL>
  NOT INDEXED:         <NULL>
Maybe you can apply a similar trick? If you know there is no possibility of duplicate rows then you can further optimise this (in terms of performance) by using UNION ALL.
Chris

Similar Messages

Maybe you are looking for