SQL Server hash match aggregate operator – Part2

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.

USE [AdventureWorks2012]

--Step1
CREATE INDEX IX_TerritoryID
ON Sales.SalesOrderHeader(TerritoryID)

--Step2
SELECT SalesOrderHeader.TerritoryID, COUNT(*)
FROM Sales.SalesOrderHeader
GROUP BY SalesOrderHeader.TerritoryID

SAGrphQueryPlan

   

 

SATextQueryPlan

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.

Happy learning!

Regards,

Kanchan

Like us on FaceBook | Join the fastest growing SQL Server group on FaceBook | Follow me on Twitter | Follow me on FaceBook

   

About Kanchan Bhattacharyya

Kanchan is an astute IT professional, a seasoned SQL Database Administrator with 13+ years of industry experience. A calculated risk taker with deep technical knowledge and has gained proficiency in database consulting and solutions across different environments. Kanchan holds MBA degree in IT and an International Executive MBA in Project Management. He is very passionate about SQL Server and holds MCSE (Data Platform), MCSA – SQL 2012, MCITP – SQL 2008 certifications. Currently he is focusing on cloud and latest releases of SQL Server. When not working, Kanchan likes to spend his time reading on new technical developments specifically on SQL Server and other related technologies.

View all posts by Kanchan Bhattacharyya →

Leave a Reply

Your email address will not be published.