%3CLINGO-SUB%20id%3D%22lingo-sub-1740735%22%20slang%3D%22en-US%22%3EPaginating%20results%20in%20Synapse%20SQL%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1740735%22%20slang%3D%22en-US%22%3E%3CP%3EPagination%20is%20important%20technique%20in%20SQL%20that%20enables%20you%20to%20return%20partial%20results%20of%20the%20query%20by%20specifying%20a%20range%20of%20rows%20that%20should%20be%20returned.%20It%20is%20commonly%20used%20when%20you%20need%20to%20get%20the%20partial%20results%20of%20the%20queries%20that%20return%20a%20large%20number%20of%20rows%20in%20the%20result%20in%20the%20pages%20with%20size%2C%2010%2C%2025%2C%2050%2C%20or%20100.%3C%2FP%3E%0A%3CP%3EIn%20this%20article%20you%20will%20see%20how%20to%20do%20pagination%20on%20large%20amount%20of%20data%20in%20Synapse%20SQL.%3C%2FP%3E%0A%3CP%3EFirst%20let%E2%80%99s%20see%20some%20pagination%20methods%20in%20SQL%20language.%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1240990569%22%20id%3D%22toc-hId--1240990511%22%3EOFFSET%2FFETCH%3C%2FH2%3E%0A%3CP%3ET-SQL%20language%20has%20OFFSET%2FFETCH%20clause%20that%20enables%20you%20to%20skip%20some%20number%20of%20rows%20and%20take%20few%20of%20remaining%20(similar%20to%20TOP%20clause).%20An%20example%20of%20query%20with%20OFFSET%2FFETCH%20clause%20is%20shown%20in%20the%20following%20code%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ESELECT%20*%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20FROM%20dbo.Supplier%20AS%20data%0AORDER%20BY%20data.S_SUPPKEY%0AOFFSET%20(%40offset)%20ROWS%0AFETCH%20NEXT%20(%40fetch)%20ROWS%20ONLY%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EUnfortunately%2C%20this%20method%20is%20still%20not%20available%20in%20Synapse%20SQL.%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1246522264%22%20id%3D%22toc-hId-1246522322%22%3ETOP%20offset%2Bfetch%3C%2FH2%3E%0A%3CP%3ETo%20get%20the%20rows%20between%20%40offset%20and%20%40offset%2B%40fetch%2C%20we%20can%20first%20select%20TOP%20%40offset%2B%40fetch%20records%20from%20query%20(for%20example%20TOP%20110%20records%20if%20the%20offset%20is%20100%20and%20fetch%20is%2010)%2C%20then%20get%20bottom%20%40fetch%20records%20from%20that%20row%20set%20(for%20example%20rows%20between%20100%20and%20110).%3C%2FP%3E%0A%3CP%3EThe%20query%20that%20can%20return%20rows%20between%20%40offset%20and%20%40offset%2B%40fetch%20is%20shown%20in%20the%20following%20example%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ESELECT%20*%0AFROM%20(%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20SELECT%20TOP%20(%40fetch)%20*%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20FROM%0A%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(SELECT%20TOP(%40offset%2B%40fetch)%20*%0A%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%26nbsp%3B%26nbsp%3B%26nbsp%3B%20FROM%20dbo.Supplier%0A%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%26nbsp%3B%26nbsp%3B%26nbsp%3B%20ORDER%20BY%20S_SUPPKEY%20ASC%0A%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)%20AS%20topFO%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20ORDER%20BY%20S_SUPPKEY%20DESC%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B)%20AS%20bottomF%0AORDER%20BY%20S_SUPPKEY%20ASC%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3ESubquery%20%3CSTRONG%3EtopFO%3C%2FSTRONG%3E%20is%20a%20set%20of%20rows%20that%20contain%20top%20%40offset%2B%40fetch%20rows%20according%20to%20the%20desired%20sort%20criterion.%20Now%20we%20need%20to%20get%20%40fetch%20bottom%20records%20by%20reverting%20the%20order%20of%20this%20rowset%20and%20getting%20TOP%20%40fetch%20rows%20as%20bottomF%20subquery.%20Note%20that%20we%20don%E2%80%99t%20have%20BOTTOM%20operator%20in%20TSQL%20so%20we%20need%20to%20temporarily%20revert%20the%20sort%20order%20and%20get%20TOP%20rows%20in%20opposite%20direction.%20These%20are%20the%20rows%20that%20we%20need%20but%20now%20in%20the%20wrong%20sort%20order%2C%20so%20we%20need%20to%20change%20sort%20order%20again%20in%20the%20surrounding%20query.%3C%2FP%3E%0A%3CP%3EIt%20might%20be%20strange%20that%20you%20need%20to%20write%20a%20query%20that%20sorts%20results%20three%20times%20(I%E2%80%99m%20calling%20this%20Salto%20mortale%20technique)%2C%20but%20we%20will%20not%20have%20big%20performance%20impact%20because%20second%20and%20third%20sort%20worn%20on%20the%20smaller%20amount%20of%20already%20sorted%20rows.%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--560932199%22%20id%3D%22toc-hId--560932141%22%3EROW_NUMBER%20window%3C%2FH2%3E%0A%3CP%3EROW_NUMBER%20window%20function%20can%20return%20order%20number%20of%20the%20row%20in%20the%20rowset%20by%20specified%20sort%20criterion.%20We%20can%20use%20this%20info%20to%20return%20only%20the%20rows%20that%20have%20ROW_NUMBER%20in%20the%20range%20between%20%40offset%20and%20%40offset%2B%40fetch%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ESELECT%20TOP%20(%40fetch)%20*%0AFROM%0A(%20SELECT%20ROW_NUMBER()%20OVER(ORDER%20BY%20S_SUPPKEY)%20AS%20RowNum%2C%20*%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20FROM%20dbo.Supplier%20AS%20tr)%20AS%20data%0AWHERE%20%40offset%20%26lt%3B%20RowNum%0AORDER%20BY%20data.S_SUPPKEY%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EInner%20sub-query%20will%20return%20rows%20with%20positions%20and%20outer%20query%20will%20filter%20them%20out.%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1926580634%22%20id%3D%22toc-hId-1926580692%22%3EPerformance%20test%3C%2FH2%3E%0A%3CP%3EThe%20important%20question%20what%20method%20we%20should%20choose%20to%20implement%20queries%20with%20pagination.%20I%20have%20created%20procedures%20that%20select%20and%20paginate%20rows%20from%20tables%20by%20specified%20criterion%20and%20return%2010-row%20pages%20with%20offsets%2030%2C%20300%2C%20and%202000.%20I%20have%20executed%20these%20tests%20on%20dbo.Customer%20table%20from%20TPCH%201TB%20database%20with%20150.000.000%20rows.%20Using%20the%20following%20code%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3Edeclare%20%40offset%20int%20%3D%2010%3B%0AEXEC%20page_row_number%20'dbo.Customer'%2C%20'C_CUSTKEY'%2C%20%40offset%2C%2010%0AEXEC%20page_offset_fetch%20'dbo.Customer'%2C%20'C_CUSTKEY'%2C%20%40offset%2C%2010%0AEXEC%20page_salto_mortale%20'dbo.Customer'%2C%20'C_CUSTKEY'%2C%20%40offset%2C%2010%0A%0Aset%20%40offset%20%3D%20300%3B%0AEXEC%20page_salto_mortale%20'dbo.Customer'%2C%20'C_CUSTKEY'%2C%20%40offset%2C%2010%0AEXEC%20page_row_number%20'dbo.Customer'%2C%20'C_CUSTKEY'%2C%20%40offset%2C%2010%0AEXEC%20page_offset_fetch%20'dbo.Customer'%2C%20'C_CUSTKEY'%2C%20%40offset%2C%2010%0A%0Aset%20%40offset%20%3D%205000%3B%0AEXEC%20page_offset_fetch%20'dbo.Customer'%2C%20'C_CUSTKEY'%2C%20%40offset%2C%2010%0AEXEC%20page_salto_mortale%20'dbo.Customer'%2C%20'C_CUSTKEY'%2C%20%40offset%2C%2010%0AEXEC%20page_row_number%20'dbo.Customer'%2C%20'C_CUSTKEY'%2C%20%40offset%2C%2010%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3ESince%20OFFSET%2FFETCH%20is%20still%20not%20available%20in%20Synapse%20SQL%2C%20I%20have%20run%20the%20test%20on%20Azure%20SQL%20Managed%20Instance%20and%20the%20results%20are%20displayed%20in%20the%20following%20Query%20Store%20UI%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22JovanPop_0-1601670941315.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F223782iFF6BAA4510C9264E%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22JovanPop_0-1601670941315.png%22%20alt%3D%22JovanPop_0-1601670941315.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFirst%20three%20results%20are%20ROW_NUMBER%20methods%20with%20offsets%2010%2C%20300%2C%20and%205000.%20It%20seems%20that%20this%20method%20provides%20worst%20results.%20Salto%20mortale%20(or%20TOP%20offset%2Bfetch%20with%20switching%20order)%20seems%20providing%20even%20slightly%20better%20results%20than%20native%20OFFSET%2FFETCH%20but%20with%20minor%20perf%20difference.%3C%2FP%3E%0A%3CP%3E%26nbsp%3BI%20also%20double-checked%20the%20results%20using%20%3CA%20title%3D%22Query%20Performance%20Insights%22%20href%3D%22https%3A%2F%2Fgithub.com%2FJocaPC%2Fqpi%23installation%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EQuery%20Performance%20Insights%3C%2FA%3E%20library%20and%20found%20average%20duration%2C%20execution%20count%20and%20spent%20CPU%20time%20for%20these%20queries%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22JovanPop_1-1601670941340.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F223781iEFFC3B36716D8545%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22JovanPop_1-1601670941340.png%22%20alt%3D%22JovanPop_1-1601670941340.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20conclusions%20are%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EThe%20best%20performance%20we%20are%20getting%20with%20TOP%20offset%2Bfetch%20query%20(a.k.a.%20Salto%20mortale%20query)%2C%20but%20similar%20perf%20we%20can%20expect%20with%20OFFSET%2FFETCH.%3C%2FLI%3E%0A%3CLI%3EPerformance%20don%E2%80%99t%20depend%20on%20offset.%20Regardless%20of%20offset%20most%20of%20the%20time%20is%20probably%20spent%20on%20sorting%20entire%20data%20set%20to%20find%20what%20is%20the%20TOP%20offset%2Bfetch%20rows%20in%20the%20desired%20sort%20order%20or%20position%20using%20ROW_NUMBER%20by%20desired%20sort%20order.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EIf%20you%20want%20to%20try%20this%20experiment%2C%20the%20code%20of%20the%20test%20procedures%20is%20below%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ECREATE%20OR%20ALTER%20PROCEDURE%20page_row_number%20(%40table%20varchar(200)%2C%20%40column%20sysname%2C%20%40offset%20varchar(20)%2C%20%40fetch%20varchar(20))%0AAS%20BEGIN%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20DECLARE%20%40sql%20NVARCHAR(4000)%20%3D%20N'%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20SELECT%20TOP%20('%20%2B%20%40fetch%20%2B%20')%20*%20%2F*%20Row%20number%20'%20%2B%20%40table%20%2B%20'%20offset%3A%20'%2B%40offset%2B'%20*%2F%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20FROM%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20(%20SELECT%20ROW_NUMBER()%20OVER(ORDER%20BY%20'%20%2B%20%40column%20%2B%20')%20AS%20RowNum%2C%20*%0A%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%20FROM%20'%20%2B%20%40table%20%2B%20'%20AS%20tr)%20AS%20data%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20WHERE%20'%20%2B%20%40offset%20%2B%20'%20%26lt%3B%20RowNum%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20ORDER%20BY%20data.'%20%2B%20%40column%20%2B%20''%3B%0A%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20EXEC%20sp_executesql%20%40tsql%20%3D%20%40sql%3B%0AEND%0AGO%0ACREATE%20OR%20ALTER%20PROCEDURE%20page_offset_fetch%20(%40table%20varchar(200)%2C%20%40column%20sysname%2C%20%40offset%20varchar(20)%2C%20%40fetch%20varchar(20))%0AAS%20BEGIN%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20DECLARE%20%40sql%20NVARCHAR(4000)%20%3D%20N'%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20SELECT%20*%20%2F*%20Offset%20fetch%20'%20%2B%20%40table%20%2B%20'%20offset%3A%20'%2B%40offset%20%2B%20'%20*%2F%0A%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%20FROM%20'%20%2B%20%40table%20%2B%20'%20AS%20data%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20ORDER%20BY%20data.'%20%2B%20%40column%20%2B%20'%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20OFFSET%20('%2B%40offset%2B')%20ROWS%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20FETCH%20NEXT%20('%2B%40fetch%2B')%20ROWS%20ONLY%3B'%3B%0A%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20EXEC%20sp_executesql%20%40tsql%20%3D%20%40sql%3B%0AEND%0AGO%0ACREATE%20OR%20ALTER%20PROCEDURE%20page_salto_mortale%20(%40table%20varchar(200)%2C%20%40column%20sysname%2C%20%40offset%20int%2C%20%40fetch%20int)%0AAS%20BEGIN%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20DECLARE%20%40sql%20NVARCHAR(4000)%20%3D%20N'%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20SELECT%20*%20%2F*%20Salto%20mortale%20'%20%2B%20%40table%20%2B%20'%20offset%3A%20'%2B%20CAST(%40offset%20AS%20VARCHAR(20))%20%2B%20'*%2F%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20FROM%20(%0A%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%20SELECT%20TOP%20('%2BCAST(%40fetch%20AS%20VARCHAR(20))%2B')%20*%0A%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%20FROM%0A%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%26nbsp%3B%26nbsp%3B%26nbsp%3B%20(SELECT%20TOP('%2BCAST(%20(%40offset%2B%40fetch)%20AS%20VARCHAR(20))%2B')%20*%0A%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%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20FROM%20'%20%2B%20%40table%20%2B%20'%0A%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%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20ORDER%20BY%20'%20%2B%20%40column%20%2B%20'%20ASC)%20AS%20topFO%0A%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%20ORDER%20BY%20'%20%2B%20%40column%20%2B%20'%20DESC%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20)%20AS%20topF%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20ORDER%20BY%20'%20%2B%20%40column%20%2B%20'%20ASC'%3B%0A%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20EXEC%20sp_executesql%20%40tsql%20%3D%20%40sql%3B%0AEND%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1740735%22%20slang%3D%22en-US%22%3E%3CP%3EIn%20this%20article%20you%20will%20see%20comparison%20of%20few%20result%20set%20pagination%20techniques%20that%20you%20can%20use%20in%20Synapse%20SQL%20and%20see%20what%20could%20be%20the%20best%20option%20to%20implement%20pagination%20of%20your%20results.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1740735%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESynapse%20SQL%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

