Re: slow count in window query

Поиск
Список
Период
Сортировка
От Greg Stark
Тема Re: slow count in window query
Дата
Msg-id 407d949e0907160402y26bb728agabcbf2b27d9d6a43@mail.gmail.com
обсуждение исходный текст
Ответ на slow count in window query  (Pavel Stehule <pavel.stehule@gmail.com>)
Ответы Re: slow count in window query  (Pavel Stehule <pavel.stehule@gmail.com>)
Re: slow count in window query  (Hitoshi Harada <umi.tanuki@gmail.com>)
Список pgsql-hackers
On Wed, Jul 15, 2009 at 11:18 AM, Pavel Stehule<pavel.stehule@gmail.com> wrote:
> postgres=# select avg(a) from (select a, row_number() over (order by
> a) as r, count(*) over () as rc from x ) p where r in
> ((rc+1)/2,(rc+2)/2) ;

How does this compare to the plain non-windowing SQL implementation:

select a from x order by a offset (select trunc(count(*)/2) from x) limit 1

(except that that only works if count(*) is odd).

Interestingly finding the median is actually O(n) using Quickselect.
Maybe we should provide a C implementation of quickselect as a window
function. I'm not sure how to wedge in the concept that the sort is
unnecessary even though the ORDER BY is specified though.

I'm also not sure how to handle this if the set has to be spooled to
disk. Quicksort and Quickselect do a lot of scans throught he data and
wouldn't perform well on disk.

-- 
greg
http://mit.edu/~gsstark/resume.pdf


В списке pgsql-hackers по дате отправления:

Предыдущее
От: Peter Eisentraut
Дата:
Сообщение: Re: [GENERAL] pg_migrator not setting values of sequences?
Следующее
От: Pavel Stehule
Дата:
Сообщение: Re: slow count in window query