Our application displays data in groups of 15.
That way, the user can view 15 customers sorted by name, click a 'next' button, and get the next 15.
As there is lots of data involved, it is no option to buffer all data of all customers. So, I've been looking for the most efficient way to get rows in sets: Select records 30 to 45 etc.
The thing that bugs me is that this doesn't work:
Judging by the performance, it seems that SQL-server reevalutes that 'SELECT TOP 30' for every record again, and on 100.000 records that is not very practical.
The solution therefore seems to be to create a cursor, tempory-table or recordset with primary-keys, then do a new select using these PK's.
But I still wonder: as SQL-server has to sort and count on a whole table anyway to do the 'SELECT TOP', why can't it also return numbers 30 to 45? Did I just overlook a key-word?
That way, the user can view 15 customers sorted by name, click a 'next' button, and get the next 15.
As there is lots of data involved, it is no option to buffer all data of all customers. So, I've been looking for the most efficient way to get rows in sets: Select records 30 to 45 etc.
The thing that bugs me is that this doesn't work:
Code:
SELECT TOP 15 PK FROM mytable WHERE ...
AND PK NOT IN(SELECT TOP 30 PK FROM mytable WHERE ... ORDER BY ...) ORDER BY ...
The solution therefore seems to be to create a cursor, tempory-table or recordset with primary-keys, then do a new select using these PK's.
Code:
SELECT TOP 45 PK FROM mytable WHERE ... ORDER BY ...