Does Your GROUP BY Order Matter?


Sometimes when you do GROUP BY, the order of the columns does matter. For example, these two SELECT queries produce different results:

CREATE INDEX Location_DisplayName ON dbo.Users(Location, DisplayName); SELECT TOP 100 Location, DisplayName, COUNT(*) AS Duplicates FROM dbo.Users GROUP BY Location, DisplayName ORDER BY Location, DisplayName; SELECT TOP 100 DisplayName, Location, COUNT(*) AS Duplicates FROM dbo.Users GROUP BY DisplayName, Location ORDER BY DisplayName, Location;

Their actual execution plans are wildly different:

They both use the index to retrieve their data, sure, but:

They both use index scans on the same index, but the first one only has to read