Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 637A72E2DF2 for ; Thu, 6 Mar 2008 23:00:27 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 81594-01 for ; Thu, 6 Mar 2008 23:00:23 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from mail.emolecules.com (66-226-76-106.dedicated.abac.net [66.226.76.106]) by postgresql.org (Postfix) with ESMTP id 24B352E2B28 for ; Thu, 6 Mar 2008 23:00:20 -0400 (AST) Received: (qmail 14384 invoked by uid 2020); 6 Mar 2008 19:00:19 -0800 Received: from wsip-68-224-174-233.sd.sd.cox.net (HELO lisa-james-computer.local) (68.224.174.233) by 192.168.0.4 with SMTP; 6 Mar 2008 19:00:19 -0800 Message-ID: <47D0AFC1.5070305@emolecules.com> Date: Thu, 06 Mar 2008 19:00:17 -0800 From: Craig James User-Agent: Thunderbird 2.0.0.12 (Macintosh/20080213) MIME-Version: 1.0 To: Tom Lane , pgsql-performance@postgresql.org Subject: Re: count * performance issue References: <20080306060829.GA21084@a-kretschmer.de> <47CF8EA6.8070503@mark.mielke.cc> <20080306063643.GB21084@a-kretschmer.de> <3E9C3A51-C28E-4B75-9739-E344DAC72880@fastcrypt.com> <47D00DB2.7040007@emolecules.com> <18451.1204824562@sss.pgh.pa.us> In-Reply-To: <18451.1204824562@sss.pgh.pa.us> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200803/142 X-Sequence-Number: 29096 Tom Lane wrote: > Craig James writes: >> Count() on Oracle and MySQL is almost instantaneous, even for very large tables. So why can't Postgres do what they do? > > AFAIK the above claim is false for Oracle. They have the same > transactional issues we do. My experience doesn't match this claim. When I ported my application from Oracle to Postgres, this was the single biggest performance problem. count() in Oracle was always very fast. We're not talking about a 20% or 50% difference, we're talking about a small fraction of a second (Oracle) versus a minute (Postgres) -- something like two or three orders of magnitude. It may be that Oracle has a way to detect when there's no transaction and use a faster method. If so, this was a clever optimization -- in my experience, that represents the vast majority of the times you want to use count(). It's not very useful to count the rows of a table that many apps are actively modifying since the result may change the moment your transaction completes. Most of the time when you use count(), it's because you're the only one modifying the table, so the count will be meaningful. Craig