Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case Statements versus coded if statements

Tags:

What is more efficient - handling with case statements in sql or handling the same data using if statements in code. I'm asking because my colleague has a huge query that has many case statements. I advised her to take stress off of the DB by coding the case statements. I've found that it is more efficient...but why?

like image 639
Eric Avatar asked Mar 11 '10 22:03

Eric


People also ask

Why is a case statement better than if statement?

The other advantage of CASE statements over IF statements is that CASE statements are faster. This is because they function as a sort of switch statement. However, CASE statements in Tableau are significantly limited because they cannot perform boolean algebra!

Is case and if the same?

In 1984, Tenneco bought International Harvester's agricultural equipment division and merged it into Case, and the farm equipment brands were combined as Case IH, although the corporation legally remained the J. I. Case Company.

What is a coding if statement?

If statements are logical blocks used within programming. They're conditional statements that tell a computer what to do with certain information. In other words, they let a program make 'decisions' while it's running.


1 Answers

There's a more fundamental question that's not being asked here: What are these CASE statements actually doing?

Forget performance for a minute. If CASE is only being used to transform the final output of a query, and it's actually possible to replace the same functionality with an if or select case in ASP, then it probably means that the database query/procedure is trying to do things that the UI should be responsible for, such as formatting. The separation-of-concerns issue is more serious than any possible performance issue.

If you have a query like this:

SELECT InvoiceID, InvoiceDate,     CASE WHEN PaidStatus = 0 THEN 'Unpaid' ELSE 'Paid' END FROM ... 

This is just silly, because the UI, or whatever layer does the data-to-domain mapping, should know how to convert a status in the database to its corresponding description. It makes no sense to be including this logic in the query itself.

On the other hand, if the CASE construct is an essential part of the query, like:

SELECT     SUM(CASE WHEN PaidStatus = 0 THEN Amount ELSE 0 END) AS TotalUnpaid,     SUM(CASE WHEN PaidStatus = 1 THEN Amount ELSE 0 END) AS TotalPaid FROM ... 

Don't even try to move this kind logic to the UI, because the database is much better at it. And the CASE is semantically a part of the query ("compute total paid and unpaid amounts for x"), it's not taking over any UI function.

Worry first about where the logic actually belongs based on what it's intending to accomplish. Performance concerns should only enter into the discussion if you are actually noticing significant performance problems.

like image 78
Aaronaught Avatar answered Oct 04 '22 22:10

Aaronaught