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 70B789FA999 for ; Mon, 22 May 2006 20:32:21 -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 50986-08 for ; Mon, 22 May 2006 20:32:16 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from server.nextgap2000.com (server.nextgap2000.com [66.98.160.7]) by postgresql.org (Postfix) with ESMTP id 966469FA92E for ; Mon, 22 May 2006 20:32:16 -0300 (ADT) Received: from 85.64.42.28.dynamic.barak-online.net ([85.64.42.28] helo=jonathanlaptop) by server.nextgap2000.com with esmtpa (Exim 4.52) id 1FiJsi-0007yB-AF; Tue, 23 May 2006 02:32:12 +0300 From: "Jonathan Blitz" To: "'Tim Jones'" , Subject: Re: slow query using sub select Date: Tue, 23 May 2006 02:33:09 +0200 Message-ID: <04e401c67e00$79648b50$ddf71005@jonathanlaptop> MIME-Version: 1.0 Content-Type: text/plain; charset="windows-1250" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook 11 In-Reply-To: <47668A1334CDBF46927C1A0DFEB223D340EED2@mail.optiosoftware.com> X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1807 Thread-Index: AcZ97KAanKWDbSdZSf+p4rSohMfwuAAExGbw X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - server.nextgap2000.com X-AntiAbuse: Original Domain - postgresql.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - anykey.co.il X-Source: X-Source-Args: X-Source-Dir: X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200605/426 X-Sequence-Number: 19213 > -----Original Message----- > From: Tim Jones [mailto:TJones@optio.com] > Sent: Tuesday, May 23, 2006 12:11 AM > To: pgsql-performance@postgresql.org > Subject: [PERFORM] slow query using sub select > > Hi, > I am having a problem with a sub select query being kinda slow. The > query is as follows: > > select batterycode, batterydescription, observationdate from Battery t1 > where patientidentifier=611802158 and observationdate = (select > max(observationdate) from Battery t2 where t2.batterycode=t1.batterycode > and patientidentifier=611802158) order by batterydescription. How about changing it into a standard join: select t1.batterycode, t1.batterydescription, t2.observationdate from Battery t1, (Select batterycode ,max(observationdate) from Battery t2 where patientidentifier=611802158 group by batterycode) AS T2 where t1. batterycode = t2. batterycode Jonathan Blitz AnyKey Limited Israel -- No virus found in this outgoing message. Checked by AVG Free Edition. Version: 7.1.392 / Virus Database: 268.6.1/344 - Release Date: 05/19/2006