Join Algorithm on SQL Server

Join Hints enforce query optimizer to use a specified join algorithm.

The characteristics of the algorithms are as follows:

Name Pros Cons
Nested Loops - Very fast on “Small driven table”
- Less consume of memory and disk (suitable for OLTP)
- Available for non-equijoins joins
- Unsuitable for large tables join
Hash - Suitable for large tables join - Expensive for memory (unsuitable for OLTP)
- Unavailable for non-equijoins joins
Sort Merge - Suitable for large tables join
- Available for non-equijoins joins
- Expensive for memory (unsuitable for OLTP)
- Unavailable for non-equijoins joins
- Inefficient fro unsorted tables.

Optimum algorithms

Based on table size, the proper algorithms for each join query are as follows:

  1. Small to Small
    “Nested Loops”

  2. Small to Large
    “Nested Loops” with using an index of the large table’s join key.
    In case of many rows, exchange the driven table from the small one to the large one. Or “Hash” algorithm might be better solution.

  3. Large to Large
    “Hash” should be the first try. If the join key has been sorted, then try “Sort Merge”