Re: performance of count(*) - Mailing list pgsql-general

From Andrew Sullivan
Subject Re: performance of count(*)
Date
Msg-id 20110506195902.GK29489@shinkuro.com
Whole thread Raw
In response to performance of count(*)  (Scott Ribe <scott_ribe@elevated-dev.com>)
List 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@crankycanuck.ca

pgsql-general by date:

Previous
From: Tomas Vondra
Date:
Subject: Re: performance of count(*)
Next
From: Tony Capobianco
Date:
Subject: pgloader hangs with an invalid filename