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 8ED62529FD for ; Fri, 8 Jul 2005 01:27:28 -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 48059-08 for ; Fri, 8 Jul 2005 04:27:26 +0000 (GMT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id BCAFD529D7 for ; Fri, 8 Jul 2005 01:27:25 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.1/8.13.1) with ESMTP id j684RNHn008509; Fri, 8 Jul 2005 00:27:23 -0400 (EDT) To: Chris Browne Cc: pgsql-sql@postgresql.org Subject: Re: Make COUNT(*) Faster? In-reply-to: <60vf3mklue.fsf@dba2.int.libertyrms.com> References: <9EF2931D-1097-470D-8ECB-59D00166C96F@apple.com> <60vf3mklue.fsf@dba2.int.libertyrms.com> Comments: In-reply-to Chris Browne message dated "Thu, 07 Jul 2005 23:39:21 -0400" Date: Fri, 08 Jul 2005 00:27:23 -0400 Message-ID: <8508.1120796843@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.006 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200507/59 X-Sequence-Number: 22037 Chris Browne writes: > vmehta@apple.com (Varun Mehta) writes: >> 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. > [ example scenario snipped ] > If there were some "single secret place" with a count, how would you > suggest it address those 78 tuples and 16 transactions that aren't yet > (and maybe never will be) part of the count? It's worse than that: once some of those transactions have committed, the right answer is observer-dependent, since some onlooker transactions may see those guys as committed while others think they are not yet committed. So there could certainly not be just one secret place... There are solutions suggested in the archives, but they all amount to making COUNT(*)-with-no-WHERE-or-GROUP-BY-clause fast at the price of nontrivial distributed overhead for all updates --- overhead that would be paid whether or not the application ever did such a COUNT. That's not a tradeoff we've wanted to make in general. You can implement it yourself via triggers for specific tables that you think it's worth doing for. Also, if an approximate answer is good enough, there are a whole other set of possible solutions. regards, tom lane