Received: from localhost (unknown [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id 0406D9FBF0D for ; Fri, 12 Oct 2007 16:04:04 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-maia, port 10024) with ESMTP id 16226-02 for ; Fri, 12 Oct 2007 16:03:58 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from gwmta.wicourts.gov (gwmta.wicourts.gov [165.219.244.99]) by postgresql.org (Postfix) with ESMTP id 9F7049FBE10 for ; Fri, 12 Oct 2007 16:04:00 -0300 (ADT) Received: from Courts-MTA by gwmta.wicourts.gov with Novell_GroupWise; Fri, 12 Oct 2007 14:03:59 -0500 Message-Id: <470F7EC5.EE98.0025.0@wicourts.gov> X-Mailer: Novell GroupWise Internet Agent 7.0.2 Date: Fri, 12 Oct 2007 14:03:50 -0500 From: "Kevin Grittner" To: , Subject: Re: Performance problems with prepared statements References: <1192027540.2495.3.camel@localhost.localdomain> <470CE918.4060608@dalibo.com> <1192044840.2549.21.camel@fedmac> <1192174502.2470.9.camel@localhost.localdomain> <1192201021.6170.47.camel@localhost.localdomain> In-Reply-To: <1192201021.6170.47.camel@localhost.localdomain> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200710/148 X-Sequence-Number: 27170 >>> On Fri, Oct 12, 2007 at 9:57 AM, in message <1192201021.6170.47.camel@localhost.localdomain>, Theo Kramer wrote:=20 >=20 > select * from foo where > (a =3D a1 and b =3D b1 and c >=3D c1) or > (a =3D a1 and b < b1) or > (a > a1) > order by a, b desc, c; >=20 > I have, however, found that transforming the above into a union based > query performs substantially better. =20 Another approach which often performs better is to rearrange the logic so that the high-order predicate is AND instead of OR: =20 select * from foo where ( a >=3D a1 and ( a > a1 or ( b <=3D b1 and ( b < b1 or ( c >=3D c1 ))))) order by a, b desc, c; =20 With the right index and a limit on rows, this can do particularly well. =20 -Kevin =20