Home
%3CLINGO-SUB%20id%3D%22lingo-sub-383537%22%20slang%3D%22en-US%22%3EOPTIMIZED%20Nested%20Loops%20Joins%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-383537%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%2018%2C%202009%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20In%20my%20past%20two%20posts%2C%20I%20explained%20how%20SQL%20Server%20may%20add%20a%20sort%20to%20the%20outer%20side%20of%20a%20nested%20loops%20join%20and%20showed%20how%20%3CA%20href%3D%22http%3A%2F%2Fblogs.msdn.com%2Fcraigfr%2Farchive%2F2009%2F03%2F04%2Foptimizing-i-o-performance-by-sorting-part-2.aspx%22%20mce_href%3D%22http%3A%2F%2Fblogs.msdn.com%2Fcraigfr%2Farchive%2F2009%2F03%2F04%2Foptimizing-i-o-performance-by-sorting-part-2.aspx%22%20title%3D%22Optimizing%20I%2FO%20Performance%20by%20Sorting%20-%20Part%202%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20this%20sort%20can%20significantly%20improve%20performance%20%3C%2FA%3E%20.%26nbsp%3B%20In%20%3CA%20href%3D%22http%3A%2F%2Fblogs.msdn.com%2Fcraigfr%2Farchive%2F2008%2F10%2F07%2Frandom-prefetching.aspx%22%20mce_href%3D%22http%3A%2F%2Fblogs.msdn.com%2Fcraigfr%2Farchive%2F2008%2F10%2F07%2Frandom-prefetching.aspx%22%20title%3D%22Random%20Prefetching%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20an%20earlier%20post%20%3C%2FA%3E%20%2C%20I%20discussed%20how%20SQL%20Server%20can%20use%20random%20prefetching%20to%20improve%20the%20performance%20of%20a%20nested%20loops%20join.%26nbsp%3B%20In%20this%20post%2C%20I'm%20going%20to%20explore%20one%20more%20nested%20loops%20join%20performance%20feature.%26nbsp%3B%20I'll%20use%20the%20same%20database%20that%20I%20used%20in%20%3CA%20href%3D%22http%3A%2F%2Fblogs.msdn.com%2Fcraigfr%2Farchive%2F2009%2F02%2F25%2Foptimizing-i-o-performance-by-sorting-part-1.aspx%22%20mce_href%3D%22http%3A%2F%2Fblogs.msdn.com%2Fcraigfr%2Farchive%2F2009%2F02%2F25%2Foptimizing-i-o-performance-by-sorting-part-1.aspx%22%20title%3D%22Optimizing%20I%2FO%20Performance%20by%20Sorting%20-%20Part%201%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20my%20two%20prior%20posts%20%3C%2FA%3E%20.%26nbsp%3B%20Let's%20start%20with%20the%20following%20simple%20query%3A%20%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3ESELECT%20SUM(Data)%20%3CBR%20%2F%3E%20FROM%20T%20%3CBR%20%2F%3E%20WHERE%20RandKey%20%26lt%3B%201000%3C%2FP%3E%0A%20%20%3C%2FBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3E%7C--Compute%20Scalar(DEFINE%3A(%5BExpr1003%5D%3DCASE%20WHEN%20%5BExpr1011%5D%3D(0)%20THEN%20NULL%20ELSE%20%5BExpr1012%5D%20END))%20%3CBR%20%2F%3E%20%7C--Stream%20Aggregate(DEFINE%3A(%5BExpr1011%5D%3DCOUNT_BIG(%5BT%5D.%5BData%5D)%2C%20%5BExpr1012%5D%3DSUM(%5BT%5D.%5BData%5D)))%20%3CBR%20%2F%3E%20%7C--Nested%20Loops(Inner%20Join%2C%20OUTER%20REFERENCES%3A(%5BT%5D.%5BPK%5D%2C%20%5BExpr1010%5D)%20%3CB%3E%20OPTIMIZED%20%3C%2FB%3E%20WITH%20UNORDERED%20PREFETCH)%20%3CBR%20%2F%3E%20%7C--Index%20Seek(OBJECT%3A(%5BT%5D.%5BIRandKey%5D)%2C%20SEEK%3A(%5BT%5D.%5BRandKey%5D%20%26lt%3B%20(1000))%20ORDERED%20FORWARD)%20%3CBR%20%2F%3E%20%7C--Clustered%20Index%20Seek(OBJECT%3A(%5BT%5D.%5BPK__T__...%5D)%2C%20SEEK%3A(%5BT%5D.%5BPK%5D%3D%5BT%5D.%5BPK%5D)%20LOOKUP%20ORDERED%20FORWARD)%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ENotice%20that%20the%20nested%20loops%20join%20includes%20an%20extra%20keyword%3A%20OPTIMIZED.%26nbsp%3B%20This%20keyword%20indicates%20that%20the%20nested%20loops%20join%20may%20try%20to%20reorder%20the%20input%20rows%20to%20improve%20I%2FO%20performance.%26nbsp%3B%20This%20behavior%20is%20similar%20to%20the%20explicit%20sorts%20that%20we%20saw%20in%20my%20two%20previous%20posts%2C%20but%20unlike%20a%20full%20sort%20it%20is%20more%20of%20a%20best%20effort.%26nbsp%3B%20That%20is%2C%20the%20results%20from%20an%20optimized%20nested%20loops%20join%20may%20not%20be%20(and%20in%20fact%20are%20highly%20unlikely%20to%20be)%20fully%20sorted.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ESQL%20Server%20only%20uses%20an%20optimized%20nested%20loops%20join%20when%20the%20optimizer%20concludes%20based%20on%20its%20cardinality%20and%20cost%20estimates%20that%20a%20sort%20is%20most%20likely%20not%20required%2C%20but%20where%20there%20is%20still%20a%20possibility%26nbsp%3B%26nbsp%3B%20that%20a%20sort%20could%20be%20helpful%20in%20the%20event%20that%20the%20cardinality%20or%20cost%20estimates%20are%20incorrect.%26nbsp%3B%20In%20other%20words%2C%20an%20optimized%20nested%20loops%20join%20may%20be%20thought%20of%20as%20a%20%22safety%20net%22%20for%20those%20cases%20where%20SQL%20Server%20chooses%20a%20nested%20loops%20join%20but%20would%20have%20done%20better%20to%20have%20chosen%20an%20alternative%20plan%20such%20as%20a%20full%20scan%20or%20a%20nested%20loops%20join%20with%20an%20explicit%20sort.%26nbsp%3B%20For%20the%20above%20query%20which%20only%20joins%20a%20few%20rows%2C%20the%20optimization%20is%20unlikely%20to%20have%20any%20impact%20at%20all.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ELet's%20look%20at%20an%20example%20where%20the%20optimization%20actually%20helps%3A%3C%2FP%3E%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3ESELECT%20SUM(Data)%20%3CBR%20%2F%3E%20FROM%20T%20%3CBR%20%2F%3E%20WHERE%20RandKey%20%26lt%3B%20100000000%20AND%20%3CBR%20%2F%3E%20Flags%20%26amp%3B%200x1%20%3D%200x1%20AND%20%3CBR%20%2F%3E%20Flags%20%26amp%3B%200x2%20%3D%200x2%20AND%20%3CBR%20%2F%3E%20Flags%20%26amp%3B%200x4%20%3D%200x4%20AND%20%3CBR%20%2F%3E%20Flags%20%26amp%3B%200x8%20%3D%200x8%3C%2FP%3E%0A%20%20%3C%2FBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3E%7C--Compute%20Scalar(DEFINE%3A(%5BExpr1003%5D%3DCASE%20WHEN%20%5BExpr1014%5D%3D(0)%20THEN%20NULL%20ELSE%20%5BExpr1015%5D%20END))%20%3CBR%20%2F%3E%20%7C--Stream%20Aggregate(DEFINE%3A(%5BExpr1014%5D%3DCOUNT_BIG(%5BT%5D.%5BData%5D)%2C%20%5BExpr1015%5D%3DSUM(%5BT%5D.%5BData%5D)))%20%3CBR%20%2F%3E%20%7C--Nested%20Loops(Inner%20Join%2C%20OUTER%20REFERENCES%3A(%5BT%5D.%5BPK%5D%2C%20%5BExpr1013%5D)%20%3CB%3E%20OPTIMIZED%20%3C%2FB%3E%20WITH%20UNORDERED%20PREFETCH)%20%3CBR%20%2F%3E%20%7C--Index%20Seek(OBJECT%3A(%5BT%5D.%5BIRandKey%5D)%2C%20SEEK%3A(%5BT%5D.%5BRandKey%5D%20%26lt%3B%20(100000000))%2C%26nbsp%3B%20WHERE%3A((%5BT%5D.%5BFlags%5D%26amp%3B(1))%3D(1)%20AND%20(%5BT%5D.%5BFlags%5D%26amp%3B(2))%3D(2)%20AND%20(%5BT%5D.%5BFlags%5D%26amp%3B(4))%3D(4)%20AND%20(%5BT%5D.%5BFlags%5D%26amp%3B(8))%3D(8))%20ORDERED%20FORWARD)%20%3CBR%20%2F%3E%20%7C--Clustered%20Index%20Seek(OBJECT%3A(%5BT%5D.%5BPK__T__...%5D)%2C%20SEEK%3A(%5BT%5D.%5BPK%5D%3D%5BT%5D.%5BPK%5D)%20LOOKUP%20ORDERED%20FORWARD)%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EThe%20Flags%20column%20contains%20the%20value%200xFF%20in%20every%20row.%26nbsp%3B%20Thus%2C%20every%20one%20of%20the%20bitwise%20AND%20predicates%20evaluates%20to%20true%20and%20this%20query%20returns%20about%202.5%20million%20rows%20or%2010%25%20of%20the%20table.%26nbsp%3B%20Ordinarily%2C%20when%20faced%20with%20a%20query%20like%20this%20one%2C%20SQL%20Server%20would%20resort%20to%20a%20sequential%20scan%20of%20the%20entire%20table.%26nbsp%3B%20Indeed%2C%20if%20you%20try%20this%20query%20without%20the%20extra%20bitwise%20filters%2C%20you%20will%20get%20a%20sequential%20scan.%26nbsp%3B%20However%2C%20SQL%20Server%20does%20not%20realize%20that%20these%20predicates%20are%20always%20true%2C%20estimates%20a%20much%20lower%20cardinality%20of%20less%20than%2010%2C000%20rows%2C%20and%20chooses%20a%20simple%20nested%20loops%20join%20plan.%26nbsp%3B%20Note%20that%20I%20would%20generally%20recommend%20against%20using%20predicates%20like%20these%20ones%20in%20a%20real%20world%20application%20precisely%20because%20they%20will%20lead%20to%20cardinality%20estimation%20errors%20and%20poor%20plans.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ETo%20see%20what%20effect%20the%20optimized%20nested%20loops%20join%20has%2C%20let's%20compare%20the%20above%20plan%20with%20an%20%22un-optimized%22%20nested%20loops%20join.%26nbsp%3B%20We%20can%20eliminate%20the%20optimization%20by%20using%20the%20following%20UPDATE%20STATISTICS%20statement%20to%20trick%20SQL%20Server%20into%20believing%20that%20the%20table%20is%20very%20small%3A%3C%2FP%3E%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3EUPDATE%20STATISTICS%20T%20WITH%20ROWCOUNT%20%3D%201%2C%20PAGECOUNT%20%3D%201%3C%2FP%3E%0A%20%20%3C%2FBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3EI'll%20compare%20the%20above%20query%20with%20the%20following%20simpler%20query%20which%20uses%20essentially%20the%20same%20plan%20and%20touches%20the%20same%20data%20but%20has%20an%20%22un-optimized%22%20nested%20loops%20join%3A%3C%2FP%3E%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3ESELECT%20SUM(Data)%20%3CBR%20%2F%3E%20FROM%20T%20WITH%20(INDEX%20(IRandKey))%20%3CBR%20%2F%3E%20WHERE%20RandKey%20%26lt%3B%20100000000%3C%2FP%3E%0A%20%20%3C%2FBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3E%7C--Compute%20Scalar(DEFINE%3A(%5BExpr1003%5D%3DCASE%20WHEN%20%5BExpr1009%5D%3D(0)%20THEN%20NULL%20ELSE%20%5BExpr1010%5D%20END))%20%3CBR%20%2F%3E%20%7C--Stream%20Aggregate(DEFINE%3A(%5BExpr1009%5D%3DCOUNT_BIG(%5BT%5D.%5BData%5D)%2C%20%5BExpr1010%5D%3DSUM(%5BT%5D.%5BData%5D)))%20%3CBR%20%2F%3E%20%7C--Nested%20Loops(Inner%20Join%2C%20OUTER%20REFERENCES%3A(%5BT%5D.%5BPK%5D))%20%3CBR%20%2F%3E%20%7C--Index%20Seek(OBJECT%3A(%5BT%5D.%5BIRandKey%5D)%2C%20SEEK%3A(%5BT%5D.%5BRandKey%5D%20%26lt%3B%20(100000000))%20ORDERED%20FORWARD)%20%3CBR%20%2F%3E%20%7C--Clustered%20Index%20Seek(OBJECT%3A(%5BT%5D.%5BPK__T__...%5D)%2C%20SEEK%3A(%5BT%5D.%5BPK%5D%3D%5BT%5D.%5BPK%5D)%20LOOKUP%20ORDERED%20FORWARD)%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EWe%20can%20reset%20the%20statistics%20using%20the%20following%20statement%3A%3C%2FP%3E%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3EUPDATE%20STATISTICS%20T%20WITH%20ROWCOUNT%20%3D%2025600000%2C%20PAGECOUNT%20%3D%20389323%3C%2FP%3E%0A%20%20%3C%2FBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3EAs%20in%20my%20last%20post%2C%20I'm%20going%20to%20simulate%20a%20larger%20table%20by%20reducing%20the%20memory%20available%20to%20the%20server%20to%201%20GByte%20with%20SP_CONFIGURE%20'MAX%20SERVER%20MEMORY'%20and%20I'm%20also%20going%20to%20flush%20the%20buffer%20pool%20between%20runs%20with%20DBCC%20DROPCLEANBUFFERS.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%20Note%20that%20you%20will%20NOT%20want%20to%20run%20these%20statements%20on%20a%20production%20server.%20%3C%2FB%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EI%20ran%20both%20of%20the%20above%20queries%20with%20three%20different%20constants.%26nbsp%3B%20Here%20are%20my%20results.%26nbsp%3B%20Keep%20in%20mind%20that%20these%20results%20depend%20greatly%20on%20the%20specific%20hardware.%26nbsp%3B%20If%20you%20try%20this%20experiment%2C%20your%20results%20may%20vary.%3C%2FP%3E%3CBR%20%2F%3E%3CTABLE%3E%0A%20%20%20%3CTBODY%3E%3CTR%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%20Execution%20Time%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%20Increase%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%20OPTIMIZED%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%20%22un-OPTIMIZED%22%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%20Constant%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%2010%2C000%2C000%20%3CBR%20%2F%3E%20(1%25%20of%20rows)%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E6.5%20minutes%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E26%20minutes%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E4x%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%20100%2C000%2C000%20%3CBR%20%2F%3E%20(10%25%20of%20rows)%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E10.4%20minutes%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E4.3%20hours%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E25x%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E%3CB%3E%20250%2C000%2C000%20%3CBR%20%2F%3E%20(25%25%20of%20rows)%20%3C%2FB%3E%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E11.3%20minutes%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E10.6%20hours%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%20%3CTD%3E%3CBR%20%2F%3E%3CP%3E56x%3C%2FP%3E%0A%20%20%20%20%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%3C%2FTBODY%3E%3C%2FTABLE%3E%3CBR%20%2F%3E%3CP%3EClearly%20the%20optimized%20nested%20loops%20join%20can%20have%20a%20huge%20impact%20on%20performance.%26nbsp%3B%20Moreover%2C%20as%20the%20plan%20touches%20more%20rows%20the%20benefit%20of%20the%20optimization%20grows%20dramatically.%26nbsp%3B%20Although%20a%20full%20scan%20or%20a%20nested%20loops%20join%20with%20an%20explicit%20sort%20would%20be%20faster%2C%20the%20optimized%20nested%20loops%20join%20really%20is%20a%20safety%20net%20protecting%20against%20a%20much%20worse%20alternative.%3C%2FP%3E%0A%20%0A%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-383537%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Mar%2018%2C%202009%20In%20my%20past%20two%20posts%2C%20I%20explained%20how%20SQL%20Server%20may%20add%20a%20sort%20to%20the%20outer%20side%20of%20a%20nested%20loops%20join%20and%20showed%20how%20this%20sort%20can%20significantly%20improve%20performance.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-383537%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESQLServerQueryProcessing%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft
First published on MSDN on Mar 18, 2009
In my past two posts, I explained how SQL Server may add a sort to the outer side of a nested loops join and showed how this sort can significantly improve performance .  In an earlier post , I discussed how SQL Server can use random prefetching to improve the performance of a nested loops join.  In this post, I'm going to explore one more nested loops join performance feature.  I'll use the same database that I used in my two prior posts .  Let's start with the following simple query:

SELECT SUM(Data)
FROM T
WHERE RandKey < 1000


|--Compute Scalar(DEFINE:([Expr1003]=CASE WHEN [Expr1011]=(0) THEN NULL ELSE [Expr1012] END))
|--Stream Aggregate(DEFINE:([Expr1011]=COUNT_BIG([T].[Data]), [Expr1012]=SUM([T].[Data])))
|--Nested Loops(Inner Join, OUTER REFERENCES:([T].[PK], [Expr1010]) OPTIMIZED WITH UNORDERED PREFETCH)
|--Index Seek(OBJECT:([T].[IRandKey]), SEEK:([T].[RandKey] < (1000)) ORDERED FORWARD)
|--Clustered Index Seek(OBJECT:([T].[PK__T__...]), SEEK:([T].[PK]=[T].[PK]) LOOKUP ORDERED FORWARD)


Notice that the nested loops join includes an extra keyword: OPTIMIZED.  This keyword indicates that the nested loops join may try to reorder the input rows to improve I/O performance.  This behavior is similar to the explicit sorts that we saw in my two previous posts, but unlike a full sort it is more of a best effort.  That is, the results from an optimized nested loops join may not be (and in fact are highly unlikely to be) fully sorted.


SQL Server only uses an optimized nested loops join when the optimizer concludes based on its cardinality and cost estimates that a sort is most likely not required, but where there is still a possibility   that a sort could be helpful in the event that the cardinality or cost estimates are incorrect.  In other words, an optimized nested loops join may be thought of as a "safety net" for those cases where SQL Server chooses a nested loops join but would have done better to have chosen an alternative plan such as a full scan or a nested loops join with an explicit sort.  For the above query which only joins a few rows, the optimization is unlikely to have any impact at all.


Let's look at an example where the optimization actually helps:



SELECT SUM(Data)
FROM T
WHERE RandKey < 100000000 AND
Flags & 0x1 = 0x1 AND
Flags & 0x2 = 0x2 AND
Flags & 0x4 = 0x4 AND
Flags & 0x8 = 0x8


|--Compute Scalar(DEFINE:([Expr1003]=CASE WHEN [Expr1014]=(0) THEN NULL ELSE [Expr1015] END))
|--Stream Aggregate(DEFINE:([Expr1014]=COUNT_BIG([T].[Data]), [Expr1015]=SUM([T].[Data])))
|--Nested Loops(Inner Join, OUTER REFERENCES:([T].[PK], [Expr1013]) OPTIMIZED WITH UNORDERED PREFETCH)
|--Index Seek(OBJECT:([T].[IRandKey]), SEEK:([T].[RandKey] < (100000000)),  WHERE:(([T].[Flags]&(1))=(1) AND ([T].[Flags]&(2))=(2) AND ([T].[Flags]&(4))=(4) AND ([T].[Flags]&(8))=(8)) ORDERED FORWARD)
|--Clustered Index Seek(OBJECT:([T].[PK__T__...]), SEEK:([T].[PK]=[T].[PK]) LOOKUP ORDERED FORWARD)


