memory goat
Your first query plan with the merge join has a sort in it to support that operation. Merge joins require ordered data, and you only have index-ordered data coming from the inner table.
The outer table's index does not have the join key in a useful place in the key of the index, hence the sort.
When you SELECT *
, you'll be sorting *
by whatever ordering element(s) exist(s). That's a size of data operation, which you need a memory grant for scratch space to accommodate.
In the other merge join query, your sort operation will only take place on the join column, which will require significantly less memory space than the *
in your first example.
Feel free to hover your mouse over the Sort operator to validate output and order by columns.
The hash join query has no sorting, but the hash join will require some memory (just less because hashes normally aren't size of data the way sorts can be) to build the hash table.
Though it's not mentioned, it may be worth noting that parallelism does not cause memory grants to increase by DOP.