Re: count and limit

From: Chris <dmagick(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: count and limit
Date: 2006-08-18 00:52:32
Message-ID: 44E50F50.40609@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Tom Lane wrote:
> Chris <dmagick(at)gmail(dot)com> writes:
>> Fabio Victora Hecht wrote:
>>> I was wondering if there's a way to count the results of a query and
>>> return part of the result set it in one query (LIMIT).
>
>> I was going to suggest a cursor but I don't think you can get the number
>> of results a cursor has :(
>
> In general it's not possible to determine the number of rows a query
> will return without actually executing it to completion.
>
> You could use a cursor like this:
>
> begin;
> declare c cursor for select ... ;
> fetch 20 from c;
> -- display the first 20 results
> move forward all in c;
> -- note the count returned by MOVE, add 20 to get the total
>
> but if you're expecting the MOVE to be instantaneous, prepare to be
> disappointed.
>
> If you're willing to settle for a (very) approximate count, there are
> things you could do. Some people just feed the query to EXPLAIN and
> grab the rowcount estimate out of the first line of output.

Interesting idea :) I wasn't sure if cursor did the whole query and
(somehow) stored it in memory.. obviously not (and of course that would
blow your memory usage out of the water for big result sets so there
goes that idea anyway!)..

--
Postgresql & php tutorials
http://www.designmagick.com/

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Chris 2006-08-18 00:53:26 Re: count and limit
Previous Message Michael Fuhr 2006-08-18 00:49:33 Re: count and limit