tags:

views:

110

answers:

4

Which operator in oracle gives better performance IN or OR

ex:

select * from table where y in (1,2,3)

or

select * from table where y = 1 or y = 2 or y = 3
+8  A: 

You'd want to do an explain plan to be sure, but I would expect the performance to be identical.

Eric Petroelje
Note, if they are identical, from a readability standpoint, the IN list is easier to read, so I suggest going with that assuming the explain plan shows them equivalent.
Jay
Question http://stackoverflow.com/questions/1387612/how-can-i-introduce-multiple-conditions-in-like-operator has a similiar problem and is answered with an alternativ solution which might improve performance.
Christian13467
+6  A: 

The two statements are equivalent, the optimizer will generate the same access path. Therefore you should choose the one that is the most readable (I'd say the first one).

Vincent Malgrat
A: 

In a simple query like yours, the optimizer is smart enough to treat them both 100% the same so they are identical.

HOWEVER, that is potentially not 100% the case.

E.g. when optimizing large complex joints, it is plausible that the optimizer will not equate the two approaches as intelligently, thus choosing the wrong plan. I have observed somewhat similar problem on Sybase, although I don't know if it exists in Oracle (thus my "potentially" qualifyer).

DVK
+3  A: 

I would hesitate to use OR like that. You need to be careful if you add additional criteria. For instance adding an AND will require you remember to add parenthesis.

eg:

select * from table where y = 1 or y = 2 or y = 3

gets changed to:

select * from table where ( y = 1 or y = 2 or y = 3 ) AND x = 'value'

It is quite easy to forget to include the parenthesis and inject a difficult to daignose bug. For maintainability alone I would strongly suggest using IN instead of OR.

David
While I agree that the first two answers (@vincent and @eric) more directly address the issue, I'm with you on this.I seldom use 'OR' without parenthesis, no matter how simple.It makes it safer for later code and improves readability
moleboy
There were other good answers - just thought I'd give the value add comment ;).
David