From: Brent Wood <b.wood@niwa.co.nz>
To: pgsql-general@postgresql.org
Cc: Denis <denis@next2me.com>
Subject: Re: Yet Another (Simple) Case of Index not used
Date: Wed, 9 Apr 2003 12:44:48 +1200 (NZST)
Message-ID: <20030409123856.Q34167-100000@storm.niwa.co.nz> (raw)
In-Reply-To: <20030408234623.GD15928@svana.org>
On Wed, 9 Apr 2003, Martijn van Oosterhout wrote:
> On Tue, Apr 08, 2003 at 12:57:16PM -0700, Denis wrote:
> > The query I am trying to do (fast) is:
> >
> > select count(*) from addresses;
> >
> > This takes more than a second to complete, because, as the 'explain' command
> > shows me,
> > the index created on 'addresses' is not used, and a seq scan is being used.
> > One would assume that the creation of an index would allow the counting of
> > the number of entries in a table to be instantanous?
>
> Incorrect assumption. select count(*) can produce different results in
> different backends depending on the current state of the active
> transactions.
Some thoughts:
Select count(*) is often applied to views, and may take some time
depending on the underlying query.
However, for a single table, I would have thought that if there are no
write locks or open transactions for the table, the index would return a
faster result than a scan? Is there room for some optimisation here?
Does count(<primary_key>) work faster, poss using the unique index on the
key (for non-composite keys)?
Cheers
Brent Wood
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-sql@postgresql.org
Cc: b.wood@niwa.co.nz, pgsql-general@postgresql.org, denis@next2me.com
Subject: Re: Yet Another (Simple) Case of Index not used
In-Reply-To: <20030409123856.Q34167-100000@storm.niwa.co.nz>
References: <20030408234623.GD15928@svana.org> <20030409123856.Q34167-100000@storm.niwa.co.nz>
* 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