Re: Best way to get the latest revision from a table

From: "Kevin Grittner" <Kevin(dot)Grittner(at)wicourts(dot)gov>
To: "Nikolas Everett" <nik9000(at)gmail(dot)com>
Cc: <sthomas(at)peak6(dot)com>, "pgsql-performance(at)postgresql(dot)org" <pgsql-performance(at)postgresql(dot)org>, "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Subject: Re: Best way to get the latest revision from a table
Date: 2011-01-15 00:59:50
Message-ID: 4D309D26020000250003963E@gw.wicourts.gov
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:

> Shaun's example is a bit off

> As for speed, either one might be faster in a particular
> situation.

After fixing a mistake in my testing and learning from Tom's example
I generated queries against the OP's test data which produce
identical results, and I'm finding no significant difference between
run times for the two versions. The OP should definitely try both
against the real tables.

Here are the queries which run against the test set:

DROP TABLE IF EXISTS request;
CREATE TEMPORARY TABLE request (a INTEGER NOT NULL);
INSERT INTO request SELECT a FROM generate_series(2, 200) AS t(a);
ANALYZE request;
SELECT y.*
from (select a, max(revision) as revision
from test join request using (a)
group by a) x
join test y using (a, revision)
order by a, revision DESC;

DROP TABLE IF EXISTS request;
CREATE TEMPORARY TABLE request (a INTEGER NOT NULL);
INSERT INTO request SELECT a FROM generate_series(2, 200) AS t(a);
ANALYZE request;
SELECT DISTINCT ON (a, b, c) revision, a, b, c
FROM test join request using (a)
ORDER BY a, b, c, revision DESC;

Sorry for not sorting it out better initially.

-Kevin

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Nikolas Everett 2011-01-15 01:50:02 Re: Best way to get the latest revision from a table
Previous Message Tom Lane 2011-01-14 23:40:43 Re: Best way to get the latest revision from a table