Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL performance tuning for Oracle Many OR vs IN () [duplicate]

I dont have "explain plan" on hand. Could you help to tell which of the below is more efficient?

Option 1:

select ... 
    from VIEW_ABC 
    where STRING_COL = 'AA' 
       OR STRING_COL = 'BB' 
       OR STRING_COL = 'BB' 
       OR ...
       OR STRING_COL = 'ZZ'

Option 2:

select ... 
    from VIEW_ABC 
    where STRING_COL IN ('AA','BB',...,'ZZ')
like image 761
m12345y Avatar asked Nov 21 '11 16:11

m12345y


2 Answers

This is a similar question: IN vs OR of Oracle, which faster?

Look at the answer given by OMG Ponies, he suggests that IN is more optimized than OR.

There is also this question: IN vs OR in the SQL WHERE Clause

Though it is non database specific, it has a few good answers from an Oracle perspective.

For what it's worth, the use of IN would be far easier to read and therefore easier to support in the long run. That alone might sway your choice, especially if (as I suspect) the Oracle optimiser treats them both the same way.

If, however, your list of values is very large then you should probably look into a totally different method for retrieving your results.

like image 102
Ollie Avatar answered Sep 28 '22 12:09

Ollie


I would assume the optimizer would treat both versions the same. IN is really just syntactic shorthand for multiple OR statements.

like image 30
Joe Stefanelli Avatar answered Sep 28 '22 14:09

Joe Stefanelli