pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Josh Berkus <josh@agliodbs.com>
To: Scott Cain <cain@cshl.org>
To: pgsql-sql@postgresql.org
Subject: Re: OR vs UNION
Date: Thu, 17 Jul 2003 12:00:18 -0700
Message-ID: <200307171200.18626.josh@agliodbs.com> (raw)
In-Reply-To: <1058465506.3345.15.camel@localhost.localdomain>
References: <1058465506.3345.15.camel@localhost.localdomain>

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




view thread (6+ messages)  latest in thread

Message-ID: <200307171200.18626.josh@agliodbs.com>
Permalink:  ../200307171200.18626.josh@agliodbs.com/
Also on:    postgresql.org/message-id/200307171200.18626.josh@agliodbs.com

 · 

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: josh@agliodbs.com, cain@cshl.org
  Subject: Re: OR vs UNION
  In-Reply-To: <200307171200.18626.josh@agliodbs.com>

* 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