开发者

Does OR clause suppress indexes in oracle?

开发者 https://www.devze.com 2023-01-15 14:19 出处:网络
Does OR clause suppress Indexes, If yes can someone provide appropriate example? create table test2(field1 varchar2(100),field2 varchar2(100),

Does OR clause suppress Indexes, If yes can someone provide appropriate example?

create table test2(field1 varchar2(100),field2 varchar2(100),
                   field3 number,field4 varchar2(100));

create index test2_idx1 on test2(field1);

create index test2_idx2 on test2(field3);

declare
  j number :=1;
begin
  for i in 1..500000 loop
    insert into test2 (field1,field2,field3,field4)
    values('field1='||j,'field2='||i,j,'field4='||‌​i);
    if (i mod 1000)=0 then 
      j:= j+1;
    end if; 
  end loop;
  commit;
end;

EXEC D开发者_StackOverflowBMS_STATS.GATHER_TABLE_STATS ('user', 'test2');

select * from test2 where field1='field1=1' or field3=1;


No, the use of the OR clause does not cause an index not to be used.
The OR clause only splinters the filtration decision path, and can be very costly for this depending on the complexity of the query & data involved.


I don't know what prompted you to ask this question but remember:

"Full scans are not always evil; indexes are not always good" (Tom Kyte in "Effective Oracle by Design")


You can always do a UNION between both clauses and it will be easier to make indexes work

select * from test2 where field1='field1=1' union select * from test2 where field3=1;

Anyway I run your sample script and the index is being used.

Does OR clause suppress indexes in oracle?

0

精彩评论

暂无评论...
验证码 换一张
取 消