0

When writing an SQL query such as

SELECT ID, NAME FROM USER_TABLE WHERE ID IN (1, 2, 10, 14, 15, ..., n)

does the parser just rephrase that into this?

SELECT ID, NAME FROM USER_TABLE WHERE ID = 1 
                                      OR ID =  2 
                                      OR ID =  10
                                      OR ID =  14
                                      OR ID =  15 
                                      ...
                                      OR ID =  n

Or does it do something else in the background for efficiency? While a nightmare to write out by hand and I would never advocate doing so, is there any theoretical performance benefit or hit to using IN rather than a series of OR conditions like that?

SandPiper
  • 2,816
  • 5
  • 30
  • 52

2 Answers2

3

That depends on the database. Logically, the IN is interpreted as a sequence of ORs, but that does not mean that is the underlying implementation.

For instance, MySQL will order a list of constants and use a binary search to find a match. That is quite different from a series of ORs.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
1

According to the specs (SQL92 for example), x IN (a, b, c) is described as:

8.4  <in predicate>

[...]

4) The expression
     RVC IN IPV
   is equivalent to
     RVC = ANY IPV

And:

8.7  <quantified comparison predicate>

[...]

<quantified comparison predicate> ::=
     <row value constructor> <comp op> <quantifier> <table subquery>

<quantifier> ::= <all> | <some>

<all> ::= ALL

<some> ::= SOME | ANY

[...]

   c) If the implied <comparison predicate> is true for at least
     one row RT in T, then "R <comp op> <some> T" is true.

The last line seems to suggest that x IN (a, b, c) is supposed to provide identical result as x = a OR x = b OR x = c. However the specs do not dictate how the RDBMs should implement the behavior, it could vary across RDBMs.

This following posts contain some interesting observations:

SQL Server seems to generate same execution plan for x IN (...) and x = ... OR x = ... where as MySQL handles them differently.

Salman A
  • 262,204
  • 82
  • 430
  • 521
  • For SQL Server have a look at https://blog.sqlauthority.com/2018/06/13/sql-server-performance-comparison-in-vs-or/ and for others have a look at https://stackoverflow.com/questions/3074713/in-vs-or-in-the-sql-where-clause. SQL Server simply translates IN to multiple ORs, MySQL handles them differently. – Salman A Dec 26 '18 at 21:13