Grabbing Newest Records From Duplicates

From: "Travis Whitton" <tinymountain(at)gmail(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Grabbing Newest Records From Duplicates
Date: 2007-03-16 15:28:48
Message-ID: cf9b4f3e0703160828s2cc0b9v6c01db6e8634e37e@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Given the following test table, I want to grab only the newest record and
disregard any older duplicates based on name. Is this the most efficient way
to do it? Will the indicies even make a difference? The table below
demonstrates a simple proof of concept. My final table will have millions of
records; however, the test I'm doing now does not seem to use the indicies
(probably because there are too few records?).

optin=# \d test
Table "public.test"
Column | Type | Modifiers
--------+---------+-----------
name | text |
time | date |
id | integer |
Indexes:
"idx_name" btree (name)
"idx_time" btree ("time")

optin=# explain select * from test t1 where not exists (select 1 from test
t2 where t2.name = t1.name and t2.time > t1.time);

Thanks,
Travis

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2007-03-16 15:35:40 Re: Grabbing Newest Records From Duplicates
Previous Message Alvaro Herrera 2007-03-16 14:45:07 Re: INSERT INTO