Question

This is a followup on the question: ASP.NET next/previous buttons to display single row in a form

As it says on the page above, theres a previous/next button on the page, that retrieves a single row one at a time.

Totally there's ~500,000 rows.

When I "page" through each subscribtion number, the form gets filled with subscriber details. What approach should I use on the SQL server?

Using the ROW_NUMBER() function seems a bit overkill as it has to number all ~500.000 rows (I guess?), so what other possible solutions are there?

Thanks in advance!

Was it helpful?

Solution

ROW_NUMBER() is probably your best choice.

From this MSDN article: http://msdn.microsoft.com/en-us/library/ms186734.aspx

WITH OrderedOrders AS
(
    SELECT SalesOrderID, OrderDate,
    ROW_NUMBER() OVER (ORDER BY OrderDate) AS 'RowNumber'
    FROM Sales.SalesOrderHeader 
) 
SELECT * 
FROM OrderedOrders 
WHERE RowNumber BETWEEN 50 AND 60;

And just subsititute 50 and 60 with a parameter for the row number you want.

OTHER TIPS

Tommy, if your user has time to page through 500,000 rows at one page per row, then he/she is unique.

I guess what I am saying here is that you may be able to provide a better UX. When - Too many pages? Build a search feature.

There are two potential workarounds (for this purpose, using a start of 201, pages of 100):

SQL

SELECT TOP 100 * FROM MyTable WHERE ID > 200 ORDER BY ID

LINQ to SQL

var MyRows = (from t in db.Table 
              order by t.ID ascending
              select t).Skip(200).Take(100)

If your ID field has a clustered index, use the former. If not, both of these will take the same amount of time (LINQ returns 500,000 rows, then skips, then takes).

If you're sorting by something that's NOT ID and you have it indexed, use ROW_NUMBER().

Edit: Because the OP isn't sorting by ID, the only solution is ROW_NUMBER(), which is the clause that I put at the end there.

In this case, the table isn't indexed, so please see here for ideas on how to index to improve query performance.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top