Yesterday we discussed about SQL Server hash match aggregate opertor and today’s post is a continuation of that. If you missed yesterday’s post, you can click here to read it.
Hash aggregate helps when our data is not sorted and if we can create an index on the appropriate column, SQL Server query optimize may use Stream aggregate instead. Let us create an index on TerritoryID column then run same query from yesterday to verify if query optimizer is using Stream aggregate operator.
CREATE INDEX IX_TerritoryID
SELECT SalesOrderHeader.TerritoryID, COUNT(*)
GROUP BY SalesOrderHeader.TerritoryID
From above images, we can see that after creating a non-clustered index on TerritoryID column, SQL Server query optimizer has chosen to use Stream aggregate instead of Hash Match aggregate. You can read more on Stream aggregate operators by clicking here.
Stay tuned, we are going to explore more on this operator tomorrow.