<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results matching tags 'SQL Server 11', 'offset', and 'SQL 11'</title><link>http://sqlblog.com/search/SearchResults.aspx?o=DateDescending&amp;tag=SQL+Server+11,offset,SQL+11&amp;orTags=0</link><description>Search results matching tags 'SQL Server 11', 'offset', and 'SQL 11'</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>SQL Server v.Next (Denali) : Using the OFFSET clause (paging)</title><link>http://sqlblog.com/blogs/aaron_bertrand/archive/2010/11/10/sql-server-11-denali-using-the-offset-clause.aspx</link><pubDate>Thu, 11 Nov 2010 00:33:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:30120</guid><dc:creator>AaronBertrand</dc:creator><description>&lt;p&gt;A very common request over the past, oh, 10 years, maybe more, has been better support for paging within T-SQL (&lt;a href="http://connect.microsoft.com/SQLServer/feedback/details/124495/implement-limit-keyword" title="http://connect.microsoft.com/SQLServer/feedback/details/124495/implement-limit-keyword" target="_blank"&gt;this Connect request dates back to 2005&lt;/a&gt;).&amp;nbsp; Well, you will see some broader support for this within a year, give or take: the next version of SQL Server will offer OFFSET / FETCH syntax.&amp;nbsp; Tobias Ternstrom spilled the beans during yesterday's keynote at SQL Connections, so there's not much to stop anyone from starting to publish code samples (and I posted a very brief and simple example in &lt;a href="http://sqlblog.com/blogs/aaron_bertrand/archive/2010/11/02/blogging-from-the-sql-connections-keynote.aspx" title="http://sqlblog.com/blogs/aaron_bertrand/archive/2010/11/02/blogging-from-the-sql-connections-keynote.aspx" target="_blank"&gt;my post from the SQL Connections keynote&lt;/a&gt;).&amp;nbsp; I thought I would just show a quick example using AdventureWorks2008R2, demonstrating both the shorter syntax as well as a slightly more efficient plan.&lt;br&gt;&lt;/p&gt;

&lt;p&gt;Here is the way we might handle paging today... several variations exist, but hopefully we can agree that typically the approach is similarly ugly and verbose:&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;DECLARE&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@PageSize&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="blue"&gt;TINYINT&lt;/font&gt;&lt;font color="black"&gt; &lt;/font&gt;&lt;font color="blue"&gt;= &lt;/font&gt;&lt;font color="black"&gt;20&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@CurrentPage &lt;/font&gt;&lt;font color="blue"&gt;INT&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; = &lt;/font&gt;&lt;font color="black"&gt;1500&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;WITH &lt;/font&gt;&lt;font color="black"&gt;o &lt;/font&gt;&lt;font color="blue"&gt;AS&lt;br&gt;&lt;/font&gt;&lt;font color="gray"&gt;(&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;SELECT TOP &lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="#434343"&gt;@CurrentPage &lt;/font&gt;&lt;font color="gray"&gt;* &lt;/font&gt;&lt;font color="#434343"&gt;@PageSize&lt;/font&gt;&lt;font color="gray"&gt;)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;[RowNumber] &lt;/font&gt;&lt;font color="blue"&gt;= &lt;/font&gt;&lt;font color="black"&gt;ROW_NUMBER&lt;/font&gt;&lt;font color="gray"&gt;() &lt;/font&gt;&lt;font color="blue"&gt;OVER &lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="blue"&gt;ORDER BY &lt;/font&gt;&lt;font color="black"&gt;SalesOrderID&lt;/font&gt;&lt;font color="gray"&gt;),&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;SalesOrderID &lt;/font&gt;&lt;font color="green"&gt;/* , ... */&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;FROM&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;Sales.SalesOrderHeader&lt;br&gt;&lt;/font&gt;&lt;font color="gray"&gt;)&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="black"&gt;SalesOrderID &lt;/font&gt;&lt;font color="green"&gt;/* , ... */&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;FROM &lt;/font&gt;&lt;font color="black"&gt;o&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;WHERE&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;[RowNumber] &lt;/font&gt;&lt;font color="gray"&gt;BETWEEN ((&lt;/font&gt;&lt;font color="#434343"&gt;@CurrentPage &lt;/font&gt;&lt;font color="gray"&gt;- &lt;/font&gt;&lt;font color="black"&gt;1&lt;/font&gt;&lt;font color="gray"&gt;) * &lt;/font&gt;&lt;font color="#434343"&gt;@PageSize &lt;/font&gt;&lt;font color="gray"&gt;+ &lt;/font&gt;&lt;font color="black"&gt;1&lt;/font&gt;&lt;font color="gray"&gt;)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;AND (((&lt;/font&gt;&lt;font color="#434343"&gt;@CurrentPage &lt;/font&gt;&lt;font color="gray"&gt;- &lt;/font&gt;&lt;font color="black"&gt;1&lt;/font&gt;&lt;font color="gray"&gt;) * &lt;/font&gt;&lt;font color="#434343"&gt;@PageSize&lt;/font&gt;&lt;font color="gray"&gt;) + &lt;/font&gt;&lt;font color="#434343"&gt;@PageSize&lt;/font&gt;&lt;font color="gray"&gt;)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;ORDER BY&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;[RowNumber]&lt;/font&gt;&lt;font color="gray"&gt;; &lt;br&gt;&lt;/font&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;


