David Osborne <david@xxxxxxxxxxx> 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@xxxxxxxxxxxxxx) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance