Re: Faster 'select count(*) from table' ?

From: "scott(dot)marlowe" <scott(dot)marlowe(at)ihs(dot)com>
To: Matt Mello <alien(at)spaceship(dot)com>
Cc: <pgsql-performance(at)postgresql(dot)org>
Subject: Re: Faster 'select count(*) from table' ?
Date: 2003-02-25 23:25:26
Message-ID: Pine.LNX.4.33.0302251619480.16809-100000@css120.ihs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

On Tue, 25 Feb 2003, Matt Mello wrote:

> Does anyone know if there is a fast way to find out how many records are
> in a table?
>
> "Select count(*) from table" is very slow.
>
> I would think that PG would keep up with the number of undeleted rows on
> a realtime basis. Is that the case? If so, how would I query it?

Sorry, it doesn't, and it's one of the areas that having an MVCC style
database costs you. Also, if postgresql kept up with this automatically,
it would have an overhead for each table, but how often do you use it on
ALL your tables? Most the time, folks use count(*) on a few tables only,
and it would be a waste to have a seperate counting mechanism for all
tables when you'd only need it for a few.

The general mailing list has several postings in the last 12 months about
how to setup a trigger to a single row table that keeps the current
count(*) of the master table.

If you need a rough count, you can get one from the statistics gathered by
analyze in the pg_* tables.

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Anuradha Ratnaweera 2003-02-26 04:57:28 Re: Superfluous merge/sort
Previous Message Matt Mello 2003-02-25 22:44:13 Faster 'select count(*) from table' ?