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 4B13852A06 for ; Thu, 7 Jul 2005 23:22:00 -0300 (ADT) 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 57238-01 for ; Fri, 8 Jul 2005 02:21:50 +0000 (GMT) Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by svr1.postgresql.org (Postfix) with ESMTP id A0AFA52965 for ; Thu, 7 Jul 2005 23:21:48 -0300 (ADT) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.3/8.13.3) with ESMTP id j682LhEK098399 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 7 Jul 2005 20:21:46 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.3/8.13.3) with ESMTP id j682LhZb045091; Thu, 7 Jul 2005 20:21:43 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.3/8.13.3/Submit) id j682LgTQ045090; Thu, 7 Jul 2005 20:21:42 -0600 (MDT) (envelope-from mfuhr) Date: Thu, 7 Jul 2005 20:21:42 -0600 From: Michael Fuhr To: Varun Mehta Cc: pgsql-sql@postgresql.org Subject: Re: Make COUNT(*) Faster? Message-ID: <20050708022141.GB3339@winnie.fuhr.org> References: <9EF2931D-1097-470D-8ECB-59D00166C96F@apple.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <9EF2931D-1097-470D-8ECB-59D00166C96F@apple.com> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.005 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200507/55 X-Sequence-Number: 22033 On Thu, Jul 07, 2005 at 03:48:39PM -0700, Varun Mehta wrote: > > I've started using PostgreSQL pretty recently, and I am quite > disturbed about the performance of a simple SELECT COUNT(*) FROM > table. What should (in my mind) be a nearly instantaneous operation > instead takes nearly 700ms in a table with only 87k rows of data! Speeding up COUNT is on the developers' TODO list, but it's not as simple as it might seem or it would have been done already. This has been brought up many times over the years -- search the archives to see past discussion. Words to search for include "MVCC," "index," and "visibility." > If I run an EXPLAIN on this query I can see that it is doing a > sequential scan, which seems quite needless, as surely this > information is cached in some secret location. If an estimate will suffice then you could use the table's pg_class.reltuples value, but beware that it can be rather out of date. http://www.postgresql.org/docs/8.0/static/catalog-pg-class.html -- Michael Fuhr http://www.fuhr.org/~mfuhr/