pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ragnar Hafstaư <gnari@simnet.is>
To: Andrew Sullivan <ajs@crankycanuck.ca>
Cc: pgsql-sql@postgresql.org
Subject: Re: getting count for a specific querry
Date: Fri, 08 Apr 2005 16:17:45 +0000
Message-ID: <1112977065.7444.30.camel@localhost.localdomain> (raw)
In-Reply-To: <20050408150731.GB27718@phlogiston.dyndns.org>
References: <000001c53c3e$f4f5b730$797ba8c0@jfradkin>
	<20050408150731.GB27718@phlogiston.dyndns.org>

On Fri, 2005-04-08 at 11:07 -0400, Andrew Sullivan wrote:
> On Fri, Apr 08, 2005 at 09:29:13AM -0400, Joel Fradkin wrote:
> > 
> > Is there a fast way to get the count? 
> 
> Not really, no.  You have to perform a count() to get it, which is
> possibly expensive.  One way to do it, though, is to do 
> 
> 	SELECT count(*) FROM tablename WHERE condition LIMIT n;
> 
> or something like that.  Assuming the condition is reasonably limited
> (i.e. it's not going to cost you a fortune to run this), you'll get
> the right number back if the number is < n or else you'll get
> n.

come again ?

test=# select count(*) from a;
 count
-------
     3
(1 row)

test=# select count(*) from a limit 2;
 count
-------
     3
(1 row)

the LIMIT clause limits the number of rows returned by the select,
in this case 1 row.

maybe you mean something like:

test=# select count(*) from (select * from a limit 2) as foo;
 count
-------
     2
(1 row)

gnari





view thread (38+ messages)  latest in thread

Message-ID: <1112977065.7444.30.camel@localhost.localdomain>
Permalink:  ../1112977065.7444.30.camel@localhost.localdomain/
Also on:    postgresql.org/message-id/1112977065.7444.30.camel@localhost.localdomain

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: gnari@simnet.is, ajs@crankycanuck.ca
  Subject: Re: getting count for a specific querry
  In-Reply-To: <1112977065.7444.30.camel@localhost.localdomain>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox