Warning in Execution Plan

Posted on

Question :

I have a T-SQL query with four inner joins, I executed the query in SSMS with Actual Execution Plan selected. When I review the generated plan I saw the warning icon on Sort operator.
What is the reason for this warning in Sort operator?

The Query Plan URL: https://www.brentozar.com/pastetheplan/?id=HJULL9H4N

I provided the screen shot of Execution Plan.
enter image description here

The Warning:
enter image description here

Answer :

What is the reason for this warning in Sort operator?

You can see the warning description in properties of the element. I’m sure you will see information like below:

Operator used tempdb to spill data during execution…..

It means that memory which was granted for the query turned out to be less than it needed. Two possible reasons for that:

  1. Wrong cardinality estimation so the query asked for the less memory grant than it really needed
  2. Lack of memory so the query asked enough memory but got less

Detailed information about memory grant can be found in the properties window of SELECToperation from your query plan.

I am not writing a complete new answer. Most of it is covered between the comments and answer by Denis.

Two possible reason mentioned. Wrong cardinality estimation and Lack of memory. I will explain how you can rule out Lack of Memory.

MaxQueryMemory under MemoryGrantInfo (once you right click the leftmost select node): Maximum amount of memory available for individual query grant in KB, if the query requires memory to run.

enter image description here

So if the MaxQueryMemory is larger than requested memory it is a good indication that sort warning is NOT due lack memory, it is rather due bad cardinality estimation.

A worth reading about Understanding SQL server memory grant


Leave a Reply

Your email address will not be published.