Re: Yet Another (Simple) Case of Index not used - Mailing list pgsql-general

From Brent Wood
Subject Re: Yet Another (Simple) Case of Index not used
Date
Msg-id 20030409123856.Q34167-100000@storm.niwa.co.nz
Whole thread Raw
In response to Re: Yet Another (Simple) Case of Index not used  (Martijn van Oosterhout <kleptog@svana.org>)
List pgsql-general

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


pgsql-general by date:

Previous
From: "Ted Taylor"
Date:
Subject: Re: Live Link to Oracle?
Next
From: Peter Nixon
Date:
Subject: Delete large amount of records and INSERT (with indexes) goes VERY slow