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

From Andreas Kretschmer
Subject Re: bad execution plan for subselects containing windowing-function
Date
Msg-id 20100114183139.GA11334@tux
Whole thread Raw
In response to Re: bad execution plan for subselects containing windowing-function  (Andreas Kretschmer <akretschmer@spamfence.net>)
List pgsql-performance
Andreas Kretschmer <akretschmer@spamfence.net> wrote:
> > 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.
>
> Okay, i believe you now ;-)
>
> I will try to find a case with different results ...

I have got it!


test=# select * from values;
 id | value
----+-------
  1 |    10
  2 |    20
  3 |    30
  4 |    40
  5 |    50
  6 |    60
  7 |    70
  8 |    80
  9 |    90
(9 rows)

Time: 0.240 ms
test=*# select id, sum(value) over (order by id) from values where id = 5;
 id | sum
----+-----
  5 |  50
(1 row)

Time: 0.352 ms
test=*# select * from (select id, sum(value) over (order by id) from values) foo where id = 5;
 id | sum
----+-----
  5 | 150
(1 row)

Time: 0.383 ms



Andreas
--
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect.                              (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly."   (unknown)
Kaufbach, Saxony, Germany, Europe.              N 51.05082°, E 13.56889°

pgsql-performance by date:

Previous
From: Greg Smith
Date:
Subject: Re: a heavy duty operation on an "unused" table kills my server
Next
From: "Kevin Grittner"
Date:
Subject: Re: Slow "Select count(*) ..." query on table with 60 Mio. rows