0

Is there any performance difference between query A and query B?

Query A

SELECT * FROM SomeTable
WHERE 1 = 1 AND (SomeField LIKE '[1,m][6,e][n]%')

Query B

SELECT * FROM SomeTable
WHERE 1 = 1 AND (SomeField IN ('16', 'Mens'))
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
denizemektup
  • 85
  • 1
  • 3

1 Answers1

5

The first could be much slower. An index can't be used with LIKE unless there is a constant prefix, for example LIKE 'foo%'. The first query will therefore require a table scan. The second query however could use an index on SomeField if one is available.

The first query will also give the wrong results as it matches '1en'.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • 1
    @Mark: Shouldn't the optimizer be smart enough to realize that `c LIKE 'Q[a-b]%'` could use the index? As it's the same as `c LIKE 'Qa%' or c LIKE 'Qb%'`? – ypercubeᵀᴹ Mar 05 '12 at 15:23
  • 1
    My guess is the optimizer will only use the index for Q in LIKE 'Q[a-b]%'. – paparazzo Mar 05 '12 at 16:31