Skip site navigation (1) Skip section navigation (2)

Re: bad execution plan for subselects containingwindowing-function

From: Andreas Kretschmer <akretschmer(at)spamfence(dot)net>
To: pgsql-performance(at)postgresql(dot)org
Subject: Re: bad execution plan for subselects containingwindowing-function
Date: 2010-01-14 18:31:39
Message-ID: 20100114183139.GA11334@tux (view raw or flat)
Thread:
Lists: pgsql-performance
Andreas Kretschmer <akretschmer(at)spamfence(dot)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°

In response to

pgsql-performance by date

Next:From: Kevin GrittnerDate: 2010-01-14 18:33:50
Subject: Re: Slow "Select count(*) ..." query on table with 60 Mio. rows
Previous:From: Greg SmithDate: 2010-01-14 18:30:46
Subject: Re: a heavy duty operation on an "unused" table kills my server

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group