0

I have the following query in the StackOverflow2010 database:

SELECT Id,
       OwnerUserId,
       CreationDate,
       Body
FROM   Posts
ORDER BY LastEditorDisplayName

The execution plan shows me the desired memory

enter image description here

Erik Darling states that Operators that will use memory grants are

The most common reasons for memory grants are Sorts and Hashes. You may also see them for an Optimized Nested Loops Join, but whatever.

so for my execution plan, it looks like the sort operator is the only one which requires memory.

The sort operator shows that it expects to sort 3,729,200 row of 4093B

enter image description here

3,729,200 * 4093B = 15,263,615,600B

15,263,615,600 / 1024 = 14,905,875KB

so the sum of the memory required for the sort appears to be 14,905,875, yet the desired memory grant for the query is 18,817,952, why is there a discrepancy?

1 Answer 1

2
  • The sort operation will have storage overheads. Pure in-place sort algorithms exist but they may costume more CPU especially if operating in parallel.
  • Those row number and row size values are estimates, it explicitly says so. More memory is likely being requested in case they are under-estimates, to reduce the chance of unnecessary spills or other issues.
  • Other parts of the process of running the query will use memory, it may be a goodly chunk less than the sort but it won't be zero.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.