Re: Help with rewriting query - Mailing list pgsql-performance

From Tobias Brox
Subject Re: Help with rewriting query
Date
Msg-id 20050608195625.GM8451@tobias.nordicbet.com
Whole thread Raw
In response to Help with rewriting query  (Junaili Lie <junaili@gmail.com>)
Responses Re: Help with rewriting query
List 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

pgsql-performance by date:

Previous
From: Junaili Lie
Date:
Subject: Help with rewriting query
Next
From: "Kevin Grittner"
Date:
Subject: Recommendations for configuring a 200 GB database