Re: Simple SQL Question

From: Greg Stark <gsstark(at)mit(dot)edu>
To: Tomasz Myrta <jasiek(at)klaster(dot)net>
Cc: Andras Kutrovics <n-drew(at)freemail(dot)hu>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Simple SQL Question
Date: 2004-11-06 08:17:43
Message-ID: 87zn1vl7bs.fsf@stark.xeocode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


Tomasz Myrta <jasiek(at)klaster(dot)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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message vasundhar 2004-11-07 06:45:36 Hi
Previous Message Ian Barwick 2004-11-05 19:53:00 Re: oracle v$session equivalent in postgresql