Re: Picking out the most recent row using a time stamp column

From: Dave Crooke <dcrooke(at)gmail(dot)com>
To: Kevin Grittner <Kevin(dot)Grittner(at)wicourts(dot)gov>
Cc: Michael Glaesemann <grzm(at)seespotcode(dot)net>, pgsql-performance <pgsql-performance(at)postgresql(dot)org>
Subject: Re: Picking out the most recent row using a time stamp column
Date: 2011-02-24 23:38:37
Message-ID: AANLkTimSmjGrCqwZWRXU1_=FW0adbWdxLXMK0n=Vw+Cc@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Thanks to all .... I had a tickling feeling at the back of my mind that
there was a neater answer here. For the record, times (all from in-memory
cached data, averaged over a bunch of runs):

Dependent subquery = 117.9 seconds
Join to temp table = 2.7 sec
DISTINCT ON = 2.7 sec

So the DISTINCT ON may not be quicker, but it sure is tidier.

Cheers
Dave

On Thu, Feb 24, 2011 at 2:24 PM, Kevin Grittner <Kevin(dot)Grittner(at)wicourts(dot)gov
> wrote:

> Michael Glaesemann <grzm(at)seespotcode(dot)net> wrote:
>
> > SELECT DISTINCT ON (data.id_key)
> > data.id_key, data.time_stamp, data.value
> > FROM data
> > ORDER BY data.id_key, data.time_stamp DESC;
>
> Dang! I forgot the DESC in my post! Thanks for showing the
> *correct* version.
>
> -Kevin
>

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Dave Crooke 2011-02-24 23:53:08 Possible parser bug? .... Re: Picking out the most recent row using a time stamp column
Previous Message Merlin Moncure 2011-02-24 21:14:59 Re: Picking out the most recent row using a time stamp column