optimizing a query

From: Louis-David Mitterrand <vindex+lists-pgsql-sql(at)apartia(dot)org>
To: pgsql-sql(at)postgresql(dot)org
Subject: optimizing a query
Date: 2008-12-14 19:22:13
Message-ID: 20081214192213.GA11792@apartia.fr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi,

I have an 'event' table and an 'event_date' table pointing to it
containing (potentially) several event dates (start and (optionnaly)
end) for the event in the past, present and future.

I'm trying to build a query to select the most "relevant" date:
'current' or 'next' or 'last' (by order of priority).

Actually I already have a view of event+"most relevant"event_date:

CREATE VIEW event_story_review AS
SELECT d.* FROM event_list_story_review d
WHERE (d.id_date = (SELECT d2.id_date FROM event_date d2 WHERE
(d2.id_event = d.id_event)
ORDER BY d2.end_date is not null desc,
(d2.end_date >= d.today) DESC,
d2.start_date LIMIT 1));

This works but I am bothered by the subquery which has a slight
performance impact on all queries using this view (there are many in my
app).

Is there a better way of doing it? maybe without a subquery?

Thanks,

--
http://www.critikart.net

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Corey Horton 2008-12-14 19:26:05 Re: [SQL] array_to_string(anyarray, text) that was working in 8.1 is not working in 8.3
Previous Message Tom Lane 2008-12-14 18:16:03 Re: [SQL] array_to_string(anyarray, text) that was working in 8.1 is not working in 8.3