Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZwAU0-0002fp-H5 for pgsql-performance@arkaria.postgresql.org; Tue, 10 Nov 2015 15:05:16 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZwATz-0002rq-VC for pgsql-performance@arkaria.postgresql.org; Tue, 10 Nov 2015 15:05: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 1ZwASL-0000xS-V4 for pgsql-performance@postgresql.org; Tue, 10 Nov 2015 15:03: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 1ZwASJ-0001Pg-0X for pgsql-performance@postgresql.org; Tue, 10 Nov 2015 15:03: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 tAAF3TXR002571; Tue, 10 Nov 2015 10:03:29 -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: Comments: In-reply-to David Osborne message dated "Tue, 10 Nov 2015 12:13:21 +0000" Date: Tue, 10 Nov 2015 10:03:29 -0500 Message-ID: <2570.1447167809@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: > We have 3 different ways we have to do the final X join condition (we use 3 > subqueries UNIONed together), but the one causing the issues is: > (o.branch_code || o.po_number = replace(ss.order_no,' ','')) > ... So we can see straight away that the outer Nested loop expects 1 row, and > gets 53595. This isn't going to help the planner pick the most efficient > plan I suspect. > I've tried increasing default_statistics_target to the max and re analysing > all the tables involved but this does not help the estimate. > I suspect it's due to the join being based on functional result meaning any > stats are ignored? Yeah, the planner is not nearly smart enough to draw any useful conclusions about the selectivity of that clause from standard statistics. What you might try doing is creating functional indexes on the two subexpressions: create index on branch_purchase_order ((branch_code || po_number)); create index on stocksales_ib (replace(order_no,' ','')); (actually it looks like you've already got the latter one) and then re-ANALYZING. I'm not necessarily expecting that the planner will actually choose to use these indexes in its plan; but their existence will prompt ANALYZE to gather stats about the expression results, and that should at least let the planner draw more-accurate conclusions about the selectivity of the equality constraint. 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