Re: Suboptimal query plan fixed by replacing OR with UNION

From: Gurjeet Singh <singh(dot)gurjeet(at)gmail(dot)com>
To: Steven Schlansker <steven(at)likeness(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-general(at)postgresql(dot)org, David Stryker <stryker(at)likeness(dot)com>
Subject: Re: Suboptimal query plan fixed by replacing OR with UNION
Date: 2012-07-07 04:24:39
Message-ID: CABwTF4U0uZtgy_FZpAgq9A8rgtDvkbBrsf2+EkSLLTKkDwq7aA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Jul 5, 2012 at 7:16 PM, Steven Schlansker <steven(at)likeness(dot)com>wrote:

>
> On Jul 5, 2012, at 3:51 PM, Tom Lane wrote:
>
> > Steven Schlansker <steven(at)likeness(dot)com> writes:
> >> Why is using an OR so awful here?
> >
> > Because the OR stops it from being a join (it possibly needs to return
> > some rows that are not in the semijoin of the two tables).
> >
> >> Why does it pick a sequential scan? Is this an optimizer bug
> >
> > No. It can't transform OR into a UNION because the results might not
> > be the same. I assume you don't care about removal of duplicates, or
> > have some reason to know that there won't be any ... but the planner
> > doesn't know that.
> >
>
> Thanks for the insight here. It still seems unfortunate that it picks a
> sequential scan -- but if there really is no more efficient way to do this,
> I will just rewrite the query.
>

It might not be applicable to this case (because of the use of ANY in
second branch of OR clause), but some databases provide a feature called
OR-Optimization, where the optimizer breaks up the query at OR clause
boundaries and uses UNION ALL operator to join the resulting queries, just
like you did. Optimizer does need to add additional AND clauses to some of
the branches to make sure the result set is not affected.

Just a thought.
--
Gurjeet Singh
EnterpriseDB Corporation
The Enterprise PostgreSQL Company

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Joe Miller 2012-07-07 13:15:21 Re: Re: index and data tablespaces on two separate drives or one RAID 0?
Previous Message Gurjeet Singh 2012-07-07 02:17:34 Re: two-column primary key (not the typical question)