Re: sub-select with aggregate - Mailing list pgsql-sql

From Stephan Szabo
Subject Re: sub-select with aggregate
Date
Msg-id 20021023055926.O3876-100000@megazone23.bigpanda.com
Whole thread Raw
In response to sub-select with aggregate  (Tomasz Myrta <jasiek@klaster.net>)
List 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.



pgsql-sql by date:

Previous
From: "peter"
Date:
Subject: Multiple Databases
Next
From: Tomasz Myrta
Date:
Subject: Re: 7.2 time format funtion issue