Re: bad execution plan for subselects containing windowing-function - Mailing list pgsql-performance

From Tom Lane
Subject Re: bad execution plan for subselects containing windowing-function
Date
Msg-id 25915.1263490925@sss.pgh.pa.us
Whole thread Raw
In response to Re: bad execution plan for subselects containing windowing-function  (Andreas Kretschmer <akretschmer@spamfence.net>)
Responses Re: bad execution plan for subselects containing windowing-function  (Andreas Kretschmer <akretschmer@spamfence.net>)
List pgsql-performance
Andreas Kretschmer <akretschmer@spamfence.net> writes:
> Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> I see no bug here.  Your second query asks for a much more complicated
>> computation, it's not surprising it takes longer.

> But sorry, I disagree. It is the same query with the same result. I can't see
> how the queries should return different results.

In the first query

select id, avg(value) over (partition by value) from values where id = 50 order by id;

the avg() calculations are being done over only rows with id = 50.  In
the second query

select * from (select id, avg(value) over (partition by value) from values  order by id) foo where id = 50;

they are being done over all rows.  In this particular example you
happen to get the same result, but that's just because "avg(foo) over
partition by foo" is a dumb example --- it will necessarily just yield
identically foo.  In more realistic computations the results would be
different.

            regards, tom lane

pgsql-performance by date:

Previous
From: Andreas Kretschmer
Date:
Subject: Re: bad execution plan for subselects containing windowing-function
Next
From: Greg Smith
Date:
Subject: Re: Slow "Select count(*) ..." query on table with 60 Mio. rows