Help with rewriting query - Mailing list pgsql-performance

From Junaili Lie
Subject Help with rewriting query
Date
Msg-id 8d04ce990506081234121995f9@mail.gmail.com
Whole thread Raw
Responses Re: Help with rewriting query
List 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.

pgsql-performance by date:

Previous
From: Matthew Nuzum
Date:
Subject: Re: Help specifying new web server/database machine
Next
From: Tobias Brox
Date:
Subject: Re: Help with rewriting query