Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is replacing conditional logic with CASE statements efficient in Transact-SQL?

I have a stored procedure that does something like:

IF @Param = '1'
    SELECT HT.HeaderKey, HT.Description,
           (SELECT SUM(E1) -- E1 is actually a complex expression
            FROM   DetailTable DT INNER JOIN ...
                                  INNER JOIN ...
                                  INNER JOIN ...
            WHERE  DT.HeaderKey = HT.HeaderKey)
    FROM   HeaderTable HT
ELSE IF @Param = '2'
    SELECT HT.HeaderKey, HT.Description,
           (SELECT SUM(E2) -- E2 is yet another complex expression
            FROM   DetailTable DT INNER JOIN ... -- Here, DetailTable is not
                                  INNER JOIN ... -- joined to the same tables
                                  INNER JOIN ... -- as in the first case
            WHERE  DT.HeaderKey = HT.HeaderKey)
    FROM   HeaderTable HT
-- Etc. There are five cases.

I would like to reduce the query to the following:

SELECT HT.HeaderKey, HT.Description,
       CASE @Param
         WHEN '1'
           (SELECT SUM(E1)
            FROM   DetailTable DT INNER JOIN ...
                                  INNER JOIN ...
                                  INNER JOIN ...
            WHERE  DT.HeaderKey = HT.HeaderKey)
         WHEN '2'
           (SELECT SUM(E2)
            FROM   DetailTable DT INNER JOIN ...
                                  INNER JOIN ...
                                  INNER JOIN ...
            WHERE  DT.HeaderKey = HT.HeaderKey)
         -- Etc.
         ELSE 0
       END
FROM   HeaderTable HT

However, if SQL Server evaluates all the cases, regardless of which one will be actually returned, the modified query would be grossly inefficient.

Thus, I'd like to know, does SQL Server evaluate all the cases in a CASE statement, or only the first one that satisfies the CASE's condition?

like image 454
pyon Avatar asked Feb 07 '11 16:02

pyon


People also ask

Which is faster case or if statement in SQL?

CASE statements are preferred because: SQL: They are ANSI standard, making it portable to other databases without need for alteration. they support "short circuiting"

Is replace case sensitive in SQL?

Collation Affects the Results of SQL REPLACEIt is a case-insensitive collation.

What can I use instead of case in SQL?

Question: What is Alternative to CASE Statement in SQL Server? Answer: IIF Function.

Does SQL case statement short circuit?

CASE will not always short circuit evaluates its conditions sequentially and stops with the first condition whose condition is satisfied.


1 Answers

SQL Server's CASE statement, as mentioned in this article, does take advantage of short-circuiting so in the example shown it will not evaluate the results of every possible CASE outcome for every row.
However, you will still get a less efficient query than your current format as you'll then be forcing all outcomes to share the same execution plan, which may not be optimal. Depending on the differences between the CASE subqueries the effect could be quite significant.

like image 86
MartW Avatar answered Sep 27 '22 22:09

MartW