Re: Why is it not using an index?

From: "Gregory Wood" <gregw(at)com-stock(dot)com>
To: "Dmitry Tkach" <dmitry(at)openratings(dot)com>
Cc: "PostgreSQL-General" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Why is it not using an index?
Date: 2002-03-15 19:20:11
Message-ID: 001401c1cc56$6db77d40$7889ffcc@comstock.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-sql

> Am I missing something here again, or will it just not use an index for
aggregation?

PostgreSQL does not use an index to perform a full table count. I'm not sure
of the exact reasoning behind this, but I think there are multiple issues
with the approach.

> I mean, especially an this case, it looks so weird that it KNOWS the
answer to my query RIGHT AWAY (rows=... in the explain response), yet it
takes it so long to return it...

Actually, that rows= count is the *estimate* for the number of rows. That
estimate is calculated from a variety of statistics compiled when the
ANALYZE command is performed. Those statistics may or may not be up to date,
and are only used to plan the query's execution.

Greg

----- Original Message -----
From: Dmitry Tkach
To: Gregory Wood
Cc: PostgreSQL-General
Sent: Friday, March 15, 2002 3:01 PM
Subject: Re: [GENERAL] Why is it not using an index?

Gregory Wood wrote:

explain select * from a where x=3;
PostgreSQL is treating 3 as an int4 (integer) type, whereas x is an
int2(smallint) type. Try casting the constant as a smallint and it should
usethe index:explain select * from a where x=3::smallint;
Aha! Great! Thanks a lot! That worked!
Now, the next problem:

explain select count (x) from a ;

Aggregate (cost=100175934.05..100175934.05 rows=1 width=2)
-> Seq Scan on a (cost=100000000.00..100150659.04 rows=10110004 width=2)

Am I missing something here again, or will it just not use an index for
aggregation?

I mean, especially an this case, it looks so weird that it KNOWS the answer
to my query RIGHT AWAY (rows=... in the explain response), yet it takes it
so long to return it...

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Dmitry Tkach 2002-03-15 20:01:17 Re: Why is it not using an index?
Previous Message Dmitry Tkach 2002-03-15 19:09:21 Re: Why is it not using an index? (small correction)

Browse pgsql-sql by date

  From Date Subject
Next Message Dmitry Tkach 2002-03-15 19:31:44 Re: Btree index extension question
Previous Message Dmitry Tkach 2002-03-15 19:09:21 Re: Why is it not using an index? (small correction)