SQL查询跟踪问题

时间:2011-05-10 20:13:47

标签: c# .net tsql sql-server-2008 paging

好的,这是我的问题。

用户可以访问我的网站并一次检索8条记录,然后他/她可以选择加载更多。这8个记录可以通过传递给proc的参数进行排序。现在,当我在前端获得这8个记录时,我有他们的ID(显然对用户是隐藏的),但他们的ID没有任何特定的顺序,因为记录按各种可能的东西排序。

当他们点击“加载更多”时,我应该能够从数据库中获取接下来的8条记录,并按照前8种方式排序。

例如,“给我按年龄排序的前8条记录”。 - >点击加载更多 - >给我下一个最古老的8个记录,但没有向我展示我刚才看到的那些内容。

如何调用proc并确保返回第一个结果集中没有?出于效率原因,我一次只想返回8条记录。

SELECT TOP 8
      m.message,
      m.votes,
      (geography::Point(@latitude, @longitude, 4326).STDistance(m.point)) * 0.000621371192237334 as distance,
      m.location,
      datediff(hour,m.timestamp, getdate()) as age,
      m.messageId,
      ml.voted,
      ml.flagged
    FROM
      tblMessages m
    left join tblIPMessageLink ml on m.messageid = ml.messageid
    WHERE
      m.timestamp >= DATEADD(day, DATEDIFF(day, 0, @date), 0)
      and
      m.timestamp < DATEADD(day, DATEDIFF(day, 0, @date), 1)
      ORDER BY
        CASE WHEN @sort = 'votes1' THEN m.votes END DESC,
        CASE WHEN @sort = 'votes2' THEN m.votes END ASC,
        CASE WHEN @sort = 'age1' THEN datediff(hour,m.timestamp, getdate()) END ASC,
        CASE WHEN @sort = 'age2' THEN datediff(hour,m.timestamp, getdate()) END DESC,
        CASE WHEN @sort = 'distance1' THEN (geography::Point(@latitude, @longitude, 4326).STDistance(m.point)) * 0.000621371192237334 END ASC,
        CASE WHEN @sort = 'distance2' THEN (geography::Point(@latitude, @longitude, 4326).STDistance(m.point)) * 0.000621371192237334 END DESC
      END

这是我目前的查询。如何更改它以使用分页?

2 个答案:

答案 0 :(得分:3)

使用row_number

例如

致电1

;WITH cte AS(SELECT *,row_number() OVER( ORDER BY name) AS rows FROM sysobjects)

SELECT * FROM cte WHERE ROWS BETWEEN 1 AND 8
ORDER BY rows

致电2

;WITH cte AS(SELECT *,row_number() OVER( ORDER BY name) AS rows FROM sysobjects)

SELECT * FROM cte WHERE ROWS BETWEEN 9 AND 16
ORDER BY rows

当然你想使用参数而不是硬编码数字,这样你可以重用查询,如果列可以任意排序那么你可能需要使用动态SQL

编辑,这是它应该是什么样子,你可能还想返回max rownumber,以便你知道可以返回多少行 你也可以使每页的行动态,在这种情况下它会像

@StartRow和(@StartRow + @RowsPerPage)-1之间的行

请务必阅读 Dynamic Search Conditions in T-SQL Version for SQL 2008,了解如何对此进行优化以获得计划重用和更好的计划

无论如何,这是proc,当然没有经过测试,因为我不能在这里运行

DECLARE @StartRow INT,@EndRow INT
--SELECT @StartRow =1, @EndRow = 8


;WITH cte AS (SELECT ROW_NUMBER() OVER (ORDER BY
        CASE WHEN @sort = 'votes1' THEN m.votes END DESC,
        CASE WHEN @sort = 'votes2' THEN m.votes END ASC,
        CASE WHEN @sort = 'age1' THEN datediff(hour,m.timestamp, getdate()) END ASC,
        CASE WHEN @sort = 'age2' THEN datediff(hour,m.timestamp, getdate()) END DESC,
        CASE WHEN @sort = 'distance1' THEN (geography::Point(@latitude, @longitude, 4326).STDistance(m.point)) * 0.000621371192237334 END ASC,
        CASE WHEN @sort = 'distance2' THEN (geography::Point(@latitude, @longitude, 4326).STDistance(m.point)) * 0.000621371192237334 END DESC
      END) AS rows 
      m.message,
      m.votes,
      (geography::Point(@latitude, @longitude, 4326).STDistance(m.point)) * 0.000621371192237334 as distance,
      m.location,
      datediff(hour,m.timestamp, getdate()) as age,
      m.messageId,
      ml.voted,
      ml.flagged
    FROM
      tblMessages m
    left join tblIPMessageLink ml on m.messageid = ml.messageid
    WHERE
      m.timestamp >= DATEADD(day, DATEDIFF(day, 0, @date), 0)
      and
      m.timestamp < DATEADD(day, DATEDIFF(day, 0, @date), 1)
      )


     SELECT * 
     FROM cte WHERE ROWS BETWEEN @StartRow AND @EndRow
    ORDER BY rows

答案 1 :(得分:2)

David Hayden在传呼方面有nice article。您只需要跟踪记录数和偏移量。

此外,每次加载更多

时,您仍需要合并并使用客户端上的记录

这是该文章中的SP

CREATE PROCEDURE dbo.ShowLog
    @PageIndex INT, 
    @PageSize INT 
AS

BEGIN 

WITH LogEntries AS ( 
SELECT ROW_NUMBER() OVER (ORDER BY Date DESC)
AS Row, Date, Description 
FROM LOG)

SELECT Date, Description
FROM LogEntries 
WHERE Row between 
(@PageIndex - 1) * @PageSize + 1 and @PageIndex*@PageSize



END