Re: Re-ordering of OR conditions

From: "Simon Riggs" <simon(at)2ndquadrant(dot)com>
To: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "Jim Nasby" <jim(dot)nasby(at)enterprisedb(dot)com>, "PostgreSQL-development Development" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Re-ordering of OR conditions
Date: 2007-02-09 20:04:37
Message-ID: 1171051478.25938.107.camel@silverbirch.site
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Fri, 2007-02-09 at 11:46 -0500, Tom Lane wrote:
> "Jim Nasby" <jim(dot)nasby(at)enterprisedb(dot)com> writes:
> > IF I run the following with the a < 2900 condition first, the more
> > expensive EXISTS only gets executed when needed, but if I change the
> > order of the OR's, the EXISTS is always executed. It would be good if
> > the optimizer could re-order the OR conditions based on estimated
> > cost (granted, this wouldn't work very well if you've got functions
> > in the OR, but it'd still be useful):
>
> I looked at this for a bit. It's in principle do-able but I'm not
> sure it's a good idea. The problem is that while AND'ed condition
> lists are usually fairly short and hence cheap to sort, OR'ed condition
> lists are not infrequently very long --- nobody blinks an eye at
> hundreds of items in an IN-list for instance. I'm afraid we'd waste
> a lot more cycles sorting than we could hope to regain.

Seems like the planner could decide ahead of time whether sorting the
conditions at execution time was likely to be effective or not. Perhaps
limiting it to at most 5 conditions, where at least one of those was a
function or a join condition? That would be a fairly cheap test at
planning time, but potentially a good win at execution time.

The OR'ed condition is common condition when the schema uses complex
sub-classing. Now we have function costs it seems more likely this idea
would get used in practice.

Anyway, not necessarily for you to do, but sounds like a useful idea all
the same.

--
Simon Riggs
EnterpriseDB http://www.enterprisedb.com

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Jan Wieck 2007-02-09 20:16:42 Re: Proposal: Commit timestamp
Previous Message Jan Wieck 2007-02-09 20:01:23 Re: Proposal: Commit timestamp