X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id DC2945365F for ; Fri, 8 Apr 2005 17:16:20 +0100 (BST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 65956-09 for ; Fri, 8 Apr 2005 16:16:16 +0000 (GMT) Received: from quasar.skima.is (quasar.skima.is [212.30.200.205]) by svr1.postgresql.org (Postfix) with ESMTP id B61DA53635 for ; Fri, 8 Apr 2005 17:16:14 +0100 (BST) Received: from gnari ([157.157.159.208] [157.157.159.208]) by quasar.skima.is with ESMTP; Fri, 8 Apr 2005 16:16:13 Z Subject: Re: getting count for a specific querry From: Ragnar =?ISO-8859-1?Q?Hafsta=F0?= To: Andrew Sullivan Cc: pgsql-sql@postgresql.org In-Reply-To: <20050408150731.GB27718@phlogiston.dyndns.org> References: <000001c53c3e$f4f5b730$797ba8c0@jfradkin> <20050408150731.GB27718@phlogiston.dyndns.org> Content-Type: text/plain Date: Fri, 08 Apr 2005 16:17:45 +0000 Message-Id: <1112977065.7444.30.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Evolution 2.0.2 Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.001 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200504/96 X-Sequence-Number: 20989 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