Re: Re: [GENERAL] question (or feature-request): over ( partition by ... order by LIMIT N)

From: Hitoshi Harada <umi(dot)tanuki(at)gmail(dot)com>
To: David Fetter <david(at)fetter(dot)org>
Cc: "A(dot) Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com>, PG Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Re: [GENERAL] question (or feature-request): over ( partition by ... order by LIMIT N)
Date: 2010-03-26 02:06:07
Message-ID: e08cc0401003251906uc5270cay419937323379f427@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

2010/3/26 David Fetter <david(at)fetter(dot)org>:
> On Wed, Mar 24, 2010 at 06:31:59PM +0100, A. Kretschmer wrote:
>> Hello @all,
>>
>> I know, i can do:
>>
>> select * from (select ... row_number() over (...) ...) foo where
>> row_number < N
>>
>> to limit the rows per group, but the inner select has to retrieve
>> the whole set of records and in the outer select most of them
>> discarded.
>
> That sounds like the optimizer's falling down on the job.  Would this
> be difficult to fix?

I believe this isn't the task of window functions. In fact, "over( ...
LIMIT n)" or optimizer hack will fail on multiple window definitions.

To take top N items of each group (I agree this is quite common job),
I'd suggest syntax that is done by extending DISTINCT ON.

SELECT DISTINCT n ON(key1, key2) ...

where "n" means top "n" items on each "key1, key2" group. The current
DISTINCT ON() syntax is equivalent to DISTINCT 1 ON() in this way.
That'll be fairly easy to implement and you aren't be bothered by this
like multiple window definitions. The cons of this is that it can be
applied to only row_number logic. You may want to use rank,
dense_rank, etc. sometimes.

Regards,

--
Hitoshi Harada

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Pete Kay 2010-03-26 02:40:45 Problem with Memory Leak
Previous Message Geoffrey Gowey 2010-03-26 02:04:16 Geoffrey Gowey wants to stay in touch on LinkedIn

Browse pgsql-hackers by date

  From Date Subject
Next Message Mike Lewis 2010-03-26 03:39:07 Postgres 9.0 Alpha, GIN indexes, and intarray contrib module, and SQL Functions
Previous Message Thom Brown 2010-03-26 00:24:54 Re: Remove ROW | ROWS from OFFSET and FETCH