Addressing large memory grant requests from optimized Nested Loops

Published Mar 23 2019 02:21 PM 286 Views
Microsoft
First published on MSDN on Mar 08, 2016
Optimized Nested Loops (or Batch Sort) is effectively an optimization aimed at minimizing I/O during a nested loop when the inner side table is large, regardless of it being parallelized or not.

The presence of this optimization in a given plan may not be very obvious when you look at an execution plan, given the sort itself is hidden, but you can see this by looking in the plan XML, and looking for the attribute Optimized , meaning the Nested Loop join may try to reorder the input rows to improve I/O performance. You can read more about optimized Nested Loops in Craig Freeman's blog .

We have encountered cases of performance issues where the root cause is tracked back to an extreme memory grant request from a query with these Batch Sorts. When the Optimizer uses a optimized Nested Loop join or "Batch Sort", the estimated used for memory grants can be much larger than the memory used for the operation.

The issue occurs when the outer table of the Nested Loop join has a predicate that filters the result to a small input, but the batch sort appears to be using an estimate for cardinality that is equivalent to the entire outer table. This can result in a perceived excessive memory grant which in a very concurrent server can have several side-effects, like OOM conditions, memory pressure for plan cache eviction, or unexpected RESOURCE_SEMAPHORE waits. We have seen how a single query that match this pattern can actually get several GB’s of granted memory on high-end machines (1TB+ RAM).

One option until now would be to disable this feature globally using Trace Flag 2340, as described in KB 2801413 . However, in SQL Server 2016 RC0 we have changed the behavior to maintain the advantage of the optimization, but now the max grant limit is based on the available memory grant space. This improvement also translates into better scalability, in the sense more queries can be executed with a smaller memory footprint. We are looking at back porting this behavior to an upcoming have ported this behavior to SQL Server 2014 Service Pack 2 , and as usual deliver added value to in-market versions.

You can see below the difference of granted memory for the same query ( left is SQL Server 2016 RC0 , right is SQL Server 2014 ). The ratio between granted memory and the max used memory is proportionally very large on the right side.



