HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlModerate

In a CASE Statement, which is faster: "WHEN [field] in (0)" or "WHEN [field] = 0"?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
casefieldstatementfasterwhenwhich

Problem

I'm looking at a stored proc that another guy has written and every WHEN clause is using an IN statement instead of =. My gut tells me this isn't as fast, efficient, etc.

For example:

CASE WHEN a.Indicator in ('Yes') then 'something'
     WHEN a.STATUS in (0) then 'something 0'
     WHEN a.STATUS in (1) then 'something 1'
     WHEN a.STATUS in (2) then 'something 2'
     WHEN a.STATUS in (3,4,5,6,7,8) then 'something big'
END


Is it faster to use IN instead of =?

Solution

The following construct:

c IN (x, y, z)


Gets extrapolated to:

c = x OR c = y OR c = z


It will logically follow that c IN (x) extrapolates to c = x. Therefore the first comment is absolutely right: you will not see a difference in performance except at the very edge case (where the size of the query text is actually impacted sufficiently by the choice), but even there it is hard to fathom the performance difference being worth mentioning.

Code Snippets

c IN (x, y, z)
c = x OR c = y OR c = z

Context

StackExchange Database Administrators Q#17364, answer score: 11

Revisions (0)

No revisions yet.