Re: sub-select with aggregate

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: Tomasz Myrta <jasiek(at)klaster(dot)net>
Cc: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: sub-select with aggregate
Date: 2002-10-23 13:02:49
Message-ID: 20021023055926.O3876-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Wed, 23 Oct 2002, Tomasz Myrta wrote:

> Hi
> I want to perform query looking like this:
>
> select
> user_id,
> a/sum_a as percent_a,
> b/sum_b as percent_b
> from
> users join
> (select
> group_id,
> sum(a) as sum_a,
> sum(b) as sum_b
> from users group by group_id) X using (group_id)
> where group_id=3;
>
> This query works, but very slow. Subquery with aggregate is performed
> for all table rows instead of group_id=3.

Does using X.group_id=3 in the where clause work better?

On 7.3 with no statistics for the table, that appears
to move the filter into the subquery plan rather than the
outer users scan.

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tomasz Myrta 2002-10-23 13:52:56 Re: 7.2 time format funtion issue
Previous Message Tomasz Myrta 2002-10-23 10:23:47 Re: odbc drivers