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.
精彩评论