Re: performance of count(*)

From: Andrew Sullivan <ajs(at)crankycanuck(dot)ca>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: performance of count(*)
Date: 2011-05-06 19:59:02
Message-ID: 20110506195902.GK29489@shinkuro.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, May 06, 2011 at 12:45:23PM -0600, Scott Ribe wrote:

> I need to optimize queries that deal with some aggregates regarding
resource availability. My specific problem is, I think, very closely
analogous to select count(*)... where...

If the WHERE clause is fairly selective and indexed, that should be
fast. Not as fast as estimates based on trigger-written values in
another table, of course, but reasonably fast. So the first order of
business is usually to find or create indexes that will make SELECT on
the same criteria fast.

It's only unqualified "SELECT count(*)" that is slow. Generally, the
system table is good enough for that, I find. (Someone: "How long
will this take?" Me: "There are about 400 million rows to go
through." Even if you're off by 50 million at that point, it doesn't
matter.)

A

--
Andrew Sullivan
ajs(at)crankycanuck(dot)ca

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tony Capobianco 2011-05-06 20:59:41 pgloader hangs with an invalid filename
Previous Message Tomas Vondra 2011-05-06 19:39:55 Re: performance of count(*)