Re: Slow count(*)

From: Andrew Sullivan <ajs(at)crankycanuck(dot)ca>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Slow count(*)
Date: 2008-01-02 16:25:39
Message-ID: 20080102162539.GU22706@crankycanuck.ca
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

On Wed, Jan 02, 2008 at 09:29:24AM -0600, Abraham, Danny wrote:
> We are looking for a patch that will help us count using the indexes.

Is this for

SELECT count(*) FROM table;

or

SELECT count(1) FROM table WHERE. . .

The latter _will_ use an index, if the index is correct, the statistics are
right, and the index selectivity is worth the cost of reading the index.
The former will not use an index at all, because the answer depends on
visibility, and you can't know that without reading the table. If you're
counting how many rows are in the table (for, for instance, display
purposes), you probably need to do something else.

> Our product is about 20 times slower on Postgres compared to MS SQL
> Server.
>
> Any ideas?

Not without the queries, the EXPLAIN ANALYZE plans, and some information
about the database.

A

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Glyn Astill 2008-01-02 16:32:39 Table auditing / Pg/tcl help
Previous Message A. Kretschmer 2008-01-02 15:50:52 Re: Slow count(*)

Browse pgsql-hackers by date

  From Date Subject
Next Message Andrew Sullivan 2008-01-02 16:31:55 Re: Index performance
Previous Message Brian Modra 2008-01-02 15:53:35 Index performance