Paging w/o cursor or temp table?
Posted in 2000
(my apologies for the repost, but the previous had an invalid email
address)
I have an application that needs to be able to deliver a list from a
database, a page at a time.
For example, "all names starting with S." For various reasons,
the application cannot hold open a cursor while waiting for a user to
ask
for the next page, and a temp table is too expensive. Also, I can't
afford to keep the whole result set in memory (all names starting with
S, in a million person table, would be a big result set) and I don't
even want to fetch the whole result set unless the user needs it.
So the question is... how do I do this? When the user hits "next", what
is the best approach?
Also assume that the key is composite... IE it has multiple fields (in
this example: last, first, middle).
The first query could be a simple:
select * from table where lastname >= 'S' and lastname < 'T';For the "next" query, I could do:
select * from table where
((lastname > 'Smith')
or (lastname = 'Smith' and firstname > 'fred'
or (Lastname = 'Smith' and firstname = 'fred' middlename >= 'C'))
and (lastname < 'T');
But this is sort of ugly, and does not generalize well as the number of
fields in the composite key increases. It is also not clear what this
does to the query optimizer.
Furthermore it may retrieve a bunch of records I have already seen,
which I then have to filter out based on some other critera.
Is there any elegant general solution to this problem?
Thanks in advance