Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id DF56F9FBBDC for ; Thu, 22 Mar 2007 09:28:26 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-new, port 10024) with ESMTP id 87572-09 for ; Thu, 22 Mar 2007 09:28:18 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from heaven.kostyrka.org (h1634.serverkompetenz.net [81.169.154.44]) by postgresql.org (Postfix) with ESMTP id 2117B9FBBD4 for ; Thu, 22 Mar 2007 09:28:23 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by heaven.kostyrka.org (Postfix) with ESMTP id 083F94FB8A; Thu, 22 Mar 2007 13:28:21 +0100 (CET) Received: from heaven.kostyrka.org ([127.0.0.1]) by localhost (heaven.kostyrka.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 01962-01; Thu, 22 Mar 2007 13:28:20 +0100 (CET) Received: from andi-lap (localhost [127.0.0.1]) by heaven.kostyrka.org (Postfix) with SMTP id D33824FB7C; Thu, 22 Mar 2007 13:28:18 +0100 (CET) Received: by andi-lap (sSMTP sendmail emulation); Thu, 22 Mar 2007 13:29:46 +0100 Date: Thu, 22 Mar 2007 13:29:46 +0100 From: Andreas Kostyrka To: Andreas Tille Cc: pgsql-performance@postgresql.org Subject: Re: Performance of count(*) Message-ID: <20070322122944.GD4439@andi-lap.la.revver.com> References: <20070322111044.GC4439@andi-lap.la.revver.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: mutt-ng/devel-r804 (Debian) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200703/310 X-Sequence-Number: 23715 * Andreas Tille [070322 13:24]: > On Thu, 22 Mar 2007, Andreas Kostyrka wrote: > > >Which version of PG? > > Ahh, sorry, forgot that. The issue occurs in Debian (Etch) packaged > version 7.4.16. I plan to switch soon to 8.1.8. I'd recommend 8.2 if at all possible :) > > >That's the reason why PG (check the newest releases, I seem to > >remember that there has been some aggregate optimizations there), > > I'll verify this once I moved to the new version. 8.1 won't help you I'd guess. ;) > > >does > >a SeqScan for select count(*) from table. btw, depending upon your > >data, doing a select count(*) from table where user=X will use an > >Index, but will still need to fetch the rows proper to validate them. > > I have an index on three (out of 7 columns) of this table. Is there > any chance to optimize indexing regarding this. Well, that depends upon you query pattern. It's an art and a science at the same time ;) > > Well, to be honest I'm not really interested in the performance of > count(*). I was just discussing general performance issues on the > phone line and when my colleague asked me about the size of the > database he just wonderd why this takes so long for a job his > MS-SQL server is much faster. So in principle I was just asking > a first question that is easy to ask. Perhaps I come up with > more difficult optimisation questions. Simple. MSSQL is optimized for this case, and uses "older" datastructures. PG uses a MVCC storage, which is not optimized for this usecase. It's quite fast for different kinds of queries. The basic trouble here is that mvcc makes it a little harder to decide what is valid for your transaction, plus the indexes seems to be designed for lookup, not for data fetching. (Basically, PG can use indexes only to locate potential data, but cannot return data directly out of an index) Andreas