Re: Can query planner prefer a JOIN over a high-cost Function? - Mailing list pgsql-performance

From Tom Lane
Subject Re: Can query planner prefer a JOIN over a high-cost Function?
Date
Msg-id 12172.1377028372@sss.pgh.pa.us
Whole thread Raw
In response to Can query planner prefer a JOIN over a high-cost Function?  (David McNett <pglists@macnugget.org>)
List pgsql-performance
David McNett <pglists@macnugget.org> writes:
> Is there any way for me to influence the query planner so that it can
> know that the JOIN is far less expensive than the function for planning?
> The COST attribute on the function appears to have no effect.

I think what you're missing is an index on examples.code_id, which
would allow for a plan like this one:

 Nested Loop  (cost=154.41..205263.18 rows=2185 width=16)
   ->  Seq Scan on codes c  (cost=0.00..1.06 rows=1 width=8)
         Filter: ((code)::text = 'three'::text)
   ->  Bitmap Heap Scan on examples e  (cost=154.41..205234.81 rows=2731 width=1
2)
         Recheck Cond: (code_id = c.code_id)
         Filter: (painfully_slow_function(example_id, value) IS TRUE)
         ->  Bitmap Index Scan on examples_code_id_idx  (cost=0.00..153.73 rows=
8192 width=0)
               Index Cond: (code_id = c.code_id)

If you really want to force the join to occur separately, you could
probably do something involving a sub-select with OFFSET 0, but I wouldn't
recommend pursuing that path unless you can't get a decent result without
contorting the query.

Another thing worth thinking about is whether you could precalculate the
expensive function via a functional index.  It'd have to be immutable,
but if it is, this is a useful way of changing the ground rules.

            regards, tom lane


pgsql-performance by date:

Previous
From: David McNett
Date:
Subject: Can query planner prefer a JOIN over a high-cost Function?
Next
From: Tom Lane
Date:
Subject: Re: queries with DISTINCT / GROUP BY giving different plans