Index on two columns not used - Mailing list pgsql-performance

From Arnaud Lesauvage
Subject Index on two columns not used
Date
Msg-id 45360E25.9010403@freesurf.fr
Whole thread Raw
Responses Re: Index on two columns not used  ("Heikki Linnakangas" <heikki@enterprisedb.com>)
Re: Index on two columns not used  (Tom Lane <tgl@sss.pgh.pa.us>)
List 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

pgsql-performance by date:

Previous
From: Mark Kirkwood
Date:
Subject: Re: Optimization of this SQL sentence
Next
From: "Heikki Linnakangas"
Date:
Subject: Re: Index on two columns not used