Hash Join
Hash is used in two most commonly seen , Hash Join and Hash Aggregate. Those two happen when there are no any other alternatives which can be used for more efficient operation.
For instance, when SQL Server joins 2 tables together but none of them has an index. SQL Server has no idea whether the joining keys are sorted or not.
In most of the case for such scenarios, hash join will take place. As its name, hash join uses hash algorithm to encode the joining keys from both side, compares the hashed values, and produce the result. This sounds very complex.Yes, it is a very complex & heavy operator.
When is Hash Match used by SQL Server
A Hash Match is likely to pop-up in the following scenarios:
- No covering index is available on the tables involved in join, union or aggregation operations.
- One large table is being joined against a much small table, Hash Match sometimes proves to be very efficient in these cases.
A Hash Match operator can pop-up in the following situations:
- Hash Match Join: Hashing is initially performed on the joining columns of the smaller table, which is then matched against the corresponding columns of the larger table.
- Hash Match Aggregation (or Distinct): In this scenario SQL Server execution plan decides to build a hash table to facilitate the aggregation of a table.
- Hash Match Union: Hash Match is also used to facilitate a Union operation between two tables.
Optimize SQL Server Hash Match operator
Optimizing Hash Match might be difficult due to the nature of the query itself, or Hash Match might actually be the best option for the query you are trying to execute, nevertheless, here are a few options you could try out in order to improve the performance of Hash Match, or possibly alert SQL Server’s execution plan to a better option for joining, concatenating or grouping data.Building a Covering Index: SQL Server will not need to create a hash table to hold the hashed values being used in an argument if these columns are properly indexed.
Filter Operations: Filtering queries and making them sargable is one of the best ways to ensure efficient results for any query.
Calculated Expressions: As always, Calculated Expressions could be the culprit behind a slow Hash Match operation;you could turn a calculated expression into an indexed column that could be utilized in a much more efficient way to satisfy join or aggregation operations.
Post a Comment