Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 7E3419FA4F4 for ; Wed, 18 Oct 2006 08:40:03 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 73121-03 for ; Wed, 18 Oct 2006 08:39:58 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id EC5C19FA240 for ; Wed, 18 Oct 2006 08:39:57 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.3 Received: from edb06.mail01.enterprisedb.com (63-246-7-171.contegix.com [63.246.7.171]) by svr4.postgresql.org (Postfix) with ESMTP id 155415AF03A for ; Wed, 18 Oct 2006 11:39:57 +0000 (GMT) thread-index: AcbyqiFlHKBmfBsOQ4mXSBl0svB0VQ== Received: from [192.168.1.34] ([82.27.252.198]) by edb06.mail01.enterprisedb.com over TLS secured channel with Microsoft SMTPSVC(6.0.3790.1830); Wed, 18 Oct 2006 07:39:52 -0400 Message-ID: <45361295.9050504@enterprisedb.com> Content-Class: urn:content-classes:message Importance: normal X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.2757 Date: Wed, 18 Oct 2006 12:40:05 +0100 From: "Heikki Linnakangas" User-Agent: Thunderbird 1.5.0.7 (X11/20060915) MIME-Version: 1.0 To: "Arnaud Lesauvage" Cc: Subject: Re: Index on two columns not used References: <45360E25.9010403@freesurf.fr> In-Reply-To: <45360E25.9010403@freesurf.fr> Content-Type: text/plain; format=flowed; charset="ISO-8859-1" Content-Transfer-Encoding: 7bit X-OriginalArrivalTime: 18 Oct 2006 11:39:53.0078 (UTC) FILETIME=[2087F160:01C6F2AA] X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.135 tagged_above=0 required=5 tests=FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200610/346 X-Sequence-Number: 21475 Arnaud Lesauvage wrote: > 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; My theory: There's no additional restrictions besides the join condition, so the system has to scan both tables completely. It chooses to use a full index scan instead of a seq scan to be able to do a merge join. Because it's going to have to scan the indexes completely anyway, it chooses the smallest index which is subcities_pkey. You'd think that the system could do the merge using just the indexes, and only fetch the heap tuples for matches. If that were the case, using the 2-column index would indeed be a good idea. However, PostgreSQL can't use the values stored in the index to check the join condition, so all the heap tuples are fetched anyway. There was just recently discussion about this on this list: http://archives.postgresql.org/pgsql-performance/2006-09/msg00080.php. -- Heikki Linnakangas EnterpriseDB http://www.enterprisedb.com