Re: [HACKERS] Much Ado About COUNT(*)

From: Greg Stark <gsstark(at)mit(dot)edu>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: [HACKERS] Much Ado About COUNT(*)
Date: 2005-01-13 13:59:31
Message-ID: 87d5w9xwe4.fsf@stark.xeocode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

David Garamond <lists(at)zara(dot)6(dot)isreserved(dot)com> writes:

> Merlin Moncure wrote:
> > 6. for large tables, you can get a pretty accurate count by doing:
> > select count(*) * 10 from t where random() > .9;
> > on my setup, this shaved about 15% off of the counting time...YMMV.
>
> That's an interesting idea, using sampling to get an estimate.

It's an interesting idea but this particular implementation isn't going to
save any time. It still has to read every record only now it has to spend
extra time doing a random() and the arithmetic.

In order for sampling to speed things up you would have to use an index to
actually reduce the number of records read.

The database could be clever and implement the same kind of sampling vacuum
does. That picks a random sampling of pages from the table without using an
index. But there's no way to implement the same kind of behaviour from the
user-visible features.

--
greg

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Dave Smith 2005-01-13 14:01:08 Adding UNIQUE constraint on NULL column
Previous Message Valentin Militaru 2005-01-13 13:03:59 Problems with a trigger

Browse pgsql-hackers by date

  From Date Subject
Next Message Bruce Momjian 2005-01-13 14:04:46 Re: Much Ado About COUNT(*)
Previous Message Kevin Brown 2005-01-13 13:03:54 Re: [pgsql-hackers-win32] [BUGS] More SSL questions..