Re: Windowing Function Patch Review -> ROW_NUMBER without ORDER BY

From: "Vladimir Sitnikov" <sitnikov(dot)vladimir(at)gmail(dot)com>
To: "Andreas Joseph Krogh" <andreak(at)officenet(dot)no>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Windowing Function Patch Review -> ROW_NUMBER without ORDER BY
Date: 2008-11-10 00:01:04
Message-ID: 1d709ecc0811091601x4896ee72ib0c3cbf64f5ce3a6@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

>
>
> I see this as a greate feature.

I would treat ranking functions without explicit order by clause as a
feature rather than a bug. However, I believe, in most cases optimizer will
avoid additional sort when possible, thus an "order by" in a windowing
clause would not cause any performance degradation.

>
> It will hopefully be possible to write:
>
> SELECT *, max(row_number()) over() as total_rows from employees;

I believe this query does not make sense. At least, "row_number" without
"over" sounds odd.

To count all the rows (if you really want to) you might use "count(*) over
()".

>
> To get the maximum number of rows in a separate column. Very usefull when
> writing queries to retrieve "paged" results. Like "Give me the 20 top
> articles sorted on date and also the total number of articles" in *one*
> query, eliminating the need for a separate count(*) query.

Sometimes it is better to perform several separate queries since optimizer
could use an index scan to get "20 top" and seq scan to get the "count(*)"

Regards,
Vladimir Sitnikov

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Simon Riggs 2008-11-10 00:11:21 Re: Reducing some DDL Locks to ShareLock
Previous Message Andreas Joseph Krogh 2008-11-09 23:30:33 Re: Windowing Function Patch Review -> ROW_NUMBER without ORDER BY