2

In MySQL is it very bad to do this:

SELECT ... WHERE foo IN ('bar')

instead of this...

SELECT ... WHERE foo = 'bar'

...given that this dynamically generated WHERE clause is also likely to be

SELECT ... WHERE foo IN ('bar', 'baz', 'buz')
5
  • possible duplicate: stackoverflow.com/questions/957405/… Commented Mar 29, 2011 at 1:21
  • @mcabral: I agree. @Quassnoi's answer is the most applicable to this situation Commented Mar 29, 2011 at 1:40
  • @mcabral - out of curiosity how did you find that dupe given that 'in' is a stop word? Commented Mar 29, 2011 at 1:51
  • ye olde trick! google for 'site:stackoverflow.com mysql in keyword' Commented Mar 29, 2011 at 2:17
  • @mcabral haha! yes, obvious once you see it Commented Mar 29, 2011 at 2:37

3 Answers 3

2

My understanding is that it would be converted into the same thing before execution. Either way, I would file this under 'really really low concern'.

Sign up to request clarification or add additional context in comments.

Comments

1

I'm not sure that I would worry about this.

However, because it's dynamically generated you could always do a test to see how many values you have before building the where clause.

2 Comments

I'm not sure either, but if there's no significant performance hit I'd rather keep the generation code clean + concise
@Rob Agar: This falls under the idea of Preoptimization. If while profiling the app you notice a performance hit, then change it. Otherwise just go with what you are more comfortable with.
0

It is much better to use, for SQL query optimization and best practices:

WHERE foo = 'bar' OR foo = 'baz'

than:

WHERE foo IN ('bar', 'baz')

Regards

3 Comments

that should actually be foo = 'bar' OR foo = 'baz', but i don't think that changes something in performance at all
do you have a source for this? i don't mean to be rude but, i've been reading they are quite the same.
For small IN clauses there isn't an issue. However, if you have very large IN clauses, especially ones that vary with each execution, there is significant issues with multipage allocators and the procedure cache.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.