[Switch] High Performance tab statement SQLServer

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https: //blog.csdn.net/scholar_man/article/details/80782324


The first method: maximum efficiency

The SELECT  the TOP page size * 

the FROM ( the SELECT the ROW_NUMBER () the OVER ( the ORDER  BY ID) the AS RowNumber, *  the FROM table1) AS A 

the WHERE RowNumber > page size * (Page - . 1 ) 
 

- Note: firstly Row_number () is a table table1 add a row for each row number, row number to the columns named 'RowNumber' in over () method in the 'RowNumber' ascending made 

- then 'RowNumber' form a row of table a and all columns of the table table1 

- focus on where conditions. If this page (The currentPage) is page 2, page 10 displays data (pageSzie). Then the data of the first page is the first 11-20 

- so in order to display data of the second page, i.e., the display of data 11-20, then let RowNumber greater than 10 * (2-1)

Stored Procedures (table name aa)

IF ( EXISTS ( the SELECT *  from sys.procedures the WHERE name = ' p_location_paging ' )) - If this stored procedure exists p_location_paging 

drop  proc p_location_paging   - then delete the stored procedure 

Go 

the Create  proc p_location_paging ( @pageSize  int , @currentPage  int ) - create a stored procedure, define two variables 'number of bars displayed per page' and 'current page' 

AS 

the SELECT   Top ( @pageSize ) *  from ( 

the SELECT ROW_NUMBER() over(order by locid) as rowid ,* from  aa

)as A where rowid> (@pageSize)*((@currentPage)-1)

 


 


The second method: Efficiency followed

The SELECT  TOP page size *   - If the data per page 10, this is 10 data query 

the FROM table1 

the WHERE the above mentioned id >   - if the current page is the third page, then you need to query 21-30 pieces of data, namely: id > 20 

        ( 

            the SELECT  ISNULL ( MAX (the above mentioned id), 0 )   - subquery largest the above mentioned id 

            the FROM 

                ( 

                    the SELECT  TOP page size * (current page - 1 ) the above mentioned id the FROM table1 the ORDER  BY the above mentioned id
 -Because the current page is the third page, each page displays ten data. Then I will: page size * (current page -1), is to get to the "front" and "current page" 20 data. Therefore, the above queries max (id) id largest, to take the 20, the condition where the front id> 20 i.e. to fetch the data for the third page of data that is to take 21-30 

                ) AS A 

           ) 

the ORDER  BY id

存储过程(表名 aa)

if(exists(select * from sys.procedures where name='p_location_paging'))

drop proc p_location_paging

go

create proc p_location_paging(@pageSize int ,@currentPage int)

as

select  top (@pageSize) * from aa

where locId>(select ISNULL(MAX(locId),0)

from (select top ((@pageSize)*(@currentPage-1))locid from location  order by locId) as a

)order by locId


The third method: the worst

The SELECT  TOP page size * 

the FROM table1 

the WHERE the above mentioned id the NOT  the IN  - data where limited conditional statements to query sub-query data is not contained inside. I.e., the query data following the 10 "subquery." I.e. this page data 

        ( 

           - if this page is the second page, data per page 10, this is an overview of all the data ahead of the current page. 

           The SELECT  the TOP page size * (this page - . 1 ) ID the FROM table1 the ORDER  BY the above mentioned id 

        ) 

the ORDER  BY the above mentioned id

 



Guess you like

Origin www.cnblogs.com/ypynb/p/11653001.html