From: Tom Lane <tgl@sss.pgh.pa.us>
To: Tim Jones <TJones@optio.com>
Cc: pgsql-performance@postgresql.org
Subject: Re: slow query using sub select
Date: Mon, 22 May 2006 19:07:24 -0400
Message-ID: <15852.1148339244@sss.pgh.pa.us> (raw)
In-Reply-To: <47668A1334CDBF46927C1A0DFEB223D340EED2@mail.optiosoftware.com>
References: <47668A1334CDBF46927C1A0DFEB223D340EED2@mail.optiosoftware.com>
"Tim Jones" <TJones@optio.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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-performance@postgresql.org
Cc: tgl@sss.pgh.pa.us, TJones@optio.com
Subject: Re: slow query using sub select
In-Reply-To: <15852.1148339244@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox