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 1BF8452924 for ; Wed, 10 Aug 2005 11:15:37 -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 92153-08 for ; Wed, 10 Aug 2005 14:15:34 +0000 (GMT) Received: from mail.metronet.co.uk (mail.metronet.co.uk [213.162.97.75]) by svr1.postgresql.org (Postfix) with ESMTP id 238005295A for ; Wed, 10 Aug 2005 11:15:32 -0300 (ADT) Received: from mainbox.archonet.com (84-51-143-99.archon037.adsl.metronet.co.uk [84.51.143.99]) by smtp.metronet.co.uk (MetroNet Mail) with ESMTP id 5265F4144AD; Wed, 10 Aug 2005 15:15:26 +0100 (BST) Received: from localhost (localhost.localdomain [127.0.0.1]) by mainbox.archonet.com (Postfix) with ESMTP id 7BEA815EDA; Wed, 10 Aug 2005 15:04:52 +0100 (BST) Received: from mainbox.archonet.com ([127.0.0.1]) by localhost (mainbox [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 16103-07; Wed, 10 Aug 2005 15:04:49 +0100 (BST) Received: from [192.168.1.17] (client17.office.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id 4297C15ED9; Wed, 10 Aug 2005 15:04:49 +0100 (BST) Message-ID: <42FA0980.1060104@archonet.com> Date: Wed, 10 Aug 2005 15:04:48 +0100 From: Richard Huxton User-Agent: Mozilla Thunderbird 1.0.2 (X11/20050317) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Owen Jacobson Cc: pgsql-sql@postgresql.org Subject: Re: Faster count(*)? References: <000101c59d33$36a4a560$9b00015a@osl.com> In-Reply-To: <000101c59d33$36a4a560$9b00015a@osl.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.057 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200508/48 X-Sequence-Number: 22276 Owen Jacobson wrote: > Salve. > > I understand from various web searches and so on that PostgreSQL's MVCC > mechanism makes it very hard to use indices or table metadata to optimise > count(*). Is there a better way to guess the "approximate size" of a table? Plenty of good answers on how to estimate table-size, but it sounds like you just want to run your maintenance function "every so often". 1. Create a sequence "my_table_tracker_seq" 2. On insert, call nextval(my_table_tracker_seq) 3. If value modulo 1000 = 0, run the maintenance routine That's about as fast as you can get, and might meet your needs. Of course you'll need to be more complex if you insert multiple rows at a time. If you do a lot of deletion on the table and want to take that into account, have a second sequence you increment on deletion and subtract the one from the other. Not always accurate enough, but it is quick. -- Richard Huxton Archonet Ltd