Re: Simple query: how to optimize

Поиск
Список
Период
Сортировка
От Roger Hand
Тема Re: Simple query: how to optimize
Дата
Msg-id DB28E9B548192448A4E8C8A3C1B1E475611DAF@sj1-exch-01.us.corp.kailea.com
обсуждение исходный текст
Ответ на Simple query: how to optimize  (Collin Peters <cadiolis@gmail.com>)
Ответы Re: Simple query: how to optimize  (Collin Peters <cadiolis@gmail.com>)
Re: Simple query: how to optimize  ("Steinar H. Gunderson" <sgunderson@bigfoot.com>)
Список pgsql-performance
On October 28, 2005 2:54 PM
Collin Peters wrote:
> I have two tables, one is called 'users' the other is 'user_activity'.
...
> I am trying to write a simple query that returns the last time each
> user logged into the system.  This is how the query looks at the
> moment:
>
> SELECT u.user_id, MAX(ua.activity_date)
> FROM pp_users u
> LEFT OUTER JOIN user_activity ua ON (u.user_id = ua.user_id AND
> ua.user_activity_type_id = 7)
> WHERE u.userstatus_id <> 4
> AND age(u.joined_date) < interval '30 days'
> GROUP BY u.user_id

You're first joining against the entire user table, then filtering out the users
you don't need.

Instead, filter out the users you don't need first, then do the join:

SELECT users.user_id, MAX(ua.activity_date)
FROM
(SELECT u.user_id
FROM pp_users u
WHERE u.userstatus_id <> 4
AND age(u.joined_date) < interval '30 days'
) users
LEFT OUTER JOIN user_activity ua
  ON (users.user_id = ua.user_id
  AND ua.user_activity_type_id = 7)
GROUP BY users.user_id

(disclaimer: I haven't actually tried this sql)

В списке pgsql-performance по дате отправления:

Предыдущее
От: Havasvölgyi Ottó
Дата:
Сообщение: Re: Best way to check for new data.
Следующее
От: Collin Peters
Дата:
Сообщение: Re: Simple query: how to optimize