高效的sql server分页

发布于 2024-12-09 15:20:32 字数 927 浏览 0 评论 0原文

由于前两条评论,我删除了自己的所有代码,并将 4 个人直接提供的示例放在这里。

我对“select @first_id”应该如何编码感兴趣。该示例显示了使用联接拉取的行,我希望 first_id 不是有效的开始位置,因为它不使用相同的联接语法。

CREATE  PROCEDURE [dbo].[usp_PageResults_NAI] 
(
    @startRowIndex int,
    @maximumRows int
)
AS

DECLARE @first_id int, @startRow int

-- A check can be added to make sure @startRowIndex isn't > count(1)
-- from employees before doing any actual work unless it is guaranteed
-- the caller won't do that

-- Get the first employeeID for our page of records
SET ROWCOUNT @startRowIndex
SELECT @first_id = employeeID FROM employees ORDER BY employeeid

-- Now, set the row count to MaximumRows and get
-- all records >= @first_id
SET ROWCOUNT @maximumRows

SELECT e.*, d.name as DepartmentName 
FROM employees e
   INNER JOIN Departments D ON
       e.DepartmentID = d.DepartmentID
WHERE employeeid >= @first_id
ORDER BY e.EmployeeID

SET ROWCOUNT 0

GO 

Due to the first two comments I've removed all my own code and placed the example directly from 4 guys here.

I'm interested in how the 'select @first_id' should be coded. The example shows the rows being pulled using joins and I would expect that the first_id wouldn't be a valid place to start because it doesn't use the same join syntax.

CREATE  PROCEDURE [dbo].[usp_PageResults_NAI] 
(
    @startRowIndex int,
    @maximumRows int
)
AS

DECLARE @first_id int, @startRow int

-- A check can be added to make sure @startRowIndex isn't > count(1)
-- from employees before doing any actual work unless it is guaranteed
-- the caller won't do that

-- Get the first employeeID for our page of records
SET ROWCOUNT @startRowIndex
SELECT @first_id = employeeID FROM employees ORDER BY employeeid

-- Now, set the row count to MaximumRows and get
-- all records >= @first_id
SET ROWCOUNT @maximumRows

SELECT e.*, d.name as DepartmentName 
FROM employees e
   INNER JOIN Departments D ON
       e.DepartmentID = d.DepartmentID
WHERE employeeid >= @first_id
ORDER BY e.EmployeeID

SET ROWCOUNT 0

GO 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江南烟雨〆相思醉 2024-12-16 15:20:32

您可以使用ROW_NUMBER()进行高效分页

DECLARE @skipRows Int = 10 --Change to input parameter to sp
DECLARE @takeRows Int = 20 --Change to input parameter to sp

SELECT *
FROM (
    SELECT 
         ROW_NUMBER() OVER (ORDER BY a.DateCreated) As RowNumber, 
         a.PKID, 
         a.AlertUrl, 
         a.AlertDescription, 
         a.Users_PKID_creator, 
         dbo.Users_GetFullName(a.Users_PKID_creator) as Users_FullName,
         a.Dealers_PKID, 
         d.Dealer,
         dbo.convertDateFromUTC(a.DateCreated, @dealers_pkid) as DateCreated,
         dbo.convertDateFromUTC(a.DateCreated, @dealers_pkid) as ComparisonDate,
         dbo.convertDateFromUTC(a.DateModified, @dealers_pkid) as DateModified,
         a.Active,
         a.Contacts_PKID,
         dbo.Contacts_GetFullName(a.Contacts_PKID) as Contacts_FullName
from     Alerts a
join     Dealers d on d.PKID = a.Dealers_PKID
where    a.DateCreated between dbo.convertDateToUTC(@datetimeDateStart, @dealers_pkid) and dbo.convertDateToUTC(@datetimeDateEnd, @dealers_pkid)
and      a.Active = @bitActive
and      a.PKID >= @first_id
    ) AS [t1]
WHERE [t1].RowNumber BETWEEN @skipRows + 1 AND @skipRows + @takeRows 

You can to efficient paging using ROW_NUMBER()

DECLARE @skipRows Int = 10 --Change to input parameter to sp
DECLARE @takeRows Int = 20 --Change to input parameter to sp

SELECT *
FROM (
    SELECT 
         ROW_NUMBER() OVER (ORDER BY a.DateCreated) As RowNumber, 
         a.PKID, 
         a.AlertUrl, 
         a.AlertDescription, 
         a.Users_PKID_creator, 
         dbo.Users_GetFullName(a.Users_PKID_creator) as Users_FullName,
         a.Dealers_PKID, 
         d.Dealer,
         dbo.convertDateFromUTC(a.DateCreated, @dealers_pkid) as DateCreated,
         dbo.convertDateFromUTC(a.DateCreated, @dealers_pkid) as ComparisonDate,
         dbo.convertDateFromUTC(a.DateModified, @dealers_pkid) as DateModified,
         a.Active,
         a.Contacts_PKID,
         dbo.Contacts_GetFullName(a.Contacts_PKID) as Contacts_FullName
from     Alerts a
join     Dealers d on d.PKID = a.Dealers_PKID
where    a.DateCreated between dbo.convertDateToUTC(@datetimeDateStart, @dealers_pkid) and dbo.convertDateToUTC(@datetimeDateEnd, @dealers_pkid)
and      a.Active = @bitActive
and      a.PKID >= @first_id
    ) AS [t1]
WHERE [t1].RowNumber BETWEEN @skipRows + 1 AND @skipRows + @takeRows 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文