Re: Make COUNT(*) Faster? - Mailing list pgsql-sql

From Michael Fuhr
Subject Re: Make COUNT(*) Faster?
Date
Msg-id 20050708022141.GB3339@winnie.fuhr.org
Whole thread Raw
In response to Make COUNT(*) Faster?  (Varun Mehta <vmehta@apple.com>)
List pgsql-sql
On Thu, Jul 07, 2005 at 03:48:39PM -0700, Varun Mehta wrote:
> 
> I've started using PostgreSQL pretty recently, and I am quite  
> disturbed about the performance of a simple SELECT COUNT(*) FROM  
> table.  What should (in my mind) be a nearly instantaneous operation  
> instead takes nearly 700ms in a table with only 87k rows of data!

Speeding up COUNT is on the developers' TODO list, but it's not as
simple as it might seem or it would have been done already.  This
has been brought up many times over the years -- search the archives
to see past discussion.  Words to search for include "MVCC," "index,"
and "visibility."

> If I run an EXPLAIN on this query I can see that it is doing a  
> sequential scan, which seems quite needless, as surely this  
> information is cached in some secret location.

If an estimate will suffice then you could use the table's
pg_class.reltuples value, but beware that it can be rather
out of date.

http://www.postgresql.org/docs/8.0/static/catalog-pg-class.html

-- 
Michael Fuhr
http://www.fuhr.org/~mfuhr/


pgsql-sql by date:

Previous
From: Michael Fuhr
Date:
Subject: Re: getting back autonumber just inserted
Next
From: PFC
Date:
Subject: Re: getting back autonumber just inserted