Re: Simple SQL Question - Mailing list pgsql-sql

From Greg Stark
Subject Re: Simple SQL Question
Date
Msg-id 87zn1vl7bs.fsf@stark.xeocode.com
Whole thread Raw
In response to Re: Simple SQL Question  (Tomasz Myrta <jasiek@klaster.net>)
List pgsql-sql
Tomasz Myrta <jasiek@klaster.net> writes:

> > select * from table1 LIMIT x
> > gives me the first x row of the result.
> > After that, I save the last value, and next time, I adjust
> > the query as
> > select * from table1 where itemkey>:lastvalue LIMIT x
> 
> Why do you complicate it so much? Everything you need is:
> 
> select * from table1 LIMIT x
> select * from table1 LIMIT x OFFSET x
> select * from table1 LIMIT x OFFSET 2*x
> 
> Remember to sort rows before using limit/offset.

There are two good reasons to prefer his Andras' solution to yours.

a) If the data is modified between the two queries his will continue from
where the previous page left off. Yours will either skip records or overlap
with the previous page.

b) If itemkey is indexed his will be an efficient index scan that performs
similarly regardless of what page is being fetched. Yours will perform more
and more slowly as the user gets deeper into the results.


Note that both queries are wrong however. You need an "ORDER BY itemkey" or
else nothing guarantees the second page has any relation at all to the first
page.

-- 
greg



pgsql-sql by date:

Previous
From: Ian Barwick
Date:
Subject: Re: oracle v$session equivalent in postgresql
Next
From: T E Schmitz
Date:
Subject: tricky GROUP BY / JOIN question