Pagination is important technique in SQL that enables you to return partial results of the query by specifying a range of rows that should be returned. It is commonly used when you need to get the partial results of the queries that return a large number of rows in the result in the pages with size, 10, 25, 50, or 100.

In this article you will see how to do pagination on large amount of data in Synapse SQL.

First let’s see some pagination methods in SQL language.

OFFSET/FETCH

T-SQL language has OFFSET/FETCH clause that enables you to skip some number of rows and take few of remaining (similar to TOP clause). An example of query with OFFSET/FETCH clause is shown in the following code:

SELECT *
       FROM dbo.Supplier AS data
ORDER BY data.S_SUPPKEY
OFFSET (@offset) ROWS
FETCH NEXT (@fetch) ROWS ONLY;

Unfortunately, this method is still not available in Synapse SQL.

TOP offset+fetch

To get the rows between @offset and @offset+@fetch, we can first select TOP @offset+@fetch records from query (for example TOP 110 records if the offset is 100 and fetch is 10), then get bottom @fetch records from that row set (for example rows between 100 and 110).

The query that can return rows between @offset and @offset+@fetch is shown in the following example:

SELECT *
FROM (
       SELECT TOP (@fetch) *
       FROM
              (SELECT TOP(@offset+@fetch) *
                      FROM dbo.Supplier
                      ORDER BY S_SUPPKEY ASC
              ) AS topFO
       ORDER BY S_SUPPKEY DESC
     ) AS bottomF
