Re: Returning the total number of rows as a separate column when using limit

From: Gregory Stark <stark(at)enterprisedb(dot)com>
To: "Andreas Joseph Krogh" <andreak(at)officenet(dot)no>
Cc: <pgsql-sql(at)postgresql(dot)org>, "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Subject: Re: Returning the total number of rows as a separate column when using limit
Date: 2007-11-05 18:54:52
Message-ID: 87fxzkwmir.fsf@oxford.xeocode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


"Andreas Joseph Krogh" <andreak(at)officenet(dot)no> writes:

> That's what I'm doing now. I run the query with "limit+1" as limit and if it
> results in more than limit, I know there is more data and I run count(*) to
> count them all. But count(*) cannot use indices in PG so it's limited in
> speed anyway AFAICS.

Well count(*) can use indexes the same as the query can.

> I really hoped there was an "Oracle over()" equivalent way in PG. I understand
> that Oracle's LIMIT-hack with "3 subselects and rownum between 1 AND 20" is
> rather expensive compared to PG's implementation of LIMIT. Oralce keeps
> snapshot-info in the index, so counting only involves the index AFAIK.

Well that's only going to be true if the index satisfies the whole query which
is not going to be true for the simplest cases.

--
Gregory Stark
EnterpriseDB http://www.enterprisedb.com
Ask me about EnterpriseDB's PostGIS support!

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Gregory Stark 2007-11-06 06:58:59 Re: Returning the total number of rows as a separate column when using limit
Previous Message Andreas Joseph Krogh 2007-11-05 16:33:10 Re: Returning the total number of rows as a separate column when using limit