SQL Server hash match aggregate operator – Part3

Hi Friends,

I hope you liked SQL Server hash match aggregate Part1 and Part2. Today we are going to see if input data is not sorted and we explicitly specify the order in query, what is the behaviour of the query optimizer.

USE [AdventureWorks2012]

--Step1
DROP INDEX Sales.SalesOrderHeader.IX_TerritoryID

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

HMWithOrderByGrphQueryPlan

SQL Server query optimizer decided to use a Hash Match aggregate then sort results by TerritoryID column. Query optimizer could have chosen a Stream aggregate and sort but as it is a cost based decision, whichever is less expensive is going to be the choice of optimizer.

That’s all for today, see you tomorrow on One operator a day series.

Regards,

Kanchan

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

Data Platform Virtual Summit 2020

Subscribe to SQLServerGeeks YouTube channel. If you want more learning content in your inbox, subscribe to SQLServerGeeks Bulletin.

SQLServerGeeks YouTube | SQLServerGeeks Bulletin | SQLServerGeeks Twitter

Avatar

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. Required fields are marked *