Re: [ADMIN] Index not used. WHY?

From: Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com>
To: Andrei Bintintan <klodoma(at)ar-sd(dot)net>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: [ADMIN] Index not used. WHY?
Date: 2003-12-05 15:34:45
Message-ID: 20031205072359.X5253@megazone.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin pgsql-performance

On Fri, 5 Dec 2003, Andrei Bintintan wrote:

> There are around 700 rows in this table.
> If I set enable_seqscan=off then the index is used and I also used Vacuum
> Analyze recently.
>
> I find it strange because the number of values of id_user and id_modull are
> somehow in the same distribution and when I search the table the id_user
> index is used but the id_modull index is not used.

It was guessing that one would return 11 rows and the other 42 which is
why one used the index and the other wouldn't. If those numbers aren't
realistic, you may want to raise the statistics target for the columns
(see ALTER TABLE) and re-run analyze.

> Does somehow postgre know that a seq scan runs faster in this case as a
> index scan? Should I erase this index?

It's making an educated guess. When you're doing an index scan, it needs
to read through the index and then get matching rows from the table.
However, because those reads from the table are in a potentially random
order, there's usually a higher cost associated with those reads than if
the table was read in order (barring cases where you know your database
should always stay cached in disk cache, etc...). If there's say 50 pages
in the entire table, a sequence scan does 50 sequential page reads and is
checking all those tuples. If you're getting say 42 rows through an
index, you're first reading through the index, and then getting <n> pages
in a random order from the table where <n> depends on the distribution of
values throughout the table. There's a variable in the configuration,
random_page_cost which controls the ratio of cost between a sequential
read and a random one (defaulting to 4).

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Peter Eisentraut 2003-12-05 18:51:40 Re: Trigger to call an external program
Previous Message Kris Kiger 2003-12-05 15:02:26 Mammoth PostgreSQL Replicator

Browse pgsql-performance by date

  From Date Subject
Next Message Ivar Zarans 2003-12-05 16:47:43 Re: Slow UPADTE, compared to INSERT
Previous Message Shridhar Daithankar 2003-12-05 13:51:38 Re: Slow UPADTE, compared to INSERT