I have a SQL query like this:
SELECT A, B, (CASE WHEN C=0 THEN 0 ELSE 1 END), COUNT(D)
FROM SomeTable
GROUP BY A, B, (CASE WHEN C=0 THEN 0 ELSE 1 END)
On a huge dataset the actual execution plan attributes most of the time to a node that corresponds to logical operation Aggregate and physical operation HashMatch.
Will changing the order in which the columns and conditions are listed in the GROUP BY
list affect performance?
Best Answer
Despite what MSDN documentation says, no, it doesn't matter for
GROUP BY
queries.You can test it here, at: SQL-Fiddle test (SQL-Server 2012)
The queries:
produce the same execution plan:
Now if you change that Group by to:
it does make a difference and it does produce different execution plans (and different result sets of course). It still uses the index though, at least in that SQL-Fiddle test. It the mismatching order case, it gets the data from the index but it has to do an intermediate sort (to calculate the rollup values).