Home
%3CLINGO-SUB%20id%3D%22lingo-sub-483037%22%20slang%3D%22en-US%22%3EThe%20case%20of%20the%20filter%20predicate%20with%20window%20functions%20such%20as%20ROW_NUMBER%20and%20RANK%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-483037%22%20slang%3D%22en-US%22%3E%3CP%3EI%20recently%20helped%20troubleshoot%20a%20query%20performance%20issue%20that%20involved%20the%20window%20function%20%22ROW_NUMBER%22.%20The%20running%20theory%20was%20that%20the%20query%E2%80%99s%20filter%20predicate%20should%20target%20the%20index%20resulting%20in%20a%20seek%2C%20but%20was%20instead%20applied%20via%20a%20Filter%20operator%20positioned%20at%20the%20top%20of%20the%20query%20plan.%20This%20led%20to%20a%20costly%20index%20scan%20and%20higher%20query%20duration.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhile%20the%20actual%20use%20case%20was%20a%20bit%20more%20complicated%2C%20it%20can%20be%20repro'd%20using%20a%20trivial%20data%20set%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3E--Create%20table%20and%20index%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3ECREATE%20TABLE%20%5BDBO%5D.%5BT1%5D(%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3E%5BCOL1%5D%20%5BINT%5D%20NULL%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3E)%20ON%20%5BPRIMARY%5D%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3EGO%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3ECREATE%20CLUSTERED%20INDEX%20%5BClusteredIndex_T1%5D%20ON%20%5BDBO%5D.%5BT1%5D%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3E(%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3E%5BCOL1%5D%20ASC%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3E)ON%20%5BPRIMARY%5D%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3EGO%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3E--Load%20~10%2C000%20rows%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3EDECLARE%20%40START%20INT%3B%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3ESET%20%40START%20%3D%201%3B%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3EWHILE%20%40START%20%26lt%3B%2010001%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3EBEGIN%3B%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3EINSERT%20INTO%20T1%20VALUES%20(%40START)%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3ESET%20%40START%20%3D%20%40START%20%2B%201%3B%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3EEND%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3E--Create%20a%20View%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3ECREATE%20VIEW%20MyView%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3EAS%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3ESELECT%20*%20FROM%20T1%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3E--Create%20a%20View%20w%2Fnon-deterministic%20function%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3ECREATE%20VIEW%20RN_View%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3EAS%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3ESELECT%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3E%26nbsp%3B%26nbsp%3BROW_NUMBER()%20OVER(ORDER%20BY%20COL1)%20AS%20Row%23%2C%20*%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3EFROM%20T1%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELet's%20enable%20the%20Actual%20Execution%20Plan%20and%20execute%20test%20%231%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3E--Test%20%231%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3ESELECT%20*%20FROM%20%3CSTRONG%3ERN_View%3C%2FSTRONG%3E%20AS%20RN%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3EWHERE%20RN.Row%23%20%3D%2010%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20285px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F110313i09ECDFD8679D8802%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%221.jpg%22%20title%3D%221.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENote%20the%20costly%20Index%20Scan%2C%20which%20eventually%20feeds%20to%20a%20Filter(%3CEM%3EEstimates%20versus%20Actuals%20available%20inline%20via%20SSMS%20v18.0%3C%2FEM%3E)%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20864px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F110314iF0DFDDAAD03B8597%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%222.gif%22%20title%3D%222.gif%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EInstead%20of%20applying%20the%20predicate%20earlier%20in%20the%20execution%2C%20which%20would%20be%20optimal%20as%20it%20would%20reduce%20the%20output%20down%20to%20%221%22%20row%20quickly%2C%20our%20predicate%20is%20instead%20being%20applied%20via%20the%20Filter%20operator%20%22late%22%20in%20the%20execution%22%2C%20after%20a%20costly%20scan%20operator%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20294px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F110315i3FA53D1B604C3F91%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%223.jpg%22%20title%3D%223.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELet's%20try%20another%20test%2C%20in%20which%20we%20move%20the%20ROW_NUMBER%20function%20to%20the%20%22base%22%20query%20itself%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3E--Test%20%232%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3ESELECT%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3E%26nbsp%3B%26nbsp%3BROW_NUMBER()%20OVER(ORDER%20BY%20COL1)%20AS%20Row%23%2C%20*%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3EFROM%20%3CSTRONG%3EMyView%3C%2FSTRONG%3E%20MV%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3EWHERE%20MV.COL1%20%3D%2010%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20393px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F110316i7BF0A5591C69FD29%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%224.jpg%22%20title%3D%224.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20plan%20has%20changed%20to%20a%20%22fast%22%20variation%2C%20in%20which%20the%20predicate%20was%20sent%20to%20the%20appropriate%20index%2C%20resulting%20in%20a%20seek.%20Also%2C%20it%20is%20important%20to%20note%20the%20result%20set%20has%20changed%20when%20compared%20to%20test%20%231.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20797px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F110317i9521CE510BC0DCBC%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%225.gif%22%20title%3D%225.gif%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EManagement%20Studio%E2%80%99s%20Plan%20Comparison%20feature%20gives%20us%20a%20birds%20eye%20view%20of%20plan%20%231%20versus%20plan%20%232%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F110318i283E19DCFBB3B0DC%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%227.gif%22%20title%3D%227.gif%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESo%20what%20happened%20in%20Test%20%231%3F%20Why%20can't%20the%20predicate%20drive%20an%20index%20seek%20like%20in%20Test%20%232%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20key%20factor%20here%20relates%20to%20when%20the%20Query%20Optimizer%20deems%20it%20safe%2C%20or%20not%20safe%2C%20to%20apply%20a%20predicate.%20Specifically%2C%20if%20the%20QO%20determines%20the%20result%20set%20requested%20by%20the%20T-SQL%20would%20change%20if%20the%20predicate%20was%20in%20fact%20applied%20at%20that%20point%20in%20the%20plan...%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20View%20definition%2C%20AKA%20the%20T-SQL%20executed%20by%20the%20View%2C%20is%20basically%20considered%20a%20subquery%20by%20the%20Algebrizer.%20Each%20subquery%20has%20its%20own%20result%20set.%20If%20we%20execute%20the%20SELECT%20directly%20from%20the%20View%20definition%2C%20we%20receive%20a%20result%20set%2010%2C000%20rows%20with%20a%20sequential%20Row%23%2C%20which%20were%20added%20by%20the%20ROW_NUMBER%20function(result%20set%20truncated%20to%20save%20blog%20space).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3ECREATE%20VIEW%20RN_View%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3Eas%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3CFONT%20color%3D%22%230000FF%22%3ESELECT%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3CFONT%20color%3D%22%230000FF%22%3E%26nbsp%3B%26nbsp%3BROW_NUMBER()%20OVER(ORDER%20BY%20COL1)%20AS%20Row%23%2C%20*%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3CFONT%20color%3D%22%230000FF%22%3EFROM%20T1%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20238px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F110319iA39977D35C6B74FB%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%228.jpg%22%20title%3D%228.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20we%20review%20the%20ROW_NUMBER%20definition%2C%20we%20can%20see%20why%20applying%20the%20predicate%20up%20front%20would%20not%20be%20a%20good%20idea%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fnam06.safelinks.protection.outlook.com%2F%3Furl%3Dhttps%253A%252F%252Fdocs.microsoft.com%252Fen-us%252Fsql%252Ft-sql%252Ffunctions%252Frow-number-transact-sql%253Fview%253Dsql-server-2017%26amp%3Bdata%3D01%257C01%257Ctroymoen%2540microsoft.com%257C1c046923d98f4f008e4e08d6c9bbbf56%257C72f988bf86f141af91ab2d7cd011db47%257C1%26amp%3Bsdata%3DgsJJUCHcqOLsZ828axQauq%252B460%252BWqOMuzsBnn9i2RDU%253D%26amp%3Breserved%3D0%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Ft-sql%2Ffunctions%2Frow-number-transact-sql%3Fview%3Dsql-server-2017%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CEM%3ENumbers%20the%20output%20of%20a%20%3CSTRONG%3E%3CU%3Eresult%20set%3C%2FU%3E%3C%2FSTRONG%3E.%20More%20specifically%2C%20%3CSTRONG%3E%3CU%3Ereturns%20the%20sequential%20number%20of%20a%20row%20within%20a%20partition%20of%20a%20result%20set%2C%3C%2FU%3E%3C%2FSTRONG%3E%20starting%20at%201%20for%20the%20first%20row%20in%20each%20partition.%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20%22result%20set%22%20requested%20by%20the%20View%20definition%20is%20not%20only%2010%2C000%20rows%20in%20length%2C%20but%20also%20adds%20the%20sequential%20numbering%20to%20each%20row.%20If%20the%20following%20query%20was%20allowed%20to%20generate%20a%20query%20plan%20in%20which%20the%20filter%20%2210%22%20was%20pushed%20down%20and%20applied%20early%20in%20the%20plan%2C%20the%20result%20set%20would%20be%20limited%20to%20only%20the%20first%2010%20rows.%20Hence%2C%20the%20%3CEM%3Eresult%20set%20would%20be%20different%3A%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3ESELECT%20*%20FROM%20%3CSTRONG%3ERN_View%3C%2FSTRONG%3E%20AS%20RN%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3EWHERE%20RN.Row%23%20%3D%2010%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20189px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F110320iAA8E0FCDDF5191C3%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%229.jpg%22%20title%3D%229.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EConversely%2C%26nbsp%3B%20if%20the%20ROW_NUMBER%20function%20is%20pulled%20out%20of%20the%20View%20definition%20and%20moved%20into%20the%20%22base%22%20query%2C%20the%20predicate%20can%20be%20applied%2Fexecuted%20%E2%80%9Csafely%E2%80%9D%20at%20the%20start%20of%20the%20execution%20plans%20processing.%20The%20filter%20is%20now%20in%20the%20%22same%20query%22%20as%20the%20ROW_NUMBER%20function%2C%20and%20the%20QO%20knows%20the%20user%20is%20asking%20for%20only%20COL1%20values%20equal%20to%2010%2C%20and%20to%20apply%20the%20ROW_NUMBER%20function%20to%20%3CEM%3Eonly%20those%20rows%3C%2FEM%3E.%20Note%20the%20different%20result%20set%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3ESELECT%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3E%26nbsp%3B%26nbsp%3BROW_NUMBER()%20OVER(ORDER%20BY%20COL1)%20AS%20Row%23%2C%20*%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3EFROM%20%3CSTRONG%3EMyView%3C%2FSTRONG%3E%20MV%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3EWHERE%20MV.COL1%20%3D%2010%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20174px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F110321i1D004EC4F0DFE813%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%2210.jpg%22%20title%3D%2210.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20same%20scenario%20can%20be%20reproduced%20using%20a%20CTE%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3EWITH%20MyCTE%20(COL1%2C%20COL2)%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3EAS%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3E(%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3ESELECT%26nbsp%3B%20ROW_NUMBER()%20OVER(ORDER%20BY%20COL1)%20AS%20Row%23%2C%20*%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3EFROM%20MyView%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3E)%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%230000FF%22%3ESELECT%20*%20FROM%20MyCTE%20WHERE%20COL2%20%3D%2010%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20183px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F110322i3CFDC93137A4BDA6%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%2211.jpg%22%20title%3D%2211.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20832px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F110323i0C096C11E860EC5F%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%2212.gif%22%20title%3D%2212.gif%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20moral%20of%20the%20story%20is%20to%20always%20pay%20close%20attention%20to%20what%20your%20subquery's%20are%20asking%20for%2C%20especially%20when%20window%20functions%20such%20as%20ROW_NUMBER%20or%20RANK%20are%20used.%20The%20definition%20of%20said%20subqueries%20can%20change%20filter%20predicate%20location%20in%20query%20plans%20and%20significantly%20affect%20performance.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETroy%20Moen%3C%2FP%3E%0A%3CP%3ESr.%20Escalation%20Engineer%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-483037%22%20slang%3D%22en-US%22%3E%3CP%3EI%20recently%20helped%20troubleshoot%20a%20query%20performance%20issue%20that%20involved%20the%20window%20function%20%22ROW_NUMBER%22.%20The%20running%20theory%20was%20that%20the%20query%E2%80%99s%20filter%20predicate%20should%20target%20the%20index%20resulting%20in%20a%20seek%2C%20but%20was%20instead%20applied%20via%20a%20Filter%20operator%20positioned%20at%20the%20top%20of%20the%20query%20plan.%20This%20led%20to%20a%20costly%20index%20scan%20and%20higher%20query%20duration.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-483037%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPerformance%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EStorage%20Engine%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Etroubleshooting%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ETSQL%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-483125%22%20slang%3D%22en-US%22%3ERe%3A%20The%20case%20of%20the%20filter%20predicate%20with%20window%20functions%20such%20as%20ROW_NUMBER%20and%20RANK%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-483125%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20a%20performance%20issue%20i'd%20like%20to%20report.%20how%20do%20I%20do%20it%3F%20I%20can%20do%20a%20simple%20find%20through%20a%20list%26nbsp%3Band%20it%20takes%20.3%20seconds%20to%20complete%20(that's%20point%203).%20if%20I%20add%20conditional%20formatting-%26gt%3Bduplicate%20values%20to%20the%20column%2C%20the%20find%20time%20increases%20to%20over%2035%20seconds%20to%20complete.%26nbsp%3Bover%20100%20times%20slower.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Microsoft

I recently helped troubleshoot a query performance issue that involved the window function "ROW_NUMBER". The running theory was that the query’s filter predicate should target the index resulting in a seek, but was instead applied via a Filter operator positioned at the top of the query plan. This led to a costly index scan and higher query duration.

  

While the actual use case was a bit more complicated, it can be repro'd using a trivial data set:

 

--Create table and index

CREATE TABLE [DBO].[T1](

[COL1] [INT] NULL

) ON [PRIMARY]

GO

 

CREATE CLUSTERED INDEX [ClusteredIndex_T1] ON [DBO].[T1]

(

[COL1] ASC

)ON [PRIMARY]

GO

 

--Load ~10,000 rows

DECLARE @START INT;

SET @START = 1;

WHILE @START < 10001

BEGIN;

INSERT INTO T1 VALUES (@START)

SET @START = @START + 1;

END

 

--Create a View

CREATE VIEW MyView

AS

SELECT * FROM T1

 

--Create a View w/non-deterministic function

CREATE VIEW RN_View

AS

SELECT

  ROW_NUMBER() OVER(ORDER BY COL1) AS Row#, *

FROM T1

 

Let's enable the Actual Execution Plan and execute test #1

 

--Test #1

SELECT * FROM RN_View AS RN

WHERE RN.Row# = 10

 

1.jpg

 

Note the costly Index Scan, which eventually feeds to a Filter(Estimates versus Actuals available inline via SSMS v18.0):

 

2.gif

 

Instead of applying the predicate earlier in the execution, which would be optimal as it would reduce the output down to "1" row quickly, our predicate is instead being applied via the Filter operator "late" in the execution", after a costly scan operator:

 

3.jpg

 

Let's try another test, in which we move the ROW_NUMBER function to the "base" query itself:

 

--Test #2

SELECT

  ROW_NUMBER() OVER(ORDER BY COL1) AS Row#, *

FROM MyView MV

WHERE MV.COL1 = 10

 

4.jpg

 

The plan has changed to a "fast" variation, in which the predicate was sent to the appropriate index, resulting in a seek. Also, it is important to note the result set has changed when compared to test #1.

 

5.gif

 

Management Studio’s Plan Comparison feature gives us a birds eye view of plan #1 versus plan #2:

 

7.gif

 

So what happened in Test #1? Why can't the predicate drive an index seek like in Test #2?

 

The key factor here relates to when the Query Optimizer deems it safe, or not safe, to apply a predicate. Specifically, if the QO determines the result set requested by the T-SQL would change if the predicate was in fact applied at that point in the plan...

 

The View definition, AKA the T-SQL executed by the View, is basically considered a subquery by the Algebrizer. Each subquery has its own result set. If we execute the SELECT directly from the View definition, we receive a result set 10,000 rows with a sequential Row#, which were added by the ROW_NUMBER function(result set truncated to save blog space).

 

CREATE VIEW RN_View

as

SELECT

  ROW_NUMBER() OVER(ORDER BY COL1) AS Row#, *

FROM T1

 

8.jpg

 

If we review the ROW_NUMBER definition, we can see why applying the predicate up front would not be a good idea:

 

https://docs.microsoft.com/en-us/sql/t-sql/functions/row-number-transact-sql?view=sql-server-2017

 

Numbers the output of a result set. More specifically, returns the sequential number of a row within a partition of a result set, starting at 1 for the first row in each partition.

 

The "result set" requested by the View definition is not only 10,000 rows in length, but also adds the sequential numbering to each row. If the following query was allowed to generate a query plan in which the filter "10" was pushed down and applied early in the plan, the result set would be limited to only the first 10 rows. Hence, the result set would be different:

 

SELECT * FROM RN_View AS RN

WHERE RN.Row# = 10

 

9.jpg

 

Conversely,  if the ROW_NUMBER function is pulled out of the View definition and moved into the "base" query, the predicate can be applied/executed “safely” at the start of the execution plans processing. The filter is now in the "same query" as the ROW_NUMBER function, and the QO knows the user is asking for only COL1 values equal to 10, and to apply the ROW_NUMBER function to only those rows. Note the different result set:

 

SELECT

  ROW_NUMBER() OVER(ORDER BY COL1) AS Row#, *

FROM MyView MV

WHERE MV.COL1 = 10

 

10.jpg

 

This same scenario can be reproduced using a CTE:

 

WITH MyCTE (COL1, COL2)

AS

(

SELECT  ROW_NUMBER() OVER(ORDER BY COL1) AS Row#, *

FROM MyView

)

SELECT * FROM MyCTE WHERE COL2 = 10

 

11.jpg

12.gif

 

The moral of the story is to always pay close attention to what your subquery's are asking for, especially when window functions such as ROW_NUMBER or RANK are used. The definition of said subqueries can change filter predicate location in query plans and significantly affect performance.

 

Troy Moen

Sr. Escalation Engineer

 

1 Comment
Regular Visitor

I have a performance issue i'd like to report. how do I do it? I can do a simple find through a list and it takes .3 seconds to complete (that's point 3). if I add conditional formatting->duplicate values to the column, the find time increases to over 35 seconds to complete. over 100 times slower.