Re: Pagination - 1 or 2 queries?

From: "scott(dot)marlowe" <scott(dot)marlowe(at)ihs(dot)com>
To: CSN <cool_screen_name90001(at)yahoo(dot)com>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: Pagination - 1 or 2 queries?
Date: 2003-09-05 18:03:15
Message-ID: Pine.LNX.4.33.0309051157340.31096-100000@css120.ihs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, 5 Sep 2003, CSN wrote:

>
> --- "scott.marlowe" <scott(dot)marlowe(at)ihs(dot)com> wrote:
> > On Fri, 5 Sep 2003, CSN wrote:
> >
> > > Since you usually need to know the total number of
> > > rows a query would return, do you think it's
> > better
> > > to:
> > >
> > > a) Do one query with a LIMIT and OFFSET to get the
> > > results, and another COUNT query to get the total
> > > number of rows?
> > >
> > > b) Do a single query without a LIMIT and OFFSET,
> > then
> > > do a seek or similiar to get at the rows you want?
> >
> > >
> > > Most tutorials, code, etc. I've seen do "a". The
> > > eclipse library does "b".
> >
> > Either way works. Does the eclipse library use a
> > cursor, or grab the
> > whole dataset and then seek on the client side? If
> > it uses a cursor, I'd
> > expect it to be the fastest and simplest
> > implementation. Since a lot of
> > libs are designed to work with MySQL, they often are
> > written in the first
> > method, where select count(*) is quite quick on
> > MySQL, and MySQL doesn't
> > have cursor support.
> >
> > With Postgresql, the cursor is likely to be the
> > faster method.
> >
>
> Eclipse appears to just use pg_fetch_array($result,
> $index). That'd be pretty similiar to a cursor
> wouldn't it? i.e. only the specified rows would be
> sent to the client (but all rows would be in the
> server's memory).
>
> Eclipse's docs make the argument that "b" is better
> because "a" still needs to select/examine all rows
> before doing the LIMIT and OFFSET.

If they aren't explicitly declaring a cursor, then b isn't exactly the
same. If you do:

select * from table order by fieldname

then

$row = pg_fetch_array()

then the whole data set is returned to the client (i.e. php) before we can
get the row. Now, if they do:

begin;
declare bubba as cursor for select * from table order by fieldname;
move forward 100 in bubba;
fetch 5 from bubba;
rollback;

Then you get the same kind of effect, but only 5 rows have to be retrieved
from the database to the client, and pg_fetch_array will now iterate over
those 5 rows only, and then run dry, so to speak.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Darko Prenosil 2003-09-05 18:05:21 Re: How can I set postmaster as a service
Previous Message CSN 2003-09-05 17:51:34 Re: Pagination - 1 or 2 queries?