Help with rewriting query

From: Junaili Lie <junaili(at)gmail(dot)com>
To: pgsql-performance(at)postgresql(dot)org
Subject: Help with rewriting query
Date: 2005-06-08 19:34:32
Message-ID: 8d04ce990506081234121995f9@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Hi,
I have the following table:
person - primary key id, and some attributes
food - primary key id, foreign key p_id reference to table person.

table food store all the food that a person is eating. The more recent
food is indicated by the higher food.id.

I need to find what is the most recent food a person ate for every person.
The query:
select f.p_id, max(f.id) from person p, food f where p.id=f.p_id group
by f.p_id will work.
But I understand this is not the most efficient way. Is there another
way to rewrite this query? (maybe one that involves order by desc
limit 1)

Thank you in advance.

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Tobias Brox 2005-06-08 19:56:25 Re: Help with rewriting query
Previous Message Kim Bisgaard 2005-06-08 18:53:52 Re: full outer performance problem