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 9A9BC5289C for ; Wed, 10 Aug 2005 13:26:29 -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 01428-10 for ; Wed, 10 Aug 2005 16:26:24 +0000 (GMT) Received: from mail.osl.com (216-21-133-98.ip.van.radiant.net [216.21.133.98]) by svr1.postgresql.org (Postfix) with ESMTP id CCDF252889 for ; Wed, 10 Aug 2005 13:26:22 -0300 (ADT) Received: from scottpj ([90.1.0.155]) (authenticated) by mail.osl.com (8.11.6/8.11.2) with ESMTP id j7AGQQs19015 for ; Wed, 10 Aug 2005 09:26:26 -0700 From: "Owen Jacobson" To: Subject: Re: **SPAM** Faster count(*)? Date: Wed, 10 Aug 2005 09:27:23 -0700 Message-ID: <000001c59dc8$641169d0$9b00015a@osl.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook CWS, Build 9.0.2416 (9.0.2910.0) Importance: Normal In-Reply-To: <21626.1123642154@sss.pgh.pa.us> X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1506 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.05 tagged_above=0 required=5 tests=FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200508/49 X-Sequence-Number: 22277 Tom Lane wrote: > 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. Yick. Ok, given all of that, I've rewritten the trigger in question to fire on different, indexable criteria (difference between "earliest" and "latest" rows in the table). Thanks, everyone. Owen