ORDER BY S_SUPPKEY ASC

Subquery topFO is a set of rows that contain top @offset+@fetch rows according to the desired sort criterion. Now we need to get @fetch bottom records by reverting the order of this rowset and getting TOP @fetch rows as bottomF subquery. Note that we don’t have BOTTOM operator in TSQL so we need to temporarily revert the sort order and get TOP rows in opposite direction. These are the rows that we need but now in the wrong sort order, so we need to change sort order again in the surrounding query.

It might be strange that you need to write a query that sorts results three times (I’m calling this Salto mortale technique), but we will not have big performance impact because second and third sort worn on the smaller amount of already sorted rows.

ROW_NUMBER window

ROW_NUMBER window function can return order number of the row in the rowset by specified sort criterion. We can use this info to return only the rows that have ROW_NUMBER in the range between @offset and @offset+@fetch:

SELECT TOP (@fetch) *
FROM
( SELECT ROW_NUMBER() OVER(ORDER BY S_SUPPKEY) AS RowNum, *
       FROM dbo.Supplier AS tr) AS data
WHERE @offset < RowNum
ORDER BY data.S_SUPPKEY

Inner sub-query will return rows with positions and outer query will filter them out.

Performance test

The important question what method we should choose to implement queries with pagination. I have created procedures that select and paginate rows from tables by specified criterion and return 10-row pages with offsets 30, 300, and 2000. I have executed these tests on dbo.Customer table from TPCH 1TB database with 150.000.000 rows. Using the following code:

declare @offset int = 10;
EXEC page_row_number 'dbo.Customer', 'C_CUSTKEY', @offset, 10
EXEC page_offset_fetch 'dbo.Customer', 'C_CUSTKEY', @offset, 10
EXEC page_salto_mortale 'dbo.Customer', 'C_CUSTKEY', @offset, 10

set @offset = 300;
EXEC page_salto_mortale 'dbo.Customer', 'C_CUSTKEY', @offset, 10
EXEC page_row_number 'dbo.Customer', 'C_CUSTKEY', @offset, 10
EXEC page_offset_fetch 'dbo.Customer', 'C_CUSTKEY', @offset, 10

set @offset = 5000;
EXEC page_offset_fetch 'dbo.Customer', 'C_CUSTKEY', @offset, 10
EXEC page_salto_mortale 'dbo.Customer', 'C_CUSTKEY', @offset, 10
EXEC page_row_number 'dbo.Customer', 'C_CUSTKEY', @offset, 10

Since OFFSET/FETCH is still not available in Synapse SQL, I have run the test on Azure SQL Managed Instance and the results are displayed in the following Query Store UI:

JovanPop_0-1601670941315.png

 

 

First three results are ROW_NUMBER methods with offsets 10, 300, and 5000. It seems that this method provides worst results. Salto mortale (or TOP offset+fetch with switching order) seems providing even slightly better results than native OFFSET/FETCH but with minor perf difference.

 I also double-checked the results using Query Performance Insights library and found average duration, execution count and spent CPU time for these queries:

JovanPop_1-1601670941340.png

 

The conclusions are:

  • The best performance we are getting with TOP offset+fetch query (a.k.a. Salto mortale query), but similar perf we can expect with OFFSET/FETCH.
  • Performance don’t depend on offset. Regardless of offset most of the time is probably spent on sorting entire data set to find what is the TOP offset+fetch rows in the desired sort order or position using ROW_NUMBER by desired sort order.

If you want to try this experiment, the code of the test procedures is below:

 

CREATE OR ALTER PROCEDURE page_row_number (@table varchar(200), @column sysname, @offset varchar(20), @fetch varchar(20))
AS BEGIN
       DECLARE @sql NVARCHAR(4000) = N'
       SELECT TOP (' + @fetch + ') * /* Row number ' + @table + ' offset: '+@offset+' */
       FROM
       ( SELECT ROW_NUMBER() OVER(ORDER BY ' + @column + ') AS RowNum, *
              FROM ' + @table + ' AS tr) AS data
       WHERE ' + @offset + ' < RowNum
       ORDER BY data.' + @column + '';

       EXEC sp_executesql @tsql = @sql;
END
GO
CREATE OR ALTER PROCEDURE page_offset_fetch (@table varchar(200), @column sysname, @offset varchar(20), @fetch varchar(20))
AS BEGIN
       DECLARE @sql NVARCHAR(4000) = N'
       SELECT * /* Offset fetch ' + @table + ' offset: '+@offset + ' */
              FROM ' + @table + ' AS data
       ORDER BY data.' + @column + '
       OFFSET ('+@offset+') ROWS
       FETCH NEXT ('+@fetch+') ROWS ONLY;';

       EXEC sp_executesql @tsql = @sql;
END
GO
CREATE OR ALTER PROCEDURE page_salto_mortale (@table varchar(200), @column sysname, @offset int, @fetch int)
AS BEGIN
       DECLARE @sql NVARCHAR(4000) = N'
       SELECT * /* Salto mortale ' + @table + ' offset: '+ CAST(@offset AS VARCHAR(20)) + '*/
       FROM (
              SELECT TOP ('+CAST(@fetch AS VARCHAR(20))+') *
              FROM
                      (SELECT TOP('+CAST( (@offset+@fetch) AS VARCHAR(20))+') *
                             FROM ' + @table + '
                             ORDER BY ' + @column + ' ASC) AS topFO
              ORDER BY ' + @column + ' DESC
       ) AS topF
       ORDER BY ' + @column + ' ASC';

       EXEC sp_executesql @tsql = @sql;
END