Re: Can query planner prefer a JOIN over a high-cost Function?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: David McNett <pglists(at)macnugget(dot)org>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: Can query planner prefer a JOIN over a high-cost Function?
Date: 2013-08-20 19:52:52
Message-ID: 12172.1377028372@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

David McNett <pglists(at)macnugget(dot)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

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Tom Lane 2013-08-20 21:02:09 Re: queries with DISTINCT / GROUP BY giving different plans
Previous Message David McNett 2013-08-20 18:06:51 Can query planner prefer a JOIN over a high-cost Function?