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

On Thu, 2003-07-17 at 15:00, Josh Berkus wrote:
> 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.

Hmm, don't know for sure where I heard it, however I do know from
previous experience that unioned queries worked better in a somewhat
similar query, though it was a different schema, so it is hard to
compare directly.

One way in which I thought it might make a difference is if I build
partial indexes on feature_id for each of the type_ids of interest
(there are several thousand in the database, but only 15 or 20 that I am
interested in querying).  That way, when I write the separate queries
for each type_id, the query planner would have access to the partial
indexes for each type, and therefore may be able to complete the
individual queries very quickly.
> 
> >      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. 
-- 
------------------------------------------------------------------------
Scott Cain, Ph. D.                                         cain@cshl.org
GMOD Coordinator (http://www.gmod.org/)                     216-392-3087
Cold Spring Harbor Laboratory




view thread (6+ messages)  latest in thread

Message-ID: <1058469505.3345.26.camel@localhost.localdomain>
Permalink:  ../1058469505.3345.26.camel@localhost.localdomain/
Also on:    postgresql.org/message-id/1058469505.3345.26.camel@localhost.localdomain

 · 

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

* 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