Re: slow query using sub select

From: "Tim Jones" <TJones(at)optio(dot)com>
To: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: <pgsql-performance(at)postgresql(dot)org>
Subject: Re: slow query using sub select
Date: 2006-05-23 13:26:37
Message-ID: 47668A1334CDBF46927C1A0DFEB223D340EF94@mail.optiosoftware.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

that worked like a champ nice call as always!

thanks

Tim Jones
Healthcare Project Manager
Optio Software, Inc.
(770) 576-3555

-----Original Message-----
From: Tom Lane [mailto:tgl(at)sss(dot)pgh(dot)pa(dot)us]
Sent: Monday, May 22, 2006 7:07 PM
To: Tim Jones
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: [PERFORM] slow query using sub select

"Tim Jones" <TJones(at)optio(dot)com> writes:
> 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.

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

Browse pgsql-performance by date

  From Date Subject
Next Message Dan Gorman 2006-05-23 17:40:01 Selects query stats?
Previous Message Antonio Batovanja 2006-05-23 07:10:29 Re: Query performance