Re: Why count(*) doest use index?

From: Adrian Klaver <adrian(dot)klaver(at)gmail(dot)com>
To: obamabarak(at)e1(dot)ru
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Why count(*) doest use index?
Date: 2011-03-03 17:22:14
Message-ID: 4D6FCE46.2060404@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 03/03/2011 05:29 AM, obamabarak(at)e1(dot)ru wrote:
> I use pgsql 9.0.3 and I know that postgresql tries to use the fields in
> indexes instead of the original table if it possible
>
> But when I run
>
> SELECT COUNT(id) FROM tab
>
> or
>
> SELECT COUNT(*) FROM tab
>
> where there "id" is PRIMARY KEY and there are other indexes there I get
> execution plan that doesnt use any indexes, but sequentab scanning the
> original table.
>
> "Aggregate (cost=38685.98..38685.99 rows=1 width=0)"
> " -> Seq Scan on tab (cost=0.00..36372.38 rows=925438 width=0)"
>
> Why is it so?

See here:
http://wiki.postgresql.org/wiki/FAQ#Why_is_.22SELECT_count.28.2A.29_FROM_bigtable.3B.22_slow.3F

>
> ---
>
> Paul
>

--
Adrian Klaver
adrian(dot)klaver(at)gmail(dot)com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Merlin Moncure 2011-03-03 17:23:06 Re: Tracking table modifications / table stats
Previous Message Derrick Rice 2011-03-03 17:00:58 Tracking table modifications / table stats