&lt;p&gt;Now with the new syntax, coming soon to a virtual machine near you, a lot of this messiness and redundancy goes away: &lt;br&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;DECLARE&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@PageSize&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="blue"&gt;TINYINT = &lt;/font&gt;&lt;font color="black"&gt;20&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@CurrentPage &lt;/font&gt;&lt;font color="blue"&gt;INT&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; = &lt;/font&gt;&lt;font color="black"&gt;1500&lt;/font&gt;&lt;font color="gray"&gt;;&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="black"&gt;SalesOrderID &lt;/font&gt;&lt;font color="green"&gt;/* , ... */&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;FROM &lt;/font&gt;&lt;font color="black"&gt;Sales.SalesOrderHeader&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;ORDER BY &lt;/font&gt;&lt;font color="black"&gt;SalesOrderID&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;OFFSET&lt;/font&gt;&lt;/font&gt;&lt;font color="black"&gt; &lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="#434343"&gt;@PageSize &lt;/font&gt;&lt;font color="gray"&gt;* (&lt;/font&gt;&lt;font color="#434343"&gt;@CurrentPage &lt;/font&gt;&lt;font color="gray"&gt;- &lt;/font&gt;&lt;font color="black"&gt;1&lt;/font&gt;&lt;font color="gray"&gt;)) &lt;/font&gt;&lt;font color="black"&gt;&lt;font color="blue"&gt;ROWS&lt;br&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="blue"&gt;FETCH NEXT&lt;/font&gt;&lt;font color="black"&gt; &lt;/font&gt;&lt;font color="#434343"&gt;@PageSize &lt;/font&gt;&lt;font color="blue"&gt;ROWS ONLY&lt;/font&gt;&lt;font color="gray"&gt;;&lt;/font&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;Isn't that nicer?&amp;nbsp; I thought so. &lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;font size="4"&gt;&lt;span style="font-weight:bold;"&gt;But wait, what about the plans? &lt;/span&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;In Management Studio, running the actual execution plan yields very little difference.&amp;nbsp; There is an additional filter (the WHERE clause) and a SEQUENCE project (the ROW_NUMBER()), but otherwise these plans seem pretty equivalent (click to embiggen):&lt;/p&gt;

&lt;p style="margin-left:40px;"&gt;&amp;nbsp;&lt;a href="http://sqlblog.com/files/folders/30128/download.aspx" title="http://sqlblog.com/files/folders/30128/download.aspx" target="_blank"&gt;&lt;img src="http://sqlblog.com/files/folders/30128/download.aspx" height="143" width="457" border="1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In order to visualize some other facts about these plans, I save the plan XML to disk, and then load it up in Plan Explorer.&amp;nbsp; The first thing I notice right off the bat is that tomayto &amp;lt;&amp;gt; tomatto: in SSMS, the cost % of each plan as a percentage of the batch was rounded off to 50%, but the old way of doing things is (not surprisingly) a wee bit less efficient:&lt;/p&gt;

&lt;p style="margin-left:40px;"&gt;&lt;img src="http://sqlblog.com/files/folders/30129/download.aspx"&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;Is it a huge difference?&amp;nbsp; Absolutely not.&amp;nbsp; And when we look at the graphical plans in the default mode in Plan Explorer, we see pretty much the same thing we see in Management Studio.&lt;/p&gt;&lt;p&gt;(Please note that these screen shots are doctored in order to save 
space.&amp;nbsp; No, I didn't change any data, but I did pretend that Plan 
Explorer can visualize two execution plans at once.) &lt;br&gt;&lt;/p&gt;



&lt;p style="margin-left:40px;"&gt;&lt;img src="http://sqlblog.com/files/folders/30130/download.aspx" height="302" width="774"&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;However, when we right-click the plan and change Data Widths By to Data Size (MB), something else becomes a little more apparent... nearly 10 MB of data gets passed between multiple operators using the old plan, whereas this transfer only seems to occur once in the new plan:&lt;br&gt;&lt;/p&gt;

