From: | Jesper Krogh <jesper(at)krogh(dot)cc> |
---|---|
To: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
Cc: | pgsql-hackers(at)postgresql(dot)org |
Subject: | Re: window function count(*) and limit |
Date: | 2010-10-23 17:55:14 |
Message-ID: | 4CC32182.9090705@krogh.cc |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-hackers |
On 2010-10-23 18:42, Tom Lane wrote:
> Jesper Krogh<jesper(at)krogh(dot)cc> writes:
>
>> I have been puzzled about the evaluation order when using window
>> functions and limit.
>>
> It's basically FROM -> WHERE -> window functions -> LIMIT.
>
>> I expected it to either count to 3 or blow up and tell me that count(*)
>> wasn't a window function.
>>
> Any aggregate function can be used as a window function. It just
> aggregates over the whole partition (which in this case is all 8
> rows that satisfy the WHERE).
>
Thank you for clarifying.
Testing more.. I can see that it does that in all situations, so
it is not that "confusing" anymore. I still think it is hugely
counter intuitive and attached is a documentation patch
that should clarify it a bit.
I would just assume the reverse behaviour would be way closer to
useful for everyone. (say if you want the window function to operate
over the full set, then you wouldn't specify the limit). But that
doesn't help anyone if the SQL-spec specifies it otherwise.
As a sidenote.. the queryplan for some of them seems quite "unoptimal".
# explain select id,last_value(id) over () from testtable order by id
asc limit 3;
QUERY PLAN
-------------------------------------------------------------------------------------------------------
Limit (cost=0.00..1.30 rows=3 width=4)
-> WindowAgg (cost=0.00..6117917.93 rows=14165769 width=4)
-> Index Scan using testtable_pkey on testtable
(cost=0.00..5940845.82 rows=14165769 width=4)
The output is essentially the 3 smallest ids and the largest one in the
table which
all can be found by both a forward and reverse scan on the primary key
index so above
is absolutely not the cheapest way to find the result.
--
Jesper
Attachment | Content-Type | Size |
---|---|---|
windows-function-doc.patch | text/x-patch | 961 bytes |
From | Date | Subject | |
---|---|---|---|
Next Message | Josh Berkus | 2010-10-23 19:01:21 | Re: GIN vs. Partial Indexes |
Previous Message | Tom Lane | 2010-10-23 16:42:49 | Re: window function count(*) and limit |