Skip site navigation (1) Skip section navigation (2)

Re: slow query using sub select

From: "Jonathan Blitz" <jb(at)anykey(dot)co(dot)il>
To: "'Tim Jones'" <TJones(at)optio(dot)com>,<pgsql-performance(at)postgresql(dot)org>
Subject: Re: slow query using sub select
Date: 2006-05-23 00:33:09
Message-ID: 04e401c67e00$79648b50$ddf71005@jonathanlaptop (view raw, whole thread or download thread mbox)
Lists: pgsql-performance

> -----Original Message-----
> From: Tim Jones [mailto:TJones(at)optio(dot)com]
> Sent: Tuesday, May 23, 2006 12:11 AM
> To: pgsql-performance(at)postgresql(dot)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

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

In response to

pgsql-performance by date

Next:From: Antonio BatovanjaDate: 2006-05-23 07:10:29
Subject: Re: Query performance
Previous:From: Steinar H. GundersonDate: 2006-05-22 23:33:33
Subject: Re: Query performance

Privacy Policy | About PostgreSQL
Copyright © 1996-2018 The PostgreSQL Global Development Group