The Flags column contains the value 0xFF in every row.  Thus, every one of the bitwise AND predicates evaluates to true and this query returns about 2.5 million rows or 10% of the table.  Ordinarily, when faced with a query like this one, SQL Server would resort to a sequential scan of the entire table.  Indeed, if you try this query without the extra bitwise filters, you will get a sequential scan.  However, SQL Server does not realize that these predicates are always true, estimates a much lower cardinality of less than 10,000 rows, and chooses a simple nested loops join plan.  Note that I would generally recommend against using predicates like these ones in a real world application precisely because they will lead to cardinality estimation errors and poor plans.


To see what effect the optimized nested loops join has, let's compare the above plan with an "un-optimized" nested loops join.  We can eliminate the optimization by using the following UPDATE STATISTICS statement to trick SQL Server into believing that the table is very small:



UPDATE STATISTICS T WITH ROWCOUNT = 1, PAGECOUNT = 1


I'll compare the above query with the following simpler query which uses essentially the same plan and touches the same data but has an "un-optimized" nested loops join:



SELECT SUM(Data)
FROM T WITH (INDEX (IRandKey))
WHERE RandKey < 100000000


|--Compute Scalar(DEFINE:([Expr1003]=CASE WHEN [Expr1009]=(0) THEN NULL ELSE [Expr1010] END))
|--Stream Aggregate(DEFINE:([Expr1009]=COUNT_BIG([T].[Data]), [Expr1010]=SUM([T].[Data])))
|--Nested Loops(Inner Join, OUTER REFERENCES:([T].[PK]))
|--Index Seek(OBJECT:([T].[IRandKey]), SEEK:([T].[RandKey] < (100000000)) ORDERED FORWARD)
|--Clustered Index Seek(OBJECT:([T].[PK__T__...]), SEEK:([T].[PK]=[T].[PK]) LOOKUP ORDERED FORWARD)


We can reset the statistics using the following statement:



UPDATE STATISTICS T WITH ROWCOUNT = 25600000, PAGECOUNT = 389323


As in my last post, I'm going to simulate a larger table by reducing the memory available to the server to 1 GByte with SP_CONFIGURE 'MAX SERVER MEMORY' and I'm also going to flush the buffer pool between runs with DBCC DROPCLEANBUFFERS.


Note that you will NOT want to run these statements on a production server.


I ran both of the above queries with three different constants.  Here are my results.  Keep in mind that these results depend greatly on the specific hardware.  If you try this experiment, your results may vary.




Execution Time


Increase


OPTIMIZED


"un-OPTIMIZED"


Constant


10,000,000
(1% of rows)


6.5 minutes


26 minutes


4x


100,000,000
(10% of rows)


10.4 minutes


4.3 hours


25x


250,000,000
(25% of rows)


11.3 minutes


10.6 hours


56x


Clearly the optimized nested loops join can have a huge impact on performance.  Moreover, as the plan touches more rows the benefit of the optimization grows dramatically.  Although a full scan or a nested loops join with an explicit sort would be faster, the optimized nested loops join really is a safety net protecting against a much worse alternative.