Skip site navigation (1) Skip section navigation (2)

Re: Much Ado About COUNT(*)

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: "Jonah H(dot) Harris" <jharris(at)tvi(dot)edu>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Much Ado About COUNT(*)
Date: 2005-01-12 21:56:25
Message-ID: 20050112215625.GA5051@wolff.to (view raw or flat)
Thread:
Lists: pgsql-announcepgsql-hackerspgsql-patches
On Wed, Jan 12, 2005 at 14:09:07 -0700,
  "Jonah H. Harris" <jharris(at)tvi(dot)edu> wrote:

Please keep stuff posted to the list so that other people can contribute
and learn from the discussion unless there is a particular reason to
limited who is involved in the discussion.

> Bruno,
> 
> Thanks for the information.  I was told that PostgreSQL couldn't use 
> index scans for count(*) because of the visibility issue.  Has something 
> changed or was I told incorrectly?

It isn't that it can't, it is that for cases where you are counting more
than a few percent of a table, it will be faster to use a sequential
scan. Part of the reason is that for any hits you get in the index, you
have to check in the table to make sure the current transaction can see
the current tuple. Even if you could just get away with using just an
index scan you are only going to see a constant factor speed up with
probably not too big of a constant.

Perhaps you think that the count is somehow saved in the index so that
you don't have to scan through the whole index to get the number of
rows in a table? That isn't the case, but is what creating a materialized
view would effectively do for you.

In response to

Responses

pgsql-announce by date

Next:From: Jonah H. HarrisDate: 2005-01-12 21:58:14
Subject: Re: Much Ado About COUNT(*)
Previous:From: Jonah H. HarrisDate: 2005-01-12 21:48:33
Subject: Re: Much Ado About COUNT(*)

pgsql-hackers by date

Next:From: Jonah H. HarrisDate: 2005-01-12 21:58:14
Subject: Re: Much Ado About COUNT(*)
Previous:From: Jonah H. HarrisDate: 2005-01-12 21:48:33
Subject: Re: Much Ado About COUNT(*)

pgsql-patches by date

Next:From: Jonah H. HarrisDate: 2005-01-12 21:58:14
Subject: Re: Much Ado About COUNT(*)
Previous:From: Jonah H. HarrisDate: 2005-01-12 21:48:33
Subject: Re: Much Ado About COUNT(*)

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group