在SQL Server 2008 R2中使用偏移(分页)查询进行选择的正确方法

时间:2017-02-23 01:36:47

标签: sql-server pagination sql-server-2008-r2

我想从偏移的视图中选择数据以进行分页,并按注册日期排序。

我在下面有代码示例。

SELECT student_id,student_name 
FROM student 
WHERE student_id IN(
                 SELECT TOP 15 student_id 
                 FROM student(
                         SELECT ROW_NUMBER() OVER (ORDER BY register_date_time DESC) AS rowNo,student_id
                         FROM student
                        ) tb_pagination WHERE rowNo > 30
                 )

我的代码运行正常,但从服务器到客户端请求的响应速度太慢。

有谁能告诉我如何让它变得更好?

1 个答案:

答案 0 :(得分:0)

试试这个:

SELECT student_id,student_name 
FROM (
        SELECT ROW_NUMBER() over (ORDER BY register_date_time DESC) AS rowNo, 
        student_id, 
        student_name
        FROM student
    ) tb_pagination
WHERE rowNo > 30 
AND rowNo < 45