Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 238DC2E02A5 for ; Mon, 10 Mar 2008 19:28:46 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 66470-03 for ; Mon, 10 Mar 2008 19:28:37 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from wa-out-1112.google.com (wa-out-1112.google.com [209.85.146.177]) by postgresql.org (Postfix) with ESMTP id 762B72E01C7 for ; Mon, 10 Mar 2008 19:28:42 -0300 (ADT) Received: by wa-out-1112.google.com with SMTP id m38so2541695waf.5 for ; Mon, 10 Mar 2008 15:28:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; bh=KSOKTuGeDqIRvRktsFIL2D+SQs/ip4Wyq54Ab1ICRNI=; b=nTgi2NADNga0KmeIp3sJRpCoJ+ExVphPr1wAdX9Sk9o6sVj8pHtgB+OrtxEVeY8OmCkM6VDi0rCtOnu356qSj1xd1PzGCYq1R2D2n5U++t5aRydatNFde7tg5Xj8cc5iD6pjegCvK98SPyIVKEA6H2oCH134DgPzjjNrGq5POQY= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=pXbGhsXhNt5oV/jCe3VcL7rePZSdUMwgKT1mjzAXdtATr/yw1s3R0y+DzmN1aJEfNHTUfQgXk64BNaQHw/aE9FCLjVs+bOOKF2ZsDsEVibPR7d7CHzM0uFZwgsScTjXZrwVd/fkfHDG9eo0w3dE3fnor8cyuATDfze518cVjgkA= Received: by 10.114.13.1 with SMTP id 1mr3915170wam.60.1205187702430; Mon, 10 Mar 2008 15:21:42 -0700 (PDT) Received: by 10.114.133.6 with HTTP; Mon, 10 Mar 2008 15:21:42 -0700 (PDT) Message-ID: Date: Mon, 10 Mar 2008 15:21:42 -0700 From: "Scott Marlowe" To: "Joe Mirabal" Subject: Re: count * performance issue Cc: "Gregory Stark" , "Tom Lane" , josh@agliodbs.com, pgsql-performance@postgresql.org, "Craig James" In-Reply-To: <12ba3bf00803101354w15b023e1o237f564a1b01105f@mail.gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <47D00DB2.7040007@emolecules.com> <18451.1204824562@sss.pgh.pa.us> <200803071727.36146.josh@agliodbs.com> <13345.1204940295@sss.pgh.pa.us> <87r6eia9tz.fsf@oxford.xeocode.com> <12ba3bf00803101354w15b023e1o237f564a1b01105f@mail.gmail.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200803/210 X-Sequence-Number: 29164 On Mon, Mar 10, 2008 at 1:54 PM, Joe Mirabal wrote: > Gregory, > > I just joined this listserv and was happy to see this posting. I have a > 400GB table that I have indexed (building the index took 27 hours) , Loading > the table with 10 threads took 9 hours. I run queries on the data nad get > immediate max and min as well as other aggrgate functions very quickly, > however a select count(*) of the table takes forever usually nearly an hour > or more. > > Do you have any tuning recommendations. We in our warehouse use the > count(*) as our verification of counts by day/month's etc and in Netezza its > immediate. I tried by adding oids. BUT the situation I learned was that > adding the oids in the table adds a significasnt amount of space to the data > AND the index. Yeah, this is a typical problem people run into with MVCC databases to one extent or another. PostgreSQL has no native way to just make it faster. However, if it's a table with wide rows, you can use a lookup table to help a bit. Have a FK with cascading deletes from the master table to a table that just holds the PK for it, and do count(*) on that table. Otherwise, you have the trigger solution mentioned previously. Also, if you only need an approximate count, then you can use the system tables to get that with something like select reltuples from pg_class where relname='tablename'; after an analyze. It won't be 100% accurate, but it will be pretty close most the time.