X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 36B8552862 for ; Wed, 8 Jun 2005 16:34:34 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 29391-03 for ; Wed, 8 Jun 2005 19:34:31 +0000 (GMT) Received: from zproxy.gmail.com (zproxy.gmail.com [64.233.162.198]) by svr1.postgresql.org (Postfix) with ESMTP id 9DA2B52851 for ; Wed, 8 Jun 2005 16:34:31 -0300 (ADT) Received: by zproxy.gmail.com with SMTP id 16so644288nzp for ; Wed, 08 Jun 2005 12:34:32 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition; b=r3yyb/VT9FQ9OD+Sn2x3b/6veC8JKuYyzUxlXsIzuzRQd0MGvxlNIP/GZ0h9r3XO1ozLtI2/mRHFpU25rT+e6ZmEE7GNixwfRhJFsqLObk2QdAOnZOkNJYpDUuJWlvEhsY+9l9Ub1xh/9HJEw6Q/3FvDqj9NZ5mB+YpoTLTHVFI= Received: by 10.36.224.21 with SMTP id w21mr2297645nzg; Wed, 08 Jun 2005 12:34:32 -0700 (PDT) Received: by 10.36.47.20 with HTTP; Wed, 8 Jun 2005 12:34:32 -0700 (PDT) Message-ID: <8d04ce990506081234121995f9@mail.gmail.com> Date: Wed, 8 Jun 2005 12:34:32 -0700 From: Junaili Lie Reply-To: Junaili Lie To: pgsql-performance@postgresql.org Subject: Help with rewriting query Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.09 tagged_above=0 required=5 tests=AWL, RCVD_BY_IP X-Spam-Level: X-Archive-Number: 200506/186 X-Sequence-Number: 12823 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=3Df.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.