SQL 서버에서 페이지 나눔 처리에 대해 쿼리를 분석해 본다.
SQL서버는 일반적으로 auto increment idx번호를 부여하지 않고 사용하여 설계되는 경우가 많습니다.
Getting the total number of results and paginating are two different operations. For the sake of this example, let's assume that the query you're dealing with is
SELECT * FROM Orders WHERE OrderDate >= '1980-01-01' ORDER BY OrderDate
In this case, you would determine the total number of results using:
SELECT COUNT(*) FROM Orders WHERE OrderDate >= '1980-01-01'
...which may seem inefficient, but is actually pretty performant, assuming all indexes etc. are properly set up.
Next, to get actual results back in a paged fashion, the following query would be most efficient:
SELECT *
FROM ( SELECT ROW_NUMBER() OVER ( ORDER BY OrderDate ) AS RowNum, *
FROM Orders
WHERE OrderDate >= '1980-01-01'
) AS RowConstrainedResult
WHERE RowNum >= 1
AND RowNum < 20
ORDER BY RowNum
This will return rows 1-19 of the original query. The cool thing here, especially for web apps, is that you don't have to keep any state, except the row numbers to be returned.
참고: https://stackoverflow.com/questions/109232/what-is-the-best-way-to-paginate-results-in-sql-server
'DB관련 > SQL Server' 카테고리의 다른 글
MS SQL retore 에러 - Specified cast is not valid. (SqlMangerUI) (0) | 2019.05.18 |
---|---|
MS SQL DB 복원시 Access is denied.오류 발생시 (0) | 2019.03.05 |
SQL Local DB 2017 (Express버전에서 LocalDB선택해서 설치) + SQL Operation Studio (0) | 2018.08.17 |
SQL Server2014 Standard 설치 및 삭제 DB Instance 수동 삭제하기 (0) | 2018.08.15 |
mssql - sqlcmd명령으로 update query를 파일로 만들어 실행하기. (0) | 2018.02.21 |
SQLServer ldf 로그 파일 줄이기 - SQL Server Management Studio 사용 (0) | 2018.02.07 |
sqlcmd 이용한 쿼리 확인하기 (Management Studio가 없을때) (0) | 2017.12.09 |
MSSQL 컬럼 변경 ( 추가, 삭제, 속성변경, 컬럼명 변경 ) (0) | 2017.10.12 |
(로그인하지 않으셔도 가능)