Re: range intervals in window function frames - Mailing list pgsql-general

From Daniel Popowich
Subject Re: range intervals in window function frames
Date
Msg-id 19719.51307.411469.748205@io.astro.umass.edu
Whole thread Raw
In response to Re: range intervals in window function frames  (Vincent Veyron <vv.lists@wanadoo.fr>)
Responses Re: range intervals in window function frames  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
Vincent Veyron writes:
> > What I would LIKE to do is this:
> >
> >   select *, avg(ts) over(order by ts range (interval '5 min') preceding)
> >          from sample order by ts;
> >
>
> This?
>
>
> select t1.ts, t1.value, (select avg(t2.value) from (select value from
> sample where (t1.ts-ts)::INTERVAL <= interval '5 minutes' and ts<t1.ts)
> as t2) from sample t1;

Close.  Your where clause needed to have (ts<=t1.ts).  It can also be
simplified to this:

select t1.ts, t1.value, (select avg(t2.value)
                                   from sample t2
                                   where (t1.ts - t2.ts) <= interval '5 min'
                                          and t2.ts <= t1.ts)
          from sample t1 order by t1.ts;

HOWEVER, the performance is horrible compared to using the
avg_over_interval() function!  On a table with 53,622 rows:

   EXPLAIN ANALYZE [the above select statement];

     ...
     Total runtime: 481235.867 ms

But this:

   EXPLAIN ANALYZE select ts, value,
                          avg_over_interval(ts, interval '5 min')
                          from sample order by ts;

     ...
     Total runtime: 3934.755 ms

8 minutes vs 4 seconds!

I'm imagining window functions would be even more efficient.  Yes?
Can anyone answer when range intervals will be implemented for window
functions, as in the quoted select at the top of this message?


Cheers,

Dan




pgsql-general by date:

Previous
From: "Elford,Andrew [Ontario]"
Date:
Subject: Re: Simple, free PG GUI/query tool wanted
Next
From: Eric McDonald
Date:
Subject: Postgres DOD Certification Common Criteria Level