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 9801652977 for ; Tue, 9 Aug 2005 23:49:39 -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 24549-01 for ; Wed, 10 Aug 2005 02:49:33 +0000 (GMT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id C0A5E5289B for ; Tue, 9 Aug 2005 23:49:32 -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 j7A2nEWe021627; Tue, 9 Aug 2005 22:49:15 -0400 (EDT) To: dracula007@atlas.cz Cc: pgsql-sql@postgresql.org Subject: Re: **SPAM** Faster count(*)? In-reply-to: <196456155.20050810012958@karneval.cz> References: <000101c59d33$36a4a560$9b00015a@osl.com> <196456155.20050810012958@karneval.cz> Comments: In-reply-to dracula007@atlas.cz message dated "Wed, 10 Aug 2005 01:29:58 +0200" Date: Tue, 09 Aug 2005 22:49:14 -0400 Message-ID: <21626.1123642154@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: 200508/44 X-Sequence-Number: 22272 dracula007@atlas.cz writes: > I believe running count(*) means fulltable scan, and there's no way > to do it without it. But what about some "intermediate" table, with > the necessary counts? There's a fairly complete discussion in the PG list archives of a reasonably-efficient scheme for maintaining such counts via triggers. It wasn't efficient enough that we were willing to impose the overhead on every application ... but if you really NEED a fast count(*) you could implement it. I'd like to see someone actually do it and put up working code on pgfoundry; AFAIK it's only a paper design so far. If you only want a very-approximate count, the best bet is to rely on the planner's estimates, eg regression=# explain select * from tenk1; QUERY PLAN ------------------------------------------------------------- Seq Scan on tenk1 (cost=0.00..458.00 rows=10000 width=244) ^^^^^ Current best practice is to run the explain and parse out the "rows" figure using a perl (or axe-of-choice) regexp, though we could be persuaded to supply a simpler API if there's enough demand for it. regards, tom lane