Re: Why count(*) doest use index?

From: Raymond O'Donnell <rod(at)iol(dot)ie>
To: obamabarak(at)e1(dot)ru
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Why count(*) doest use index?
Date: 2011-03-05 17:02:35
Message-ID: 4D726CAB.8040503@iol.ie
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 03/03/2011 13:29, 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.

Because when you do SELECT COUNT(*) without any WHERE.... clause, then
PostgreSQL has to scan through *all* the rows in the table in order to
count them.

Ray.

--
Raymond O'Donnell :: Galway :: Ireland
rod(at)iol(dot)ie

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Andre Lopes 2011-03-05 19:22:32 PHP array to PlPgSQL arrat. How to?
Previous Message Martijn van Oosterhout 2011-03-05 15:55:59 Re: updating all records of a table