X-Original-To: pgsql-admin-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id BF3B2D1D16D for ; Wed, 1 Oct 2003 03:06:09 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 08548-06 for ; Wed, 1 Oct 2003 00:05:22 -0300 (ADT) Received: from www.postgresql.com (unknown [64.117.225.209]) by svr1.postgresql.org (Postfix) with ESMTP id 541C5D1DC02 for ; Tue, 30 Sep 2003 23:22:46 -0300 (ADT) Received: from svr4.postgresql.org (svr4.postgresql.org [64.117.224.192]) by www.postgresql.com (Postfix) with ESMTP id C1C34CF4A9E for ; Tue, 30 Sep 2003 15:29:44 -0300 (ADT) Received: from sss.pgh.pa.us (unknown [192.204.191.242]) by svr4.postgresql.org (Postfix) with ESMTP id 72AE21CB46C6 for ; Tue, 30 Sep 2003 15:12:42 +0000 (GMT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.10/8.12.10) with ESMTP id h8UF8u6c019518; Tue, 30 Sep 2003 11:08:56 -0400 (EDT) To: Rhaoni Chiu Pereira Cc: PostgreSQL ADMIN Subject: Re: How can I make PosgreSQL use an Index ? In-reply-to: <1063835678.3f68d81e27127@sistemica.info> References: <1063831482.3f68c7bab4b1a@sistemica.info> <5977.1063833646@sss.pgh.pa.us> <1063835678.3f68d81e27127@sistemica.info> Comments: In-reply-to Rhaoni Chiu Pereira message dated "Wed, 17 Sep 2003 18:54:38 -0300" Date: Tue, 30 Sep 2003 11:08:56 -0400 Message-ID: <19517.1064934536@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200310/1 X-Sequence-Number: 10562 Rhaoni Chiu Pereira writes: > [ very large join plan ] Sorry for not responding sooner --- somehow this got overlooked in my inbox. It looks to me like the big problem is that you have all these nested-loop joins: > -> Nested Loop (cost=100025960.94..100027780.65 rows=1 > width=818) (actual time=3476.87..99606.77 rows=19923 loops=1) > Join Filter: (("outer".emp = "inner".emp) AND ("outer".fil > = "inner".fil) AND ("outer".codigo_supervisor = "inner".codigo_supervisor) AND > ("outer".codigo_grupo_rep = "inner".codigo_grupo_rep)) > -> Nested Loop (cost=100025960.94..100027775.22 rows=1 > width=765) (actual time=3476.74..97802.69 rows=19923 loops=1) > Join Filter: (("inner".ano_mes)::text = to_char > ("outer".data_emissao, 'YYYYMM'::text)) > -> Nested Loop (cost=25960.94..27762.92 rows=1 > width=755) (actual time=3475.14..32090.12 rows=19923 loops=1) > Join Filter: (("inner".emp = "outer".emp) AND > ("outer".fil = "inner".fil) AND ("outer".codigo = "inner".cla_categoria) AND > ("outer".codigo = "inner".cla_marca) AND ("outer".codigo = "inner".situacao)) > -> Nested Loop (cost=25960.94..27705.22 > rows=10 width=665) (actual time=3474.12..17734.21 rows=199230 loops=1) > Join Filter: (("outer".emp > = "inner".emp) AND ("inner".fil = "outer".fil)) > -> Nested Loop > (cost=25960.94..27699.30 rows=1 width=638) (actual time=3474.02..6030.09 > rows=19923 loops=1) > Join Filter: (("inner".emp > = "outer".emp) AND ("inner".empfil = "outer".fil)) The reason the planner is choosing nestloop here is that it thinks there are only a few rows involved (notice the estimated row counts are all "1" or "10"). Nestloop is a fine join plan for small numbers of rows, but it pretty well sucks for tens of thousands of rows which is what you've actually got. I am not sure why the row-count estimates are so far off, but it could be because the planner is unaware of cross-column correlations in your data. Are the multiple join conditions actually necessary, or are some of them redundant? A quick and dirty thing you could try to see if the plan can be improved is to set "enable_nestloop" off (do NOT set "enable_seqscan" off). In the long run you want the planner to do better without such a brute-force hack, though. A simple answer is to boost the statistics target on the join columns and re-analyze, but that may not help much if the real issue is cross-column correlations. A more invasive solution is to reconsider your data design. It looks like you have a lot of multi-column join keys --- can you find ways to combine those into single columns? (As an example, I'd never build a table containing separate date and time columns rather than a single timestamp column.) I realize that this'd probably be a pain in the neck, but if you can do it, it would simplify your queries as well as help the planner produce better plans. regards, tom lane