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

Re: Performance of count(*)

From: Michael Stone <mstone+postgres(at)mathom(dot)us>
To: pgsql-performance(at)postgresql(dot)org
Subject: Re: Performance of count(*)
Date: 2007-03-22 14:41:51
Message-ID: 20070322144149.GF11402@mathom.us (view raw or flat)
Thread:
Lists: pgsql-performance
On Thu, Mar 22, 2007 at 09:39:18AM -0400, Merlin Moncure wrote:
>You can get the approximate count by selecting reltuples from
>pg_class.  It is valid as of last analyze.

Of course, that only works if you're not using any WHERE clause. 
Here's a (somewhat ugly) example of getting an approximate count based 
off the statistics info, which will work for more complicated queries:
http://archives.postgresql.org/pgsql-sql/2005-08/msg00046.php
The ugliness is that you have to provide the whole query as a 
parameter to the function, instead of using it as a drop-in replacement 
for count. I assume that the TODO item is to provide the latter, but for 
now this method can be useful for UI type stuff where you just want to 
know whether there's "a little" or "a lot".

Mike Stone

In response to

pgsql-performance by date

Next:From: Luke LonerganDate: 2007-03-22 14:46:26
Subject: Re: Performance of count(*)
Previous:From: Michael FuhrDate: 2007-03-22 14:37:13
Subject: Re: Performance of count(*)

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