I have to do sum of some columns basis on where clause for better understanding i am implementing a temporary table here
declare @tbl table(a int ,b int,c int)
insert into @tbl values(1,2,3)
insert into @tbl values(2,2,3)
insert into @tbl values(1,3,1)
insert into @tbl values(1,2,3)
insert into @tbl values(1,2,3)
and for finding sum of a,b,c ob basis of value of a,b,c ; i am using following query
SELECT (
SELECT SUM(a) from @tbl where a=1
)AS a ,
(SELECT SUM(b) from @tbl where b=2
)AS b ,
(SELECT SUM(c) from @tbl where c=3
)AS c
I ask one of my friend to make a single line query for this work and he suggest me following lines
select sum((case when a=1 then a else null end)),
sum((case when b=2 then b else null end)),
sum((case when c=3 then c else null end))
from @tbl
Now i am thinking about performance which will work faster if i have 27 columns and millions of records ?
or any other method to achive this which will improve performance much better than these two
Expanding on Martin’s answer – it depends on what indexes you have and how populated the column is (nullable or not). Consider this example.
Column b is created nullable and is only 20% non-null. Now, run your queries against the table (with no indexes). Before you run it, make sure to press Ctrl-M (show actual execution plan). Run both queries in the same batch, i.e. highlight the text of both queries and execute.
I won’t bore you with images here but look at the plan which will show a cost of about 75% against the top query and 25% against the bottom. That’s expected, 75%:25% = 3:1 which is due to the first query passing through the table 3 times exactly. Now create these three indexes:
Then, rerun the query batch (both together). This time, you’ll see a cost of about 51% to 49%. Quite close. The reason is because the
(b)column being sparsely populated is very easy toSUMfrom the index pages alone. Even the other 2 columns are helped by retrieving more rows per index page than the clustered index on the data pages (which will contain all columns).When you expand this to 27 columns, the first form could run faster if each column is sparsely populated and if you have an index on each of the 27 columns. A big ask, and even then, it will probably only be very marginally faster.