Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 4E4DD9FB302 for ; Sun, 22 Apr 2007 17:42:38 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 98557-07 for ; Sun, 22 Apr 2007 17:42:35 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id B62529FB2BC for ; Sun, 22 Apr 2007 17:42:35 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.6/8.13.6) with ESMTP id l3MKgXcY010144; Sun, 22 Apr 2007 16:42:33 -0400 (EDT) To: "henk de wit" cc: pgsql-performance@postgresql.org Subject: Re: Redundant sub query triggers slow nested loop left join In-reply-to: References: Comments: In-reply-to "henk de wit" message dated "Sun, 22 Apr 2007 22:22:22 +0200" Date: Sun, 22 Apr 2007 16:42:33 -0400 Message-ID: <10143.1177274553@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200704/372 X-Sequence-Number: 24249 "henk de wit" writes: > I understand the above looks like a complicated mess, but would you > have any pointers of what I could possibly do next to force a better plan? Taking a closer look, it seems the problem is the underestimation of the number of rows resulting from this relation scan: > -> Bitmap Heap Scan on > banners_links (cost=11.43..954.03 rows=2 width=73) (actual > time=0.128..1.069 rows=359 loops=1) > Recheck Cond: (merchant_id = 5631) > Filter: ((status)::text = '0'::text) > -> Bitmap Index Scan on > banners_links_merchant_id_idx (cost=0.00..11.43 rows=424 width=0) (actual > time=0.089..0.089 rows=424 loops=1) > Index Cond: (merchant_id = 5631) You might be able to improve matters by increasing the statistics target for this table. I have a bad feeling though that the problem may be lack of cross-column statistics --- the thing is evidently assuming that only about 1 in 200 rows have status = '0', which might be accurate as a global average but not for this particular merchant. What exactly is the relationship between status and merchant_id, anyway? regards, tom lane