Help understanding how indexes are used by the query optimizer

From: "Peter Bojanic" <pbojanic(at)pictorius(dot)com>
To: <pgsql-general(at)postgresql(dot)org>
Subject: Help understanding how indexes are used by the query optimizer
Date: 2000-01-26 20:30:29
Message-ID: NDBBKMBJGKMBHLNCGCAHOEOKCEAA.pbojanic@pictorius.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,

We're having difficulty understanding why PostgreSQL (6.5.3 on Red Hat Linux
6.0) won't use a primary key index.

Here's the SQL statement:

select users.userid, phone.phone, phone.phonetype, phone.phonenumber
from users, phone
where
users.person = personphone.person AND
personphone.phone = phone.phone

Here's the phone table:

+----------------------------------+----------------------------------+-----
--+
| Field | Type |
Length|
+----------------------------------+----------------------------------+-----
--+
| phone | int4 not null |
4 |
| phonetype | int4 not null |
4 |
| phonenumber | varchar() not null |
35 |
| lastuser | varchar() |
30 |
| lastmodified | datetime |
8 |
| lastaction | varchar() |
30 |
+----------------------------------+----------------------------------+-----
--+

Here's the index for PHONE:

+----------------------------------+----------------------------------+-----
--+
| Field | Type |
Length|
+----------------------------------+----------------------------------+-----
--+
| phone | int4 |
4 |
+----------------------------------+----------------------------------+-----
--+

Here's the output from EXPLAIN

Hash Join (cost=1247.17 rows=14779 width=36)
-> Seq Scan on phone (cost=668.41 rows=14770 width=20)
-> Hash (cost=23.58 rows=1811 width=16)
-> Nested Loop (cost=23.58 rows=1811 width=16)
-> Seq Scan on users (cost=1.36 rows=11 width=8)
-> Index Scan using personphone_fkey on personphone
(cost=2.02 rows=1810 width=8)

The question is, why will PostgreSQL not use the primary key index that is
defined for the PHONE table. It appears from this output that it is doing a
full-table sequential scan of 14,770 records.

Browse pgsql-general by date

  From Date Subject
Next Message dw_remote 2000-01-26 20:31:28 ODBC Connection problem
Previous Message Bruce Bantos 2000-01-26 19:29:57 Backup, Vacuume scheduling tips?