Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id E86CA632B5D for ; Fri, 3 Sep 2010 14:15:03 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 77563-09 for ; Fri, 3 Sep 2010 17:14:52 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id 9187F632AD6 for ; Fri, 3 Sep 2010 14:14:52 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id o83HEifB025499; Fri, 3 Sep 2010 13:14:44 -0400 (EDT) To: kaloyan@digsys.bg cc: pgsql-performance@postgresql.org Subject: Re: Question about LEFT JOIN and query plan In-reply-to: <4C811F68.9060601@digsys.bg> References: <4C811F68.9060601@digsys.bg> Comments: In-reply-to Kaloyan Iliev Iliev message dated "Fri, 03 Sep 2010 19:16:40 +0300" Date: Fri, 03 Sep 2010 13:14:43 -0400 Message-ID: <25498.1283534083@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-10 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201009/13 X-Sequence-Number: 40216 Kaloyan Iliev Iliev writes: > I have I query which behave strangely (according to me). > According to the first plan PG makes absolutely unnecessary seq scan on > tables "invoices" and "domeini" and etc. I think you might get better results if you could get this rowcount estimate a bit more in line with reality: > -> Seq Scan on debts_desc dd (cost=0.00..2866.52 rows=23782 width=46) (actual time=0.481..45.085 rows=1037 loops=1) > Filter: (active AND (NOT paid) AND has_proform AND (NOT storned)) It's choosing to hash instead of doing (what it thinks will be) 23K index probes into the other table. For 1000 probes the decision might be different. I don't know if raising the stats target for that table will be enough to fix it. Most likely those four conditions are not uncorrelated. You might need to think about revising the table's representation so that the query condition can be simpler and thus more accurately estimated. regards, tom lane