sql-server add tag
10 years ago Paul White (imported from SE)

Why is this simple query granted so much memory?

For an estimated 50 rows, the optimizer reserves almost 500 MB for the sort:

Estimated plan

Top Answer
6 years ago Paul White

This is a bug in SQL Server (from 2008 to 2014 inclusive).

My bug report is here.

The filtering condition is pushed down into the scan operator as a residual predicate, but the memory granted for the sort is erroneously calculated based on the pre-filter cardinality estimate.

To illustrate the issue, we can use (undocumented and unsupported) trace flag 9130 to prevent the Filter from being pushed down into the scan operator. The memory granted to the sort is now correctly based on the estimated cardinality of the Filter output, not the scan:

Estimated plan

For a production system, steps will need to be taken to avoid the problematic plan shape (a filter pushed into a scan with a sort on another column). One way to do this is to provide an index on the filter condition and/or to provide the required sort order.

With this index in place, the desired memory grant for the sort is only 928KB:

With filter index

Going further, the following index can avoid the sort completely (zero memory grant):

With filter and sort index

Tested and bug confirmed on the following builds of SQL Server x64 Developer Edition:

The bug was fixed in SQL Server 2016 Service Pack 1. The release notes include the following:

VSTS bug number 8024987

Table scans and index scans with push down predicate tend to overestimate memory grant for the parent operator.

Tested and confirmed fixed on:

  • Microsoft SQL Server 2016 (SP1) - 13.0.4001.0 (X64) Developer Edition
  • Microsoft SQL Server 2014 (SP2-CU3) 12.0.5538.0 (X64) Developer Edition

…under both cardinality estimation (CE) models.

This room is for discussion about this question.

Once logged in you can direct comments to any contributor here.

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.