Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZwCO4-0001dt-F9 for pgsql-performance@arkaria.postgresql.org; Tue, 10 Nov 2015 17:07:16 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZwCO4-00071u-1l for pgsql-performance@arkaria.postgresql.org; Tue, 10 Nov 2015 17:07:16 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZwCMQ-0005BH-1A for pgsql-performance@postgresql.org; Tue, 10 Nov 2015 17:05:34 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZwCMN-0004DW-Kr for pgsql-performance@postgresql.org; Tue, 10 Nov 2015 17:05:32 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id tAAH5UYg015010; Tue, 10 Nov 2015 12:05:30 -0500 From: Tom Lane To: David Osborne cc: pgsql-performance@postgresql.org Subject: Re: Slow 3 Table Join with v bad row estimate In-reply-to: References: <2570.1447167809@sss.pgh.pa.us> Comments: In-reply-to David Osborne message dated "Tue, 10 Nov 2015 16:38:37 +0000" Date: Tue, 10 Nov 2015 12:05:30 -0500 Message-ID: <15009.1447175130@sss.pgh.pa.us> X-Pg-Spam-Score: -2.2 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org David Osborne writes: > Doesn't seem to quite do the trick. I created both those indexes (or the > missing one at least) > Then I ran analyse on stocksales_ib and branch_purchase_order. > I checked there were stats held in pg_stats for both indexes, which there > were. > But the query plan still predicts 1 row and comes up with the same plan. Meh. In that case, likely the explanation is that the various conditions in your query are highly correlated, and the planner is underestimating the number of rows that will satisfy them because it doesn't know about the correlation. But taking a step back, it seems like the core problem in your explain output is here: >> -> Nested Loop (cost=1.29..83263.71 rows=1 width=24) (actual time=0.196..23799.930 rows=53595 loops=1) >> Join Filter: (o.po_id = p.po_id) >> Rows Removed by Join Filter: 23006061 >> Buffers: shared hit=23217993 dirtied=1 That's an awful lot of rows being formed by the join only to be rejected. You should try creating an index on branch_purchase_order_products(po_id, product_code) so that the po_id condition could be enforced at the inner indexscan instead of the join. regards, tom lane -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance