1

Is there any performance difference between the two following queries:

SELECT foo,bar FROM table WHERE id IN (1,2,3);

and

SELECT foo,bar FROM table WHERE id = 1 OR id = 2 OR id = 3;

I'm sure the difference in negligible in a small example like this, but what about when the number of valid IDs is much larger?

Albin Sunnanbo
  • 46,430
  • 8
  • 69
  • 108
GSto
  • 41,512
  • 37
  • 133
  • 184

2 Answers2

2

The optimizer is more likely to be able to optimize WHERE id IN (1,2,3) than id = 1 OR id = 2 OR id = 3. I also think that using IN is more readable here.

Use WHERE id IN (1,2,3).

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
0

This was an assignment in a comp.sci database class I took, back in the dark ages. Turns out there's not too much of a performance difference between the two within measurement limits, but the ... IN (...) version does result in a much smaller query due to less characters required to construct it.

Marc B
  • 356,200
  • 43
  • 426
  • 500