From: Josh Berkus <josh@agliodbs.com>
To: Dave Crooke <dcrooke@gmail.com>
Cc: Kevin Grittner <Kevin.Grittner@wicourts.gov>
Cc: Michael Glaesemann <grzm@seespotcode.net>
Cc: pgsql-performance@postgresql.org
Subject: Re: Picking out the most recent row using a time stamp column
Date: Thu, 24 Feb 2011 16:20:00 -0800
Message-ID: <4D66F5B0.5080502@agliodbs.com> (raw)
In-Reply-To: <AANLkTimSmjGrCqwZWRXU1_=FW0adbWdxLXMK0n=Vw+Cc@mail.gmail.com>
References: <AANLkTinTgU-jfPOhrfteBvhAhgG_8zknpk13N9L4QMap@mail.gmail.com>
<08B3368E-E623-4E9C-83F5-AB05B471AF4C@seespotcode.net>
<4D666A0F020000250003AFCE@gw.wicourts.gov>
<AANLkTimSmjGrCqwZWRXU1_=FW0adbWdxLXMK0n=Vw+Cc@mail.gmail.com>
On 2/24/11 3:38 PM, Dave Crooke wrote:
> 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
But wait, there's more! You haven't tested the Windowing Function
solution. I'll bet it's even faster.
SELECT id_key, time_stamp, value
FROM (
SELECT id_key, time_stamp, value,
row_number()
OVER ( PARTITION BY id_key
ORDER BY time_stamp DESC)
as ranking
FROM thetable
) as filtered_table
WHERE ranking = 1
--
-- Josh Berkus
PostgreSQL Experts Inc.
http://www.pgexperts.com
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: josh@agliodbs.com, dcrooke@gmail.com, Kevin.Grittner@wicourts.gov, grzm@seespotcode.net
Subject: Re: Picking out the most recent row using a time stamp column
In-Reply-To: <4D66F5B0.5080502@agliodbs.com>
* 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