SQL Server 2005 Paging – The Holy Grail

  • SwePeso (12/6/2010)


    It seems the dual TOP/ORDER BY was the most efficient approach by the time.

    I tested the new Denali OFFSET/FETCH and I got the exact same number of reads for @StartRow = 5000, but 2 ms instead of 4 ms.

    SELECTNumber

    FROMdbo.TallyNumbers

    ORDER BYNumber

    OFFSET@StartRow - 1 ROWS

    FETCH NEXT50 ROWS ONLY

    This is unreleased SQL Server 2011 functionality only, right?

  • SwePeso (12/6/2010)


    It seems the dual TOP/ORDER BY was the most efficient approach by the time.

    I tested the new Denali OFFSET/FETCH and I got the exact same number of reads for @StartRow = 5000, but 2 ms instead of 4 ms.

    SELECTNumber

    FROMdbo.TallyNumbers

    ORDER BYNumber

    OFFSET@StartRow - 1 ROWS

    FETCH NEXT50 ROWS ONLY

    My take on the Denali paging....

    http://sqlblogcasts.com/blogs/sqlandthelike/archive/2010/11/10/denali-paging-is-it-win-win.aspx

    and

    http://sqlblogcasts.com/blogs/sqlandthelike/archive/2010/11/19/denali-paging-key-seek-lookups.aspx



    Clear Sky SQL
    My Blog[/url]

  • This is good. But i wanted to ask, what about the situation in which, say there are only 5 records in the table, and you ask for page number 2 with page size 10?

    In that case there is no data returned to you, and you will not get to know how many records are there, in the table.

  • I'm quite surprised that the "holy grail of paging" doesn't mention the (granted a bit less popular) "seek method" as described here:

    http://blog.jooq.org/2013/10/26/faster-sql-paging-with-jooq-using-the-seek-method/[/url]

    Take the following example:

    SELECT TOP 10 first_name, last_name, score

    FROM players

    WHERE (score < @previousScore)

    OR (score = @previousScore AND player_id < @previousPlayerId)

    ORDER BY score DESC, player_id DESC

    The @previousScore and @previousPlayerId values are the respective values of the last record from the previous page. This allows you to fetch the "next" page. If the ORDER BY direction is ASC, simply use > instead.

    With the above method, you cannot immediately jump to page 4 without having first fetched the previous 40 records. But often, you do not want to jump that far anyway. Instead, you get a much faster query that might be able to fetch data in constant time, depending on your indexing. Plus, your pages remain "stable", no matter if the underlying data changes (e.g. on page 1, while you're on page 4).

    This is the best way to implement paging when lazy loading more data in web applications, for instance.

  • Sorry, but at first blush I think parameter sniffing (without some other query addition) could totally screw you here. Maybe an OPTION (RECOMPILE) could help, depending on what version of SQL Server you are on.

    Best,
    Kevin G. Boles
    SQL Server Consultant
    SQL MVP 2007-2012
    TheSQLGuru on googles mail service

Viewing 5 posts - 61 through 64 (of 64 total)

You must be logged in to reply to this topic. Login to reply