Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 9B8FB9FA1A7 for ; Mon, 30 Oct 2006 07:56:27 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 89123-02 for ; Mon, 30 Oct 2006 07:56:24 -0400 (AST) X-Greylist: delayed 00:35:36.50536 by SQLgrey- Received: from mxfep04.bredband.com (mxfep04.bredband.com [195.54.107.79]) by postgresql.org (Postfix) with ESMTP id 7CF589FA4D9 for ; Mon, 30 Oct 2006 07:56:23 -0400 (AST) Received: from ironport.bredband.com ([195.54.107.82] [195.54.107.82]) by mxfep02.bredband.com with ESMTP id <20061030110909.DKRM9734.mxfep02.bredband.com@ironport.bredband.com> for ; Mon, 30 Oct 2006 12:09:09 +0100 Received: from c-a009e155.242-5-64736c11.cust.bredbandsbolaget.se (HELO server.kregert.se) ([85.225.9.160]) by ironport.bredband.com with ESMTP/TLS/AES256-SHA; 30 Oct 2006 12:09:09 +0100 Received: from server.kregert.se (server.kregert.se [10.10.0.100]) by server.kregert.se (8.13.6/8.13.4) with ESMTP id k9UB58C9001416 for ; Mon, 30 Oct 2006 12:05:08 +0100 From: Mattias Kregert Organization: Leif Kregert =?iso-8859-1?q?=C5keri?= AB To: pgsql-performance@postgresql.org Subject: Strange plan in pg 8.1.0 Date: Mon, 30 Oct 2006 13:05:07 +0200 User-Agent: KMail/1.8.2 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200610301205.08087.mattias@kregert.se> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.45 tagged_above=0 required=5 tests=BAYES_50, FORGED_RCVD_HELO, SARE_MILLIONSOF, SPF_PASS X-Spam-Level: X-Archive-Number: 200610/607 X-Sequence-Number: 21736 Look at this insane plan: lucas=# explain analyse select huvudklass,sum(summa) from kor_tjanster left outer join prislist on prislista=listid and tjanst=tjanstid where kor_id in (select id from kor where lista=10484) group by 1; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------- HashAggregate (cost=212892.07..212892.10 rows=2 width=23) (actual time=4056.165..4056.167 rows=2 loops=1) -> Hash IN Join (cost=102.84..212889.04 rows=607 width=23) (actual time=4032.931..4056.017 rows=31 loops=1) Hash Cond: ("outer".kor_id = "inner".id) -> Hash Left Join (cost=59.66..206763.11 rows=1215336 width=27) (actual time=4.959..3228.550 rows=1216434 loops=1) Hash Cond: (("outer".prislista = ("inner".listid)::text) AND ("outer".tjanst = ("inner".tjanstid)::text)) -> Seq Scan on kor_tjanster (cost=0.00..23802.36 rows=1215336 width=26) (actual time=0.032..1257.241 rows=1216434 loops=1) -> Hash (cost=51.77..51.77 rows=1577 width=29) (actual time=4.898..4.898 rows=1577 loops=1) -> Seq Scan on prislist (cost=0.00..51.77 rows=1577 width=29) (actual time=0.034..2.445 rows=1577 loops=1) -> Hash (cost=41.79..41.79 rows=557 width=4) (actual time=0.185..0.185 rows=29 loops=1) -> Index Scan using kor_lista on kor (cost=0.00..41.79 rows=557 width=4) (actual time=0.070..0.150 rows=29 loops=1) Index Cond: (lista = 10484) Total runtime: 4056.333 ms I have an index on kor_tjanster(kor_id), an index on prislist(prislist_id), did ANALYZE and all that stuff... but those indexes are not used. Why does it come up with this strange plan? It does a seqscan of 1.2 million rows and then a join!? Using the index would be much faster... I expected something like this: 1. Index Scan using kor_lista on kor (use lista_id 10484 to get a list of kor_id's - 29 rows (expected 557 rows)) 2. Index Scan using kor_id on kor_tjanster (use the kor_id's to get a list of kor_tjanster - 31 rows) 3. Index Scan using prislist_listid on prislist (use the 31 kor_tjanster rows to find the corresponding 'huvudklass' for each row) 29+31+31=91 index lookups... which is MUCH faster than seq-scanning millions of rows... I need to speed up this query. How can i make it use the correct index? Any hints? I have pg 8.1.0, default settings. /* m */