Re: Help with rewriting query

From: Tobias Brox <tobias(at)nordicbet(dot)com>
To: Junaili Lie <junaili(at)gmail(dot)com>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: Help with rewriting query
Date: 2005-06-08 19:56:25
Message-ID: 20050608195625.GM8451@tobias.nordicbet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

[Junaili Lie - Wed at 12:34:32PM -0700]
> 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)

eventually, try something like

select p.id,(select f.id from food f where f.p_id=p.id order by f.id desc limit 1)
from person p

not tested, no warranties.

Since subqueries can be inefficient, use "explain analyze" to see which one
is actually better.

This issue will be solved in future versions of postgresql.

--
Tobias Brox, +47-91700050
Tallinn

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Jim Johannsen 2005-06-08 20:48:05 Re: Help with rewriting query
Previous Message Junaili Lie 2005-06-08 19:34:32 Help with rewriting query