X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.192]) by svr1.postgresql.org (Postfix) with ESMTP id D0911CC839 for ; Thu, 17 Jul 2003 19:00:49 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (svr4.postgresql.org [64.117.224.192]) (amavisd-new, port 10024) with ESMTP id 43022-08 for ; Thu, 17 Jul 2003 19:01:15 +0000 (GMT) Received: from davinci.ethosmedia.com (unknown [209.10.40.251]) by svr1.postgresql.org (Postfix) with ESMTP id 4D4E4CBB94 for ; Thu, 17 Jul 2003 16:00:45 -0300 (ADT) Received: from [66.219.92.2] (HELO temoku) by davinci.ethosmedia.com (CommuniGate Pro SMTP 4.0.2) with ESMTP id 3225740; Thu, 17 Jul 2003 12:01:31 -0700 Content-Type: text/plain; charset="iso-8859-1" From: Josh Berkus Reply-To: josh@agliodbs.com Organization: Aglio Database Solutions To: Scott Cain , pgsql-sql@postgresql.org Subject: Re: OR vs UNION Date: Thu, 17 Jul 2003 12:00:18 -0700 User-Agent: KMail/1.4.3 References: <1058465506.3345.15.camel@localhost.localdomain> In-Reply-To: <1058465506.3345.15.camel@localhost.localdomain> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Message-Id: <200307171200.18626.josh@agliodbs.com> X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200307/235 X-Sequence-Number: 14349 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.=20=20 I'd be interested to know where you heard that; as far as I know, it could= =20 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 =3D 219 OR > f.type_id =3D 368 OR > f.type_id =3D 514 OR > f.type_id =3D 475 OR > f.type_id =3D 426 OR > f.type_id =3D 456 OR > f.type_id =3D 461 OR > f.type_id =3D 553 OR > f.type_id =3D 89) and > fl.srcfeature_id =3D 1 and > f.feature_id =3D fl.feature_id and > fl.fmin <=3D 2491413 and fl.fmax >=3D 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= =20 ... this would not help performance, but would make your query easier to=20 type/read.=20 --=20 -Josh Berkus Aglio Database Solutions San Francisco