Pedro Lopes ( @sqlpto ) – Senior Program Manager
%3CLINGO-SUB%20id%3D%22lingo-sub-384658%22%20slang%3D%22en-US%22%3EAddressing%20large%20memory%20grant%20requests%20from%20optimized%20Nested%20Loops%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-384658%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3EFirst%20published%20on%20MSDN%20on%20Mar%2008%2C%202016%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20Optimized%20Nested%20Loops%20(or%20Batch%20Sort)%20is%20effectively%20an%20optimization%20aimed%20at%20minimizing%20I%2FO%20during%20a%20nested%20loop%20when%20the%20inner%20side%20table%20is%20large%2C%20regardless%20of%20it%20being%20parallelized%20or%20not.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20The%20presence%20of%20this%20optimization%20in%20a%20given%20plan%20may%20not%20be%20very%20obvious%20when%20you%20look%20at%20an%20execution%20plan%2C%20given%20the%20sort%20itself%20is%20hidden%2C%20but%20you%20can%20see%20this%20by%20looking%20in%20the%20plan%20XML%2C%20and%20looking%20for%20the%20attribute%20%3CSTRONG%3E%20Optimized%20%3C%2FSTRONG%3E%20%2C%20meaning%20the%20Nested%20Loop%20join%20may%20try%20to%20reorder%20the%20input%20rows%20to%20improve%20I%2FO%20performance.%20You%20can%20read%20more%20about%20optimized%20Nested%20Loops%20in%20%3CA%20href%3D%22http%3A%2F%2Fblogs.msdn.com%2Fb%2Fcraigfr%2Farchive%2F2009%2F03%2F18%2Foptimized-nested-loops-joins.aspx%22%20title%3D%22http%3A%2F%2Fblogs.msdn.com%2Fb%2Fcraigfr%2Farchive%2F2009%2F03%2F18%2Foptimized-nested-loops-joins.aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%20Craig%20Freeman's%20blog%20%3C%2FA%3E%20.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20We%20have%20encountered%20cases%20of%20performance%20issues%20where%20the%20root%20cause%20is%20tracked%20back%20to%20an%20extreme%20memory%20grant%20request%20from%20a%20query%20with%20these%20Batch%20Sorts.%20When%20the%20Optimizer%20uses%20a%20optimized%20Nested%20Loop%20join%20or%20%22Batch%20Sort%22%2C%20the%20estimated%20used%20for%20memory%20grants%20can%20be%20much%20larger%20than%20the%20memory%20used%20for%20the%20operation.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20The%20issue%20occurs%20when%20the%20outer%20table%20of%20the%20Nested%20Loop%20join%20has%20a%20predicate%20that%20filters%20the%20result%20to%20a%20small%20input%2C%20but%20the%20batch%20sort%20appears%20to%20be%20using%20an%20estimate%20for%20cardinality%20that%20is%20equivalent%20to%20the%20entire%20outer%20table.%20This%20can%20result%20in%20a%20perceived%20excessive%20memory%20grant%20which%20in%20a%20very%20concurrent%20server%20can%20have%20several%20side-effects%2C%20like%20OOM%20conditions%2C%20memory%20pressure%20for%20plan%20cache%20eviction%2C%20or%20unexpected%20RESOURCE_SEMAPHORE%20waits.%20We%20have%20seen%20how%20a%20single%20query%20that%20match%20this%20pattern%20can%20actually%20get%20several%20GB%E2%80%99s%20of%20granted%20memory%20on%20high-end%20machines%20(1TB%2B%20RAM).%20%3CBR%20%2F%3E%3CBR%20%2F%3EOne%20option%20until%20now%20would%20be%20to%20disable%20this%20feature%20globally%20using%20Trace%20Flag%202340%2C%20as%20described%20in%20%3CA%20href%3D%22https%3A%2F%2Fsupport.microsoft.com%2Fen-us%2Fkb%2F2801413%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%20KB%202801413%20%3C%2FA%3E%20.%20However%2C%20in%20SQL%20Server%202016%20RC0%20we%20have%20changed%20the%20behavior%20to%20maintain%20the%20advantage%20of%20the%20optimization%2C%20but%20now%20the%20max%20grant%20limit%20is%20based%20on%20the%20available%20memory%20grant%20space.%20This%20improvement%20also%20translates%20into%20better%20scalability%2C%20in%20the%20sense%20more%20queries%20can%20be%20executed%20with%20a%20smaller%20memory%20footprint.%20We%20are%20looking%20at%20back%20porting%20this%20behavior%20to%20an%20upcoming%20have%20ported%20this%20behavior%20to%20%3CSTRONG%3E%20SQL%20Server%202014%20Service%20Pack%202%20%3C%2FSTRONG%3E%20%2C%20and%20as%20usual%20deliver%20added%20value%20to%20in-market%20versions.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20You%20can%20see%20below%20the%20difference%20of%20granted%20memory%20for%20the%20same%20query%20(%20%3CSTRONG%3E%20left%20%3C%2FSTRONG%3E%20is%20SQL%20Server%20%3CSTRONG%3E%202016%20RC0%20%3C%2FSTRONG%3E%20%2C%20%3CSTRONG%3E%20right%20%3C%2FSTRONG%3E%20is%20SQL%20Server%20%3CSTRONG%3E%202014%20%3C%2FSTRONG%3E%20).%20The%20ratio%20between%20granted%20memory%20and%20the%20max%20used%20memory%20is%20proportionally%20very%20large%20on%20the%20right%20side.%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F97976iE7D9453E82DF1191%22%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3EPedro%20Lopes%20(%20%3CA%20href%3D%22https%3A%2F%2Ftwitter.com%2Fsqlpto%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3E%20%40sqlpto%20%3C%2FA%3E%20)%20%E2%80%93%20Senior%20Program%20Manager%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-384658%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Mar%2008%2C%202016%20Optimized%20Nested%20Loops%20(or%20Batch%20Sort)%20is%20effectively%20an%20optimization%20aimed%20at%20minimizing%20I%2FO%20during%20a%20nested%20loop%20when%20the%20inner%20side%20table%20is%20large%2C%20regardless%20of%20it%20being%20parallelized%20or%20not.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-384658%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESQLServerTiger%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Version history
Last update:
‎Mar 23 2019 02:21 PM
Updated by: