Grabbing Newest Records From Duplicates
Grabbing Newest Records From Duplicates
От:
"Travis Whitton" <tinymountain@gmail.com>
Дата:
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
Re: Grabbing Newest Records From Duplicates
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
"Travis Whitton" writes: > 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? No, it's gonna be pretty awful. The best way I know of involves DISTINCT ON (see the "weather reports" example in the SELECT reference page). Unfortunately that's a Postgres-only construct. If you want something portable then you'll need something messy with subqueries... regards, tom lane
Re: Grabbing Newest Records From Duplicates
От:
"Travis Whitton" <tinymountain@gmail.com>
Дата:
One last question, using the "weather report" example, is it going to improve performance for the DISTINCT ON query to add an index to the location and time columns?
Thanks a lot,
Travis
Thanks a lot,
Travis
On 3/16/07, Tom Lane <tgl@sss.pgh.pa.us> wrote:
"Travis Whitton" <tinymountain@gmail.com> writes:
> 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?
No, it's gonna be pretty awful. The best way I know of involves
DISTINCT ON (see the "weather reports" example in the SELECT reference
page). Unfortunately that's a Postgres-only construct. If you want
something portable then you'll need something messy with subqueries...
regards, tom lane
Re: Grabbing Newest Records From Duplicates
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
"Travis Whitton" writes: > One last question, using the "weather report" example, is it going to > improve performance for the DISTINCT ON query to add an index to the > location and time columns? A two-column index matching the ORDER BY that you use might help, or then again might not --- for full-table scans it's often faster to sort than to try to use an index. regards, tom lane