In my MS SQL Server database I am pulling transaction data based on a variety of different codes that are in one column.
Would it be more efficient to:
-
join the same table over and over for each code in a WHERE clause
-
do multiple case statements on the whole table (shown below)
-
do multiple case statements on the whole table but limit it by a
WHERE SubsidCde IN ('AA','BA','BB', etc)clause
We have so many queries running per second that even though I have tried all 3 methods I get no definitive results.
SELECT
SUM(CASE WHEN Subsid_Cde = 'AA' THEN Trans_Amt END),0) [AA],
SUM(CASE WHEN Subsid_Cde = 'BA' THEN Trans_Amt END),0) [BA],
SUM(CASE WHEN Subsid_Cde = 'BB' THEN Trans_Amt END),0) [BB]
FROM
Transactions
-- There are 8 more rows like this, using a different code for each line
If you’re summing all possible (or most) values of Subsid_Cde field, then CASE is faster as it won’t scan the table multiple times as it aggregates the sums. If you only looking for a small subset of possible Subsid_Cde fields then separate selects / joins (along with an index on Subsid_Cde) will work faster.
You need to learn to read Execution Plans, then you’ll be able to figure such things by yourself.
Also, alternatively, you could do a GROUP BY on Subsid_Cde wrapped into a PIVOT clause (google for PIVOT MS SQL SERVER 2005)