Index on two columns not used

From: Arnaud Lesauvage <thewild(at)freesurf(dot)fr>
To: pgsql-performance(at)postgresql(dot)org
Subject: Index on two columns not used
Date: 2006-10-18 11:21:09
Message-ID: 45360E25.9010403@freesurf.fr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Hi list !

I have two table with a 2-column index on both of them.
In the first table, the first colum of the index is the primary key,
the second one is an integer field.
In the second table, the two columns are the primary key.
When I join these two tables, the 2-column index of the first table is
not used.
Why does the query planner think that this plan is better ?

ALTER TABLE geo.subcities_names
ADD CONSTRAINT subcities_names_pkey PRIMARY KEY(subcity_gid,
language_id);

CREATE INDEX subcities_gid_language_id
ON geo.subcities
USING btree
(gid, official_language_id);

EXPLAIN ANALYZE
SELECT * FROM geo.subcities sc, geo.subcities_names scn
WHERE sc.gid = scn.subcity_gid AND sc.official_language_id =
scn.language_id;

Result :

Merge Join (cost=0.00..4867.91 rows=37917 width=240) (actual
time=0.037..149.022 rows=39323 loops=1)
Merge Cond: ("outer".gid = "inner".subcity_gid)
Join Filter: ("outer".official_language_id = "inner".language_id)
-> Index Scan using subcities_pkey on subcities sc
(cost=0.00..1893.19 rows=39357 width=200) (actual time=0.015..43.430
rows=39357 loops=1)
-> Index Scan using subcities_names_pkey on subcities_names scn
(cost=0.00..2269.39 rows=40517 width=40) (actual time=0.012..35.465
rows=40517 loops=1)
Total runtime: 157.389 ms
(6 rows)

Thanks for your suggestions !
Regards
--
Arnaud

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Heikki Linnakangas 2006-10-18 11:40:05 Re: Index on two columns not used
Previous Message Mark Kirkwood 2006-10-18 10:19:44 Re: Optimization of this SQL sentence