Re: EXPLAIN ANALYZE not considering primary and unique indices!

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Bill Moran <wmoran(at)potentialtech(dot)com>
Cc: Sanjay <skpatel20(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: EXPLAIN ANALYZE not considering primary and unique indices!
Date: 2007-08-27 22:38:29
Message-ID: 9739.1188254309@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Bill Moran <wmoran(at)potentialtech(dot)com> writes:
> In response to Sanjay <skpatel20(at)gmail(dot)com>:
>> Seq Scan on website (cost=0.00..1.31 rows=1 width=162) (actual time=0.047..0.051 rows=1 loops=1)
>> Filter: (website_id = 1)
>> Total runtime: 0.102 ms

>> Wondering why it is not using the index, which would have been
>> automatically created for the primary key.

> Because PG thinks the seq scan is faster than an index scan.

The cost estimate shows there is only one page in the table (assuming
seq_page_cost has its default value of 1.0). You're basically never
going to get an indexscan plan for a one-page table: it takes one read
to fetch the page, and any reads done to fetch index pages are going
to be more expensive than just examining the tuples, unless you have a
*whole* lot of tuples in the one page.

Load the table up with a realistic amount of data, and ANALYZE it, and
then see what plan you get.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2007-08-27 22:41:01 Re: psql \copy command runs as a transcation?
Previous Message mrodriguez 2007-08-27 22:35:39 how to print a schema