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