X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 27FB0D8A2C for ; Thu, 8 Sep 2005 12:40:58 -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 28757-07 for ; Thu, 8 Sep 2005 15:40:53 +0000 (GMT) Received: from yass.opencloud.co.nz (yass.opencloud.co.nz [203.79.85.162]) by svr1.postgresql.org (Postfix) with ESMTP id 0827ED8A17 for ; Thu, 8 Sep 2005 12:40:47 -0300 (ADT) Received: from 203-79-104-60.cable.paradise.net.nz ([203.79.104.60] helo=[192.168.0.20]) by yass.opencloud.co.nz with asmtp (TLS-1.0:DHE_RSA_AES_128_CBC_SHA:16) (Exim 4.34) id 1EDOW6-0005Pt-U8; Fri, 09 Sep 2005 03:40:47 +1200 Message-ID: <43205B7E.3080505@opencloud.com> Date: Fri, 09 Sep 2005 03:40:46 +1200 From: Oliver Jowett User-Agent: Mozilla Thunderbird 1.0.2 (X11/20050324) X-Accept-Language: en-us, en MIME-Version: 1.0 To: David Wall Cc: pgsql-jdbc@postgresql.org Subject: Re: SELECT COUNT(*) does a scan? References: <43205750.5070806@computer.org> In-Reply-To: <43205750.5070806@computer.org> X-Enigmail-Version: 0.90.2.0 X-Enigmail-Supports: pgp-inline, pgp-mime Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.008 required=5 tests=[AWL=0.008] X-Spam-Level: X-Archive-Number: 200509/24 X-Sequence-Number: 13809 David Wall wrote: > When I do an EXPLAIN SELECT COUNT(*) FROM tablename, I noted that it > does a table scan. I thought PG had some sort of table stat that kept > track of the current number of rows in a table, but that doesn't appear > to always be the case. It's not the case, and this is a FAQ -- search archives.postgresql.org for more details (the short version is that maintaining a row count doesn't work well with MVCC). > It seems that right after a VACUUM ANALYZE, that command is very fast (on a table with 100,000+ rows), but it can also get quite slow, as if a table scan is taking place. > Does this make sense? Is there an algorithm that says to use the stats from analyze only until sufficient updates/inserts/deletes have taken place to make them "out of date"? Most likely a VACUUM ANALYZE is just pulling the whole table into cache, so there is less disk I/O needed to do the scan. -O