Double query (limit and offset)

From: Michael Landin Hostbaek <mich(at)freebsdcluster(dot)org>
To: PostgresSQL list <pgsql-sql(at)postgresql(dot)org>
Subject: Double query (limit and offset)
Date: 2005-10-11 15:11:05
Message-ID: 20051011151105.GA29943@mich2.itxmarket.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

List,

I'm using the OFFSET / LIMIT combo in order to split up my query, so it
only parses 20 rows at a time (for my php-scripted webpage).

I'm using two queries; the first basically doing a select count(*) from
[bla bla]; the second grabbing the actual data while setting LIMIT and
OFFSET.

In addition, I'm using the first query plus some calculations to parse
total hits to the query, and number of pages etc etc.

Now, my problem is this, the first query is simply counting all rows
from the main table, whereas the second query has plenty of JOINS, and a
GROUB BY statement - it's a fairly heavy query. The total (reported by
the first query), it not at all the same as the amount of rows returned
by the second query. I'd like to avoid having to run the "heavy" query
twice, just in order to get the number of rows.

Is there a smarter way of doing it ?

Thanks,

/mich

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message David Fetter 2005-10-11 18:35:21 Re: generating a sequence table against which to do a LEFT OUTER JOIN
Previous Message Richard Huxton 2005-10-11 14:39:21 Re: Returning NULL results?