X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 3EB269F94EA for ; Tue, 23 May 2006 10:26:50 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 69663-04 for ; Tue, 23 May 2006 10:26:40 -0300 (ADT) X-Greylist: delayed 15:15:26.696014 by SQLgrey- Received: from mail.optiosoftware.com (mail.optio.com [192.216.93.10]) by postgresql.org (Postfix) with ESMTP id 39EE69F946D for ; Tue, 23 May 2006 10:26:39 -0300 (ADT) X-MimeOLE: Produced By Microsoft Exchange V6.5 Content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Subject: Re: slow query using sub select Date: Tue, 23 May 2006 09:26:37 -0400 Message-ID: <47668A1334CDBF46927C1A0DFEB223D340EF94@mail.optiosoftware.com> X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: [PERFORM] slow query using sub select thread-index: AcZ99H95e0LzlgA4T722/LypaaE1kwAd8SiQ From: "Tim Jones" To: "Tom Lane" Cc: X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200605/428 X-Sequence-Number: 19215 that worked like a champ nice call as always!=20 thanks Tim Jones Healthcare Project Manager Optio Software, Inc. (770) 576-3555 -----Original Message----- From: Tom Lane [mailto:tgl@sss.pgh.pa.us]=20 Sent: Monday, May 22, 2006 7:07 PM To: Tim Jones Cc: pgsql-performance@postgresql.org Subject: Re: [PERFORM] slow query using sub select=20 "Tim Jones" writes: > I am having a problem with a sub select query being kinda slow. The > query is as follows: =20 > select batterycode, batterydescription, observationdate from Battery=20 > t1 where patientidentifier=3D611802158 and observationdate =3D (select > max(observationdate) from Battery t2 where=20 > t2.batterycode=3Dt1.batterycode and patientidentifier=3D611802158) = order by batterydescription. Yeah, this is essentially impossible for the planner to optimize, because it doesn't see any way to de-correlate the subselect, so it does it over again for every row. You might find it works better if you cast the thing as a SELECT DISTINCT ON problem (look at the "weather report" example in the SELECT reference page). regards, tom lane