pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bruce Momjian <pgman@candle.pha.pa.us>
To: terry@ashtonwoodshomes.com
Cc: josh@agliodbs.com, "'Scott Cain'" <cain@cshl.org>
Cc: pgsql-sql@postgresql.org
Subject: Re: OR vs UNION
Date: Mon, 21 Jul 2003 20:32:35 -0400 (EDT)
Message-ID: <200307220032.h6M0WZs03904@candle.pha.pa.us> (raw)
In-Reply-To: <004f01c34c9d$ce9edcc0$2766f30a@development.greatgulfhomes.com>


Gavin reported UNION faster than OR in some case when doing fts queries
two years ago at O'Reilly.

---------------------------------------------------------------------------

terry@ashtonwoodshomes.com wrote:
> Actually, I have used a UNION to replace OR's, the case (simpliefied to)
> something like this:
> 
> Sample 1:
> WHERE (f1 = 'v1' OR f1 = '')
>   AND (f2 = 'v2' OR f2 = '')
> 
> Changed to Sample 2:
> WHERE (f1 = 'v1')
>   AND (f2 = 'v2')
> UNION
> WHERE (f1 = 'v1')
>   AND (f2 = '')
> UNION
> WHERE (f1 = '')
>   AND (f2 = '')
> 
> 
> Note that Sample 1 is actually a simplified version, the queries are not
> exactly equivalent.
> 
> The point is that sample 2 ran MUCH faster because:
> a)  The table was *very* large
> b)  The OR clauses of sample 1 prevented the use of an INDEX,
> 
> Reason:  It is faster to scan an index 3 times then scan this very large
> table once.
> 
> I do not know if there is a proof to say that one can *always* replace OR's
> with a union, but sometimes certainly, and in this case it made things much
> better...
> 
> Terry Fielder
> Manager Software Development and Deployment
> Great Gulf Homes / Ashton Woods Homes
> terry@greatgulfhomes.com
> Fax: (416) 441-9085
> 
> 
> > -----Original Message-----
> > From: pgsql-sql-owner@postgresql.org
> > [mailto:pgsql-sql-owner@postgresql.org]On Behalf Of Josh Berkus
> > Sent: Thursday, July 17, 2003 3:00 PM
> > To: Scott Cain; pgsql-sql@postgresql.org
> > Subject: Re: [SQL] OR vs UNION
> >
> >
> > Scott,
> >
> > > I have a query that uses a series of ORs and I have heard
> > that sometimes
> > > this type of query can be rewritten to use UNION instead and be more
> > > efficient.
> >
> > I'd be interested to know where you heard that; as far as I
> > know, it could
> > only apply to conditional left outer joins.
> >
> > >      select distinct
> > f.name,fl.fmin,fl.fmax,fl.strand,f.type_id,f.feature_id
> > >      from feature f, featureloc fl
> > >      where
> > >        (f.type_id = 219 OR
> > >         f.type_id = 368 OR
> > >         f.type_id = 514 OR
> > >         f.type_id = 475 OR
> > >         f.type_id = 426 OR
> > >         f.type_id = 456 OR
> > >         f.type_id = 461 OR
> > >         f.type_id = 553 OR
> > >         f.type_id = 89) and
> > >       fl.srcfeature_id = 1 and
> > >       f.feature_id  = fl.feature_id and
> > >       fl.fmin <= 2491413 and fl.fmax >= 2485521
> >
> > Certainly a query of the above form would not benefit from
> > being a union.
> >
> > For readability, you could use an IN() statement rather than
> > a bunch of ORs
> > ... this would not help performance, but would make your
> > query easier to
> > type/read.
> >
> > --
> > -Josh Berkus
> >  Aglio Database Solutions
> >  San Francisco
> >
> >
> > ---------------------------(end of
> > broadcast)---------------------------
> > TIP 6: Have you searched our list archives?
> >
> >                http://archives.postgresql.org
> >
> 
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
> 
>                http://archives.postgresql.org
> 

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073



view thread (6+ messages)

Message-ID: <200307220032.h6M0WZs03904@candle.pha.pa.us>
Permalink:  ../200307220032.h6M0WZs03904@candle.pha.pa.us/
Also on:    postgresql.org/message-id/200307220032.h6M0WZs03904@candle.pha.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: pgman@candle.pha.pa.us, terry@ashtonwoodshomes.com, cain@cshl.org
  Subject: Re: OR vs UNION
  In-Reply-To: <200307220032.h6M0WZs03904@candle.pha.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox