From: Scott Cain <cain@cshl.org>
To: pgsql-sql@postgresql.org
Subject: OR vs UNION
Date: 17 Jul 2003 14:11:46 -0400
Message-ID: <1058465506.3345.15.camel@localhost.localdomain> (raw)
Hello,
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. Are there any rules of thumb for when this might be the
case? As an example here is a query of the type I am discussing:
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
which could easily be rewritten as a set of select statements for each
type_id and then union them together. For this particular query,
explain analyze indicates that this is the more efficient form, but I
could easily see that at other times/for other parameters, a set unioned
together would be better. Are there any guidelines for this?
Thanks,
Scott
--
------------------------------------------------------------------------
Scott Cain, Ph. D. cain@cshl.org
GMOD Coordinator (http://www.gmod.org/) 216-392-3087
Cold Spring Harbor Laboratory
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
Subject: Re: OR vs UNION
In-Reply-To: <1058465506.3345.15.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