Re: EXPLAIN ANALYZE not considering primary and unique indices! - Mailing list pgsql-general

From Tom Lane
Subject Re: EXPLAIN ANALYZE not considering primary and unique indices!
Date
Msg-id 9739.1188254309@sss.pgh.pa.us
Whole thread Raw
In response to Re: EXPLAIN ANALYZE not considering primary and unique indices!  (Bill Moran <wmoran@potentialtech.com>)
Responses Re: EXPLAIN ANALYZE not considering primary and unique indices!  (Sanjay <skpatel20@gmail.com>)
List pgsql-general
Bill Moran <wmoran@potentialtech.com> writes:
> In response to Sanjay <skpatel20@gmail.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

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: simple query runs 26 seconds
Next
From: "Scott Marlowe"
Date:
Subject: Re: Tables dissapearing