2

I have a query which has a list of values for checking against a single column in a multi column table.

Example query ONE......

SELECT `name`, `sleeps` 
FROM `properties` 
WHERE `town`='bude' OR `town`='newquay' OR `town`='widemouth' OR `town`='polzeath' OR `town`='crantock' OR `town`='perranporth' OR `town`='Porthmeor' OR `town`='St Ives' OR `town`='porthtowan'

Example query TWO......

SELECT `name`, `sleeps` 
FROM `properties` 
WHERE `town` IN ('bude', 'newquay', 'widemouth', 'polzeath', 'crantock', 'wadebridge', 'Porthmeor', 'St Ives', 'porthtowan')
    • -

I have run both in phpmyadmin and found the query speed to be very similar, all be it with a table of approx 5,000 records.

In terms of performance and durability when the table contains more data is is best to go for the 'IN' style query or use multiple instances OR?

Thanks

Paul
  • 26,170
  • 12
  • 85
  • 119
David G
  • 301
  • 3
  • 14

3 Answers3

4

This is a simple test result,

SELECT * FROM item WHERE id = 1 OR id = 2 ... id = 10000
This query took 0.1139 seconds

SELECT * FROM item WHERE id IN (1,2,3,...10000)
This query took 0.0413 seconds

IN faster than OR

Adem Öztaş
  • 20,457
  • 4
  • 34
  • 42
2

Convert the less efficient one into the other. So I think the answer should rather depend on the readability of each (where I think that IN is more efficient)

IN is 3 times faster than OR

Naveen Kumar Alone
  • 7,536
  • 5
  • 36
  • 57
2

I tried some querys, IN is faster everytime.

Fox
  • 566
  • 4
  • 14