Home
%3CLINGO-SUB%20id%3D%22lingo-sub-383453%22%20slang%3D%22en-US%22%3ESubqueries%20in%20BETWEEN%20and%20CASE%20Statements%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-383453%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%3E%20First%20published%20on%20MSDN%20on%20Jun%2027%2C%202008%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CP%3EConsider%20the%20following%20query%3A%3C%2FP%3E%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3ECREATE%20TABLE%20T1%20(A%20INT%2C%20B1%20INT%2C%20B2%20INT)%20%3CBR%20%2F%3E%20CREATE%20TABLE%20T2%20(A%20INT%2C%20B%20INT)%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ESELECT%20*%20%3CBR%20%2F%3E%20FROM%20T1%20%3CBR%20%2F%3E%20WHERE%20(SELECT%20SUM(T2.B)%20FROM%20T2%20WHERE%20T2.A%20%3D%20T1.A)%20BETWEEN%20T1.B1%20AND%20T1.B2%3C%2FP%3E%0A%20%20%3C%2FBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3EObserve%20that%20the%20subquery%20in%20this%20query%20only%20needs%20to%20be%20evaluated%20once%20for%20each%20row%20of%20T1.%26nbsp%3B%20Indeed%20running%20on%20SQL%20Server%202000%2C%20we%20get%20the%20following%20plan%20(with%20the%20portion%20of%20the%20plan%20corresponding%20to%20the%20subquery%20in%20%3CB%3E%20bold%20%3C%2FB%3E%20%3A(%3C%2Fimg%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7C--Nested%20Loops(Inner%20Join%2C%20OUTER%20REFERENCES%3A(%5BT2%5D.%5BA%5D%2C%20%5BExpr1004%5D))%20%3CBR%20%2F%3E%20%3CB%3E%20%7C--Compute%20Scalar(DEFINE%3A(%5BExpr1004%5D%3DIf%20(%5BExpr1014%5D%3D0)%20then%20NULL%20else%20%5BExpr1015%5D))%20%3CBR%20%2F%3E%20%3C%2FB%3E%20%3CB%3E%20%7C%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C--Stream%20Aggregate(GROUP%20BY%3A(%5BT2%5D.%5BA%5D)%20DEFINE%3A(%5BExpr1014%5D%3DCOUNT_BIG(%5BT2%5D.%5BB%5D)%2C%20%5BExpr1015%5D%3DSUM(%5BT2%5D.%5BB%5D)))%20%3CBR%20%2F%3E%20%3C%2FB%3E%20%3CB%3E%20%7C%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C--Sort(ORDER%20BY%3A(%5BT2%5D.%5BA%5D%20ASC))%20%3CBR%20%2F%3E%20%3C%2FB%3E%20%3CB%3E%20%7C%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C--Table%20Scan(OBJECT%3A(%5BT2%5D))%20%3CBR%20%2F%3E%20%3C%2FB%3E%20%7C--Filter(WHERE%3A(%5BExpr1004%5D%26lt%3B%3D%5BT1%5D.%5BB2%5D))%20%3CBR%20%2F%3E%20%7C--Index%20Spool(SEEK%3A(%5BT1%5D.%5BA%5D%3D%5BT2%5D.%5BA%5D%20AND%20%5BT1%5D.%5BB1%5D%20%26lt%3B%3D%20%5BExpr1004%5D))%20%3CBR%20%2F%3E%20%7C--Table%20Scan(OBJECT%3A(%5BT1%5D))%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ENow%20let's%20look%20at%20the%20query%20plan%20we%20get%20(with%20SQL%20Server%202005%20or%20SQL%20Server%202008)%3A%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7C--Nested%20Loops(Inner%20Join%2C%20OUTER%20REFERENCES%3A(%5BT2%5D.%5BA%5D%2C%20%5BExpr1008%5D%2C%20%5BExpr1014%5D))%20%3CBR%20%2F%3E%20%7C--Merge%20Join(Inner%20Join%2C%20MERGE%3A(%5BT2%5D.%5BA%5D)%3D(%5BT2%5D.%5BA%5D)%2C%20RESIDUAL%3A(%5BT2%5D.%5BA%5D%3D%5BT2%5D.%5BA%5D))%20%3CBR%20%2F%3E%20%3CSTRONG%3E%20%7C%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C--Compute%20Scalar(DEFINE%3A(%5BExpr1008%5D%3DCASE%20WHEN%20%5BExpr1026%5D%3D(0)%20THEN%20NULL%20ELSE%20%5BExpr1027%5D%20END))%20%3CBR%20%2F%3E%20%7C%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C--Stream%20Aggregate(GROUP%20BY%3A(%5BT2%5D.%5BA%5D)%20DEFINE%3A(%5BExpr1026%5D%3DCOUNT_BIG(%5BT2%5D.%5BB%5D)%2C%20%5BExpr1027%5D%3DSUM(%5BT2%5D.%5BB%5D)))%20%3CBR%20%2F%3E%20%7C%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C--Sort(ORDER%20BY%3A(%5BT2%5D.%5BA%5D%20ASC))%20%3CBR%20%2F%3E%20%7C%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C--Table%20Scan(OBJECT%3A(%5BT2%5D))%20%3CBR%20%2F%3E%20%7C%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C--Compute%20Scalar(DEFINE%3A(%5BExpr1014%5D%3DCASE%20WHEN%20%5BExpr1028%5D%3D(0)%20THEN%20NULL%20ELSE%20%5BExpr1029%5D%20END))%20%3CBR%20%2F%3E%20%7C%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C--Stream%20Aggregate(GROUP%20BY%3A(%5BT2%5D.%5BA%5D)%20DEFINE%3A(%5BExpr1028%5D%3DCOUNT_BIG(%5BT2%5D.%5BB%5D)%2C%20%5BExpr1029%5D%3DSUM(%5BT2%5D.%5BB%5D)))%20%3CBR%20%2F%3E%20%7C%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C--Sort(ORDER%20BY%3A(%5BT2%5D.%5BA%5D%20ASC))%20%3CBR%20%2F%3E%20%7C%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C--Table%20Scan(OBJECT%3A(%5BT2%5D))%20%3CBR%20%2F%3E%20%3C%2FSTRONG%3E%20%7C--Filter(WHERE%3A(%5BExpr1014%5D%26lt%3B%3D%5BT1%5D.%5BB2%5D))%20%3CBR%20%2F%3E%20%7C--Index%20Spool(SEEK%3A(%5BT1%5D.%5BA%5D%3D%5BT2%5D.%5BA%5D%20AND%20%5BT1%5D.%5BB1%5D%20%26lt%3B%3D%20%5BExpr1008%5D))%20%3CBR%20%2F%3E%20%7C--Table%20Scan(OBJECT%3A(%5BT1%5D))%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ENotice%20that%20the%20subquery%20is%20actually%20evaluated%20twice.%26nbsp%3B%20There%20are%20two%20scans%20of%20T2%2C%20two%20sorts%2C%20and%20two%20stream%20aggregates.%26nbsp%3B%20I've%20highlighted%20both%20sets%20of%20operators%20in%20%3CB%3E%20bold%20%3C%2FB%3E%20.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EWhy%20is%20SQL%20Server%20evaluating%20the%20subquery%20twice%3F%26nbsp%3B%20The%20answer%20is%20that%20SQL%20Server%20transforms%20%22X%20BETWEEEN%20Y%20AND%20Z%22%20into%20%22X%20%26lt%3B%3D%20Y%20AND%20X%20%26gt%3B%3D%20Z%22.%26nbsp%3B%20If%20as%20in%20this%20query%2C%20X%20is%20a%20subquery%2C%20the%20subquery%20is%20repeated%3A%3C%2FP%3E%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3ESELECT%20*%20%3CBR%20%2F%3E%20FROM%20T1%20%3CBR%20%2F%3E%20WHERE%20(SELECT%20SUM(T2.B)%20FROM%20T2%20WHERE%20T2.A%20%3D%20T1.A)%20%26gt%3B%3D%20T1.B1%20AND%20%3CBR%20%2F%3E%20(SELECT%20SUM(T2.B)%20FROM%20T2%20WHERE%20T2.A%20%3D%20T1.A)%20%26lt%3B%3D%20T1.B2%3C%2FP%3E%0A%20%20%3C%2FBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3EThis%20transformation%20occurs%20very%20early%20in%20the%20processing%20of%20the%20query.%26nbsp%3B%20Unfortunately%2C%20after%20that%20point%2C%20SQL%20Server%202005%20and%20SQL%20Server%202008%20do%20not%20realize%20that%20the%20subquery%20is%20a%20common%20subexpression%20and%20they%20evaluate%20it%20as%20if%20there%20were%20two%20completely%20different%20subqueries.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EYou%20may%20also%20have%20noticed%20that%2C%20instead%20of%20scanning%20T1%20first%20and%20then%20evaluating%20the%20subquery%20for%20each%20row%20of%20T1%2C%20this%20plan%20actually%20scans%20T2%20first.%26nbsp%3B%20This%20join%20order%20results%20from%20the%20optimizer%20%3CA%20href%3D%22http%3A%2F%2Fblogs.msdn.com%2Fcraigfr%2Farchive%2F2006%2F10%2F04%2FDecorrelating-Subqueries.aspx%22%20title%3D%22Decorrelating%20Subqueries%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20decorrelating%20the%20subqueries%20%3C%2FA%3E%20.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ESo%2C%20how%20can%20we%20get%20SQL%20Server%20to%20evaluate%20the%20subquery%20only%20once%3F%26nbsp%3B%20There%20are%20actually%20multiple%20solutions%20and%20all%20involve%20rewriting%20the%20query%20to%20calculate%20the%20subquery%20separately%20from%20the%20BETWEEN%20clause%20so%20that%20when%20SQL%20Server%20transforms%20the%20BETWEEN%20clause%2C%20it%20does%20not%20also%20duplicate%20the%20subquery.%26nbsp%3B%20Here%20are%20some%20examples%3A%3C%2FP%3E%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3ESELECT%20Q.A%2C%20Q.B1%2C%20Q.B2%20%3CBR%20%2F%3E%20FROM%20%3CBR%20%2F%3E%20(%20%3CBR%20%2F%3E%20SELECT%20*%2C%20(SELECT%20SUM(T2.B)%20FROM%20T2%20WHERE%20T2.A%20%3D%20T1.A)%20SUM_B%20%3CBR%20%2F%3E%20FROM%20T1%20%3CBR%20%2F%3E%20)%20Q%20%3CBR%20%2F%3E%20WHERE%20SUM_B%20BETWEEN%20Q.B1%20AND%20Q.B2%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ESELECT%20T1.*%20%3CBR%20%2F%3E%20FROM%20T1%20CROSS%20APPLY%20(SELECT%20SUM(T2.B)%20SUM_B%20FROM%20T2%20WHERE%20T2.A%20%3D%20T1.A)%20Q%20%3CBR%20%2F%3E%20WHERE%20Q.SUM_B%20BETWEEN%20T1.B1%20AND%20T1.B2%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ESELECT%20T1.*%20%3CBR%20%2F%3E%20FROM%20T1%2C%20(SELECT%20T2.A%2C%20SUM(T2.B)%20SUM_B%20FROM%20T2%20GROUP%20BY%20T2.A)%20Q%20%3CBR%20%2F%3E%20WHERE%20T1.A%20%3D%20Q.A%20AND%20Q.SUM_B%20BETWEEN%20T1.B1%20AND%20T1.B2%3C%2FP%3E%0A%20%20%3C%2FBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3EAll%20three%20of%20these%20rewrites%20produce%20the%20same%20plan%3A%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7C--Nested%20Loops(Inner%20Join%2C%20OUTER%20REFERENCES%3A(%5BT2%5D.%5BA%5D%2C%20%5BExpr1008%5D))%20%3CBR%20%2F%3E%20%3CB%3E%20%7C--Compute%20Scalar(DEFINE%3A(%5BExpr1008%5D%3DCASE%20WHEN%20%5BExpr1016%5D%3D(0)%20THEN%20NULL%20ELSE%20%5BExpr1017%5D%20END))%20%3CBR%20%2F%3E%20%3C%2FB%3E%20%3CB%3E%20%7C%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C--Stream%20Aggregate(GROUP%20BY%3A(%5BT2%5D.%5BA%5D)%20DEFINE%3A(%5BExpr1016%5D%3DCOUNT_BIG(%5BT2%5D.%5BB%5D)%2C%20%5BExpr1017%5D%3DSUM(%5BT2%5D.%5BB%5D)))%20%3CBR%20%2F%3E%20%3C%2FB%3E%20%3CB%3E%20%7C%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C--Sort(ORDER%20BY%3A(%5BT2%5D.%5BA%5D%20ASC))%20%3CBR%20%2F%3E%20%3C%2FB%3E%20%3CB%3E%20%7C%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C--Table%20Scan(OBJECT%3A(%5BT2%5D))%20%3CBR%20%2F%3E%20%3C%2FB%3E%20%7C--Filter(WHERE%3A(%5BExpr1008%5D%26lt%3B%3D%5BT1%5D.%5BB2%5D))%20%3CBR%20%2F%3E%20%7C--Index%20Spool(SEEK%3A(%5BT1%5D.%5BA%5D%3D%5BT2%5D.%5BA%5D%20AND%20%5BT1%5D.%5BB1%5D%20%26lt%3B%3D%20%5BExpr1008%5D))%20%3CBR%20%2F%3E%20%7C--Table%20Scan(OBJECT%3A(%5BT1%5D))%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ESQL%20Server%20also%20transforms%20a%20CASE%20statement%20of%20the%20form%3A%3C%2FP%3E%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3ECASE%20X%20%3CBR%20%2F%3E%20WHEN%20Y1%20THEN%20Z1%20%3CBR%20%2F%3E%20WHEN%20Y2%20THEN%20Z2%20%3CBR%20%2F%3E%20...%20%3CBR%20%2F%3E%20ELSE%20ZN%20%3CBR%20%2F%3E%20END%3C%2FP%3E%0A%20%20%3C%2FBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3EInto%3A%3C%2FP%3E%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3ECASE%20%3CBR%20%2F%3E%20WHEN%20X%20%3D%20Y1%20THEN%20Z1%20%3CBR%20%2F%3E%20WHEN%20X%20%3D%20Y2%20THEN%20Z2%20%3CBR%20%2F%3E%20...%20%3CBR%20%2F%3E%20ELSE%20ZN%20%3CBR%20%2F%3E%20END%3C%2FP%3E%0A%20%20%3C%2FBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3EThus%2C%20CASE%20statements%20can%20yield%20the%20same%20problematic%20behavior%20if%20X%20is%20a%20subquery.%26nbsp%3B%20Unfortunately%2C%20with%20a%20CASE%20statement%2C%20the%20number%20of%20times%20that%20the%20subquery%20is%20reevaluated%20depends%20on%20the%20number%20WHEN%20clauses%20and%20can%20be%20quite%20large.%26nbsp%3B%20Here%20is%20a%20simple%20query%20that%20illustrates%20the%20problem%3A%3C%2FP%3E%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3ESELECT%20*%2C%20%3CBR%20%2F%3E%20CASE%20(SELECT%20SUM(T2.B)%20FROM%20T2%20WHERE%20T2.A%20%3D%20T1.A)%20%3CBR%20%2F%3E%20WHEN%20T1.B1%20THEN%20'B1'%20%3CBR%20%2F%3E%20WHEN%20T1.B2%20THEN%20'B2'%20%3CBR%20%2F%3E%20ELSE%20NULL%20%3CBR%20%2F%3E%20END%20CASE_B%20%3CBR%20%2F%3E%20FROM%20T1%3C%2FP%3E%0A%20%20%3C%2FBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3EHere%20is%20the%20SQL%20Server%202000%20plan%3A%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7C--Compute%20Scalar(DEFINE%3A(%5BExpr1007%5D%3DIf%20(%5BExpr1004%5D%3D%5BT1%5D.%5BB1%5D)%20then%20'B1'%20else%20If%20(%5BExpr1004%5D%3D%5BT1%5D.%5BB2%5D)%20then%20'B2'%20else%20NULL))%20%3CBR%20%2F%3E%20%7C--Nested%20Loops(Left%20Outer%20Join%2C%20OUTER%20REFERENCES%3A(%5BT1%5D.%5BA%5D))%20%3CBR%20%2F%3E%20%7C--Table%20Scan(OBJECT%3A(%5BT1%5D))%20%3CBR%20%2F%3E%20%7C--Hash%20Match(Cache%2C%20HASH%3A(%5BT1%5D.%5BA%5D)%2C%20RESIDUAL%3A(%5BT1%5D.%5BA%5D%3D%5BT1%5D.%5BA%5D))%20%3CBR%20%2F%3E%20%3CB%3E%20%7C--Compute%20Scalar(DEFINE%3A(%5BExpr1004%5D%3DIf%20(%5BExpr1020%5D%3D0)%20then%20NULL%20else%20%5BExpr1021%5D))%20%3CBR%20%2F%3E%20%3C%2FB%3E%20%3CB%3E%20%7C--Stream%20Aggregate(DEFINE%3A(%5BExpr1020%5D%3DCOUNT_BIG(%5BT2%5D.%5BB%5D)%2C%20%5BExpr1021%5D%3DSUM(%5BT2%5D.%5BB%5D)))%20%3CBR%20%2F%3E%20%3C%2FB%3E%20%3CB%3E%20%7C--Table%20Scan(OBJECT%3A(%5BT2%5D)%2C%20WHERE%3A(%5BT2%5D.%5BA%5D%3D%5BT1%5D.%5BA%5D))%3C%2FB%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EAnd%20here%20is%20the%20SQL%20Server%202005%20and%20SQL%20Server%202008%20plan%3A%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7C--Compute%20Scalar(DEFINE%3A(%5BExpr1016%5D%3DCASE%20WHEN%20%5BExpr1008%5D%3D%5BT1%5D.%5BB1%5D%20THEN%20'B1'%20ELSE%20CASE%20WHEN%20%5BExpr1014%5D%3D%5BT1%5D.%5BB2%5D%20THEN%20'B2'%20ELSE%20NULL%20END%20END))%20%3CBR%20%2F%3E%20%7C--Nested%20Loops(Inner%20Join%2C%20PASSTHRU%3A(%5BExpr1008%5D%3D%5BT1%5D.%5BB1%5D)%2C%20OUTER%20REFERENCES%3A(%5BT1%5D.%5BA%5D))%20%3CBR%20%2F%3E%20%7C--Nested%20Loops(Left%20Outer%20Join%2C%20OUTER%20REFERENCES%3A(%5BT1%5D.%5BA%5D))%20%3CBR%20%2F%3E%20%7C%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C--Table%20Scan(OBJECT%3A(%5BT1%5D))%20%3CBR%20%2F%3E%20%3CSTRONG%3E%20%7C%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C--Compute%20Scalar(DEFINE%3A(%5BExpr1008%5D%3DCASE%20WHEN%20%5BExpr1030%5D%3D(0)%20THEN%20NULL%20ELSE%20%5BExpr1031%5D%20END))%20%3CBR%20%2F%3E%20%7C%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C--Stream%20Aggregate(DEFINE%3A(%5BExpr1030%5D%3DCOUNT_BIG(%5BT2%5D.%5BB%5D)%2C%20%5BExpr1031%5D%3DSUM(%5BT2%5D.%5BB%5D)))%20%3CBR%20%2F%3E%20%7C%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C--Table%20Scan(OBJECT%3A(%5BT2%5D)%2C%20WHERE%3A(%5BT2%5D.%5BA%5D%3D%5BT1%5D.%5BA%5D))%20%3CBR%20%2F%3E%20%7C--Compute%20Scalar(DEFINE%3A(%5BExpr1014%5D%3DCASE%20WHEN%20%5BExpr1032%5D%3D(0)%20THEN%20NULL%20ELSE%20%5BExpr1033%5D%20END))%20%3CBR%20%2F%3E%20%7C--Stream%20Aggregate(DEFINE%3A(%5BExpr1032%5D%3DCOUNT_BIG(%5BT2%5D.%5BB%5D)%2C%20%5BExpr1033%5D%3DSUM(%5BT2%5D.%5BB%5D)))%20%3CBR%20%2F%3E%20%7C--Table%20Scan(OBJECT%3A(%5BT2%5D)%2C%20WHERE%3A(%5BT2%5D.%5BA%5D%3D%5BT1%5D.%5BA%5D))%3C%2FSTRONG%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EFinally%2C%20the%20same%20solutions%20once%20again%20apply%3A%3C%2FP%3E%3CBR%20%2F%3E%3CBLOCKQUOTE%3E%3CBR%20%2F%3E%3CP%3ESELECT%20Q.A%2C%20Q.B1%2C%20Q.B2%2C%20%3CBR%20%2F%3E%20CASE%20Q.SUM_B%20%3CBR%20%2F%3E%20WHEN%20Q.B1%20THEN%20'B1'%20%3CBR%20%2F%3E%20WHEN%20Q.B2%20THEN%20'B2'%20%3CBR%20%2F%3E%20ELSE%20NULL%20%3CBR%20%2F%3E%20END%20CASE_B%20%3CBR%20%2F%3E%20FROM%20%3CBR%20%2F%3E%20(%20%3CBR%20%2F%3E%20SELECT%20*%2C%20(SELECT%20SUM(T2.B)%20FROM%20T2%20WHERE%20T2.A%20%3D%20T1.A)%20SUM_B%20%3CBR%20%2F%3E%20FROM%20T1%20%3CBR%20%2F%3E%20)%20Q%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ESELECT%20T1.*%2C%20%3CBR%20%2F%3E%20CASE%20Q.SUM_B%20%3CBR%20%2F%3E%20WHEN%20T1.B1%20THEN%20'B1'%20%3CBR%20%2F%3E%20WHEN%20T1.B2%20THEN%20'B2'%20%3CBR%20%2F%3E%20ELSE%20NULL%20%3CBR%20%2F%3E%20END%20CASE_B%20%3CBR%20%2F%3E%20FROM%20T1%20CROSS%20APPLY%20(SELECT%20SUM(T2.B)%20SUM_B%20FROM%20T2%20WHERE%20T2.A%20%3D%20T1.A)%20Q%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ESELECT%20T1.*%2C%20%3CBR%20%2F%3E%20CASE%20Q.SUM_B%20%3CBR%20%2F%3E%20WHEN%20T1.B1%20THEN%20'B1'%20%3CBR%20%2F%3E%20WHEN%20T1.B2%20THEN%20'B2'%20%3CBR%20%2F%3E%20ELSE%20NULL%20%3CBR%20%2F%3E%20END%20CASE_B%20%3CBR%20%2F%3E%20FROM%20T1%2C%20(SELECT%20T2.A%2C%20SUM(T2.B)%20SUM_B%20FROM%20T2%20GROUP%20BY%20T2.A)%20Q%20%3CBR%20%2F%3E%20WHERE%20T1.A%20%3D%20Q.A%3C%2FP%3E%0A%20%20%3C%2FBLOCKQUOTE%3E%0A%20%0A%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-383453%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Jun%2027%2C%202008%20Consider%20the%20following%20query%3ACREATE%20TABLE%20T1%20(A%20INT%2C%20B1%20INT%2C%20B2%20INT)CREATE%20TABLE%20T2%20(A%20INT%2C%20B%20INT)SELECT%20*FROM%20T1WHERE%20(SELECT%20SUM(T2.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-383453%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESQLServerQueryProcessing%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft
First published on MSDN on Jun 27, 2008

Consider the following query:



CREATE TABLE T1 (A INT, B1 INT, B2 INT)
CREATE TABLE T2 (A INT, B INT)


SELECT *
FROM T1
WHERE (SELECT SUM(T2.B) FROM T2 WHERE T2.A = T1.A) BETWEEN T1.B1 AND T1.B2


Observe that the subquery in this query only needs to be evaluated once for each row of T1.  Indeed running on SQL Server 2000, we get the following plan (with the portion of the plan corresponding to the subquery in bold ):


|--Nested Loops(Inner Join, OUTER REFERENCES:([T2].[A], [Expr1004]))
|--Compute Scalar(DEFINE:([Expr1004]=If ([Expr1014]=0) then NULL else [Expr1015]))
|    |--Stream Aggregate(GROUP BY:([T2].[A]) DEFINE:([Expr1014]=COUNT_BIG([T2].[B]), [Expr1015]=SUM([T2].[B])))
|         |--Sort(ORDER BY:([T2].[A] ASC))
|              |--Table Scan(OBJECT:([T2]))
|--Filter(WHERE:([Expr1004]<=[T1].[B2]))
|--Index Spool(SEEK:([T1].[A]=[T2].[A] AND [T1].[B1] <= [Expr1004]))
|--Table Scan(OBJECT:([T1]))


Now let's look at the query plan we get (with SQL Server 2005 or SQL Server 2008):


|--Nested Loops(Inner Join, OUTER REFERENCES:([T2].[A], [Expr1008], [Expr1014]))
|--Merge Join(Inner Join, MERGE:([T2].[A])=([T2].[A]), RESIDUAL:([T2].[A]=[T2].[A]))
|    |--Compute Scalar(DEFINE:([Expr1008]=CASE WHEN [Expr1026]=(0) THEN NULL ELSE [Expr1027] END))
|    |    |--Stream Aggregate(GROUP BY:([T2].[A]) DEFINE:([Expr1026]=COUNT_BIG([T2].[B]), [Expr1027]=SUM([T2].[B])))
|    |         |--Sort(ORDER BY:([T2].[A] ASC))
|    |              |--Table Scan(OBJECT:([T2]))
|    |--Compute Scalar(DEFINE:([Expr1014]=CASE WHEN [Expr1028]=(0) THEN NULL ELSE [Expr1029] END))
|         |--Stream Aggregate(GROUP BY:([T2].[A]) DEFINE:([Expr1028]=COUNT_BIG([T2].[B]), [Expr1029]=SUM([T2].[B])))
|              |--Sort(ORDER BY:([T2].[A] ASC))
|                   |--Table Scan(OBJECT:([T2]))
|--Filter(WHERE:([Expr1014]<=[T1].[B2]))
|--Index Spool(SEEK:([T1].[A]=[T2].[A] AND [T1].[B1] <= [Expr1008]))
|--Table Scan(OBJECT:([T1]))


Notice that the subquery is actually evaluated twice.  There are two scans of T2, two sorts, and two stream aggregates.  I've highlighted both sets of operators in bold .


Why is SQL Server evaluating the subquery twice?  The answer is that SQL Server transforms "X BETWEEEN Y AND Z" into "X <= Y AND X >= Z".  If as in this query, X is a subquery, the subquery is repeated:



SELECT *
FROM T1
WHERE (SELECT SUM(T2.B) FROM T2 WHERE T2.A = T1.A) >= T1.B1 AND
(SELECT SUM(T2.B) FROM T2 WHERE T2.A = T1.A) <= T1.B2


This transformation occurs very early in the processing of the query.  Unfortunately, after that point, SQL Server 2005 and SQL Server 2008 do not realize that the subquery is a common subexpression and they evaluate it as if there were two completely different subqueries.


You may also have noticed that, instead of scanning T1 first and then evaluating the subquery for each row of T1, this plan actually scans T2 first.  This join order results from the optimizer decorrelating the subqueries .


So, how can we get SQL Server to evaluate the subquery only once?  There are actually multiple solutions and all involve rewriting the query to calculate the subquery separately from the BETWEEN clause so that when SQL Server transforms the BETWEEN clause, it does not also duplicate the subquery.  Here are some examples:



SELECT Q.A, Q.B1, Q.B2
FROM
(
SELECT *, (SELECT SUM(T2.B) FROM T2 WHERE T2.A = T1.A) SUM_B
FROM T1
) Q
WHERE SUM_B BETWEEN Q.B1 AND Q.B2


SELECT T1.*
FROM T1 CROSS APPLY (SELECT SUM(T2.B) SUM_B FROM T2 WHERE T2.A = T1.A) Q
WHERE Q.SUM_B BETWEEN T1.B1 AND T1.B2


SELECT T1.*
FROM T1, (SELECT T2.A, SUM(T2.B) SUM_B FROM T2 GROUP BY T2.A) Q
WHERE T1.A = Q.A AND Q.SUM_B BETWEEN T1.B1 AND T1.B2


All three of these rewrites produce the same plan:


|--Nested Loops(Inner Join, OUTER REFERENCES:([T2].[A], [Expr1008]))
|--Compute Scalar(DEFINE:([Expr1008]=CASE WHEN [Expr1016]=(0) THEN NULL ELSE [Expr1017] END))
|    |--Stream Aggregate(GROUP BY:([T2].[A]) DEFINE:([Expr1016]=COUNT_BIG([T2].[B]), [Expr1017]=SUM([T2].[B])))
|         |--Sort(ORDER BY:([T2].[A] ASC))
|              |--Table Scan(OBJECT:([T2]))
|--Filter(WHERE:([Expr1008]<=[T1].[B2]))
|--Index Spool(SEEK:([T1].[A]=[T2].[A] AND [T1].[B1] <= [Expr1008]))
|--Table Scan(OBJECT:([T1]))


SQL Server also transforms a CASE statement of the form:



CASE X
WHEN Y1 THEN Z1
WHEN Y2 THEN Z2
...
ELSE ZN
END


Into:



CASE
WHEN X = Y1 THEN Z1
WHEN X = Y2 THEN Z2
...
ELSE ZN
END


Thus, CASE statements can yield the same problematic behavior if X is a subquery.  Unfortunately, with a CASE statement, the number of times that the subquery is reevaluated depends on the number WHEN clauses and can be quite large.  Here is a simple query that illustrates the problem:



SELECT *,
CASE (SELECT SUM(T2.B) FROM T2 WHERE T2.A = T1.A)
WHEN T1.B1 THEN 'B1'
WHEN T1.B2 THEN 'B2'
ELSE NULL
END CASE_B
FROM T1


Here is the SQL Server 2000 plan:


|--Compute Scalar(DEFINE:([Expr1007]=If ([Expr1004]=[T1].[B1]) then 'B1' else If ([Expr1004]=[T1].[B2]) then 'B2' else NULL))
|--Nested Loops(Left Outer Join, OUTER REFERENCES:([T1].[A]))
|--Table Scan(OBJECT:([T1]))
|--Hash Match(Cache, HASH:([T1].[A]), RESIDUAL:([T1].[A]=[T1].[A]))
|--Compute Scalar(DEFINE:([Expr1004]=If ([Expr1020]=0) then NULL else [Expr1021]))
|--Stream Aggregate(DEFINE:([Expr1020]=COUNT_BIG([T2].[B]), [Expr1021]=SUM([T2].[B])))
|--Table Scan(OBJECT:([T2]), WHERE:([T2].[A]=[T1].[A]))


And here is the SQL Server 2005 and SQL Server 2008 plan:


|--Compute Scalar(DEFINE:([Expr1016]=CASE WHEN [Expr1008]=[T1].[B1] THEN 'B1' ELSE CASE WHEN [Expr1014]=[T1].[B2] THEN 'B2' ELSE NULL END END))
|--Nested Loops(Inner Join, PASSTHRU:([Expr1008]=[T1].[B1]), OUTER REFERENCES:([T1].[A]))
|--Nested Loops(Left Outer Join, OUTER REFERENCES:([T1].[A]))
|    |--Table Scan(OBJECT:([T1]))
|    |--Compute Scalar(DEFINE:([Expr1008]=CASE WHEN [Expr1030]=(0) THEN NULL ELSE [Expr1031] END))
|         |--Stream Aggregate(DEFINE:([Expr1030]=COUNT_BIG([T2].[B]), [Expr1031]=SUM([T2].[B])))
|              |--Table Scan(OBJECT:([T2]), WHERE:([T2].[A]=[T1].[A]))
|--Compute Scalar(DEFINE:([Expr1014]=CASE WHEN [Expr1032]=(0) THEN NULL ELSE [Expr1033] END))
|--Stream Aggregate(DEFINE:([Expr1032]=COUNT_BIG([T2].[B]), [Expr1033]=SUM([T2].[B])))
|--Table Scan(OBJECT:([T2]), WHERE:([T2].[A]=[T1].[A]))


Finally, the same solutions once again apply:



SELECT Q.A, Q.B1, Q.B2,
CASE Q.SUM_B
WHEN Q.B1 THEN 'B1'
WHEN Q.B2 THEN 'B2'
ELSE NULL
END CASE_B
FROM
(
SELECT *, (SELECT SUM(T2.B) FROM T2 WHERE T2.A = T1.A) SUM_B
FROM T1
) Q


SELECT T1.*,
CASE Q.SUM_B
WHEN T1.B1 THEN 'B1'
WHEN T1.B2 THEN 'B2'
ELSE NULL
END CASE_B
FROM T1 CROSS APPLY (SELECT SUM(T2.B) SUM_B FROM T2 WHERE T2.A = T1.A) Q


SELECT T1.*,
CASE Q.SUM_B
WHEN T1.B1 THEN 'B1'
WHEN T1.B2 THEN 'B2'
ELSE NULL
END CASE_B
FROM T1, (SELECT T2.A, SUM(T2.B) SUM_B FROM T2 GROUP BY T2.A) Q
WHERE T1.A = Q.A