Turn pagination achieve: Offset-Fetch

Author : Wyatt time
This article belongs to the author and blog Park all, welcome to reprint, but without the author's consent, declared by this section must be retained, and the display is connected to the original article page prominently, otherwise the right to pursue legal responsibilities.

 

Paging method implemented now have one, in the SQL Server 2012 version, TSQL Order By clause in the new Offset-Fetch clause for centrally ordered from the results, skip a certain number of rows of data, access specified amount of data lines, the data lines so as to achieve the purpose of paging. After testing, the number of logical reads consumption and response time to the evaluation, using the pagination Offset-Fetch achieved, than many Row_Number () higher performance mode.

Offset-Fetch clause ordered result set is required, therefore, only be used in order by clause in the syntax is as follows:

Copy the code
ORDER BY order_by_expression [ ASC | DESC ]  [ ,...n ] [ <offset_fetch> ]
<offset_fetch> ::=
{ 
    OFFSET { integer_constant | offset_row_count_expression } ROWS
    [ FETCH NEXT {integer_constant | fetch_row_count_expression } ROWS ONLY ]
}
Copy the code

Keyword Analysis:

  • Offset clauses : specifies the skipped (Skip) of the data line;
  • Fetch clause : Offset is performed after the clause clause indicates after skipping (Sikp) a specified number of rows of data, a certain amount of data returned rows;
  • Execution order : Offset clause must be performed after Order By clause, Fetch clause clause must be performed after Offset;

Paging implemented ideas:

  1. In the tab implementations, Order By clause, sort the result set according to the specified Columns;
  2. Use Offset clause skip the first N pages: Offset (@ PageIndex-1) * @ RowsPerPage rows;
  3. Use Fetch clause is to render the current Page: Fetch next @RowsPerPage rows only;

First, using the tab order-offset-fetch

Creating sample data

  View Code

1, using the Offset clause skip a specified number of rows

select * 
from dbo.dt_test
order by id
offset 2 rows

2, after use Offset-Fetch clause skip a specified number of data lines, a specified number of rows returned

select * 
from dbo.dt_test
order by id
offset 2 rows
fetch next 2 rows only

3, the general format modified page component

Copy the code
- page index, the page number starting with 1
declare @PageIndex int
- the number of lines per page
declare @Size int

set @PageIndex=1
set @Size=100

select * 
from dbo.dt_test
order by id
offset (@PageIndex - 1) * @Size rows
fetch next @Size rows only
Copy the code

Second, the sort (order by)

Order by clause is the syntax: ORDER BY order_by_expression, sorted according to a specified field, usually there are three kinds of writing:

  • select clause column name, or Alias, sorting clause (order by) the execution order after the select clause, you may be used to sort the column Alias;
  • Expression, sorted according to the results of the expression;
  • Number of columns in the select clause, from a start number value here is not recommended;

The three will be written on the query result set to sort the returned result set is ordered, but if written using a constant by clause in order:

order by (select 1) 

The clause is not a number of columns, but constant, SQL Server in the original order of the result set returned, order by clause does not sort the result set.

Guess you like

Origin www.cnblogs.com/Xupp/p/11080980.html