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