&lt;p style="margin-left:40px;"&gt;&lt;img src="http://sqlblog.com/files/folders/30131/download.aspx" height="302" width="774"&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;(And yes, looking back, this should have stuck out to me using the default Rows option as well, but for some reason 9 MB hit home a little harder.&amp;nbsp; In my defense, the lines do get a little fatter when you show data vs. row counts.)&lt;br&gt;&lt;/p&gt;

&lt;p&gt;The fact that the "size" of the clustered index scan is passed between many more operators in the old version of the query makes me a little skeptical that it is doing more work than it needs to be, and this could be contributing to the overall cost difference (though I will confess that the I/O costs were actually slightly higher for the new version of the plan).&lt;br&gt;&lt;/p&gt;

&lt;p&gt;But by and large, it seems like the work done by the engine under the covers is relatively the same, with only a few minor differences.&amp;nbsp; These differences are unlikely to cause any noticeable change in your existing applications, but keep in mind that this was an overly simplistic test, and that you should always try out alternatives on your own systems, with your own hardware, your own data, and your own load and concurrency.&amp;nbsp; I plan to do some more testing of this feature against some horrific paging scenarios I've had to support in the past.&lt;br&gt;&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;font size="4"&gt;&lt;span style="font-weight:bold;"&gt;Caveats&lt;/span&gt;&lt;/font&gt; &lt;br&gt;&lt;/p&gt;

&lt;p&gt;There are a couple of notes on the syntax that may be of interest:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In order to support parameterized ordering, you will still have to use dynamic SQL or complex CASE expressions in the ORDER BY clause.&amp;nbsp; Tobias said that it may be extended in future versions, but the bare minimum is all that made the cut for Denali.&amp;nbsp; I'll try to adapt some old examples to the new syntax - while it will still represent less optimal methods, the code should still end up looking less sloppy.&lt;br&gt;&amp;nbsp;&lt;br&gt;&lt;/li&gt;

&lt;li&gt;In order to use OFFSET, you *must* include an ORDER BY clause.&amp;nbsp; If you try something like this (which essentially says we don't care about the order)...&amp;nbsp;
&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;SELECT &lt;/font&gt;&lt;font color="gray"&gt;*&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;FROM &lt;/font&gt;&lt;font color="green"&gt;sys&lt;font color="black"&gt;.&lt;/font&gt;objects&lt;/font&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font&gt;&lt;font color="blue"&gt;OFFSET&lt;/font&gt;&lt;/font&gt;&lt;font color="black"&gt; 0 &lt;/font&gt;&lt;font&gt;&lt;font color="blue"&gt;ROWS&lt;br&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="black"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="blue"&gt;FETCH &lt;/font&gt;&lt;font color="black"&gt;NEXT 10 &lt;/font&gt;&lt;font color="blue"&gt;ROWS ONLY&lt;/font&gt;&lt;font color="gray"&gt;;&lt;/font&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;

...you will get the following error messages:

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="#cc0000"&gt;Msg 102, Level 15, State 1, Line 3&lt;br&gt;Incorrect syntax near '0'.&lt;br&gt;Msg 153, Level 15, State 2, Line 4&lt;br&gt;Invalid usage of the option NEXT in the FETCH statement.&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;&lt;p&gt;In my opinion, this is the way it should be, and it has always bothered me that you could use TOP in various "dirty" ways - without an ORDER BY clause, or simply to fool SQL Server into allowing an inconsequential ORDER BY clause into a view.&lt;br&gt;&lt;/p&gt;&lt;p&gt;In fact, OFFSET and FETCH NEXT FOR are &lt;a href="http://msdn.microsoft.com/en-us/library/ms188385%28v=SQL.110%29.aspx" title="http://msdn.microsoft.com/en-us/library/ms188385(v=SQL.110).aspx" target="_blank"&gt;documented in Books Online under the ORDER BY topic&lt;/a&gt;. &lt;br&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;&lt;br&gt;&lt;font size="4"&gt;&lt;span style="font-weight:bold;"&gt;Conclusion&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;Some may consider this a small victory, if a victory at all, but I suspect there is a large faction of people out there who are rejoicing in the introduction of a cohesive way to implement paging.&amp;nbsp; It is probably not as flexible as some of you might have expected, but for a very first try I think we'll take what we can get.&lt;/p&gt;&lt;p&gt;&amp;nbsp; &lt;br&gt;&lt;/p&gt;</description></item></channel></rss>