pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Oliver Jowett <oliver@opencloud.com>
To: David Wall <d.wall@computer.org>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: SELECT COUNT(*) does a scan?
Date: Fri, 09 Sep 2005 03:40:46 +1200
Message-ID: <43205B7E.3080505@opencloud.com> (raw)
In-Reply-To: <43205750.5070806@computer.org>
References: <43205750.5070806@computer.org>

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



view thread (3+ messages)  latest in thread

Message-ID: <43205B7E.3080505@opencloud.com>
Permalink:  ../43205B7E.3080505@opencloud.com/
Also on:    postgresql.org/message-id/43205B7E.3080505@opencloud.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: oliver@opencloud.com, d.wall@computer.org
  Subject: Re: SELECT COUNT(*) does a scan?
  In-Reply-To: <43205B7E.3080505@opencloud.com>
  References: <43205750.5070806@computer.org> <43205B7E.3080505@opencloud.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox