Re: expensive function in select list vs limit clause - Mailing list pgsql-general

From Chris Mair
Subject Re: expensive function in select list vs limit clause
Date
Msg-id 792ac2c7283e3efab818aba9ca063416@smtp.hushmail.com
Whole thread Raw
In response to Re: expensive function in select list vs limit clause  (Albe Laurenz <laurenz.albe@wien.gv.at>)
List pgsql-general
>
> ORDER BY can only be processed after all rows have been fetched, this
> includes the expensive result column.
>
> You can easily avoid that by applying the LIMIT first:
>
>   SELECT r, expensive()
>   FROM (SELECT r
>         FROM big
>         ORDER BY r
>         LIMIT 10
>        ) inner;
>
> I don't know how hard it would be to only fetch the necessary columns before
> the ORDER BY and fetch the others after the LIMIT has been applied, but it
> is probably nontrivial and would require processing time for *everybody*
> who runs a query with ORDER BY to solve a rare problem that can easily be
> worked around.

Hi,

Tom Lane just pointed out that 9.6 is able to optimise this (at least
the synthetic example).

Anyway, my real problem could be beautifully improved by subselect-trick!

Thanks a lot!

Bye,
Chris.






pgsql-general by date:

Previous
From: Chris Mair
Date:
Subject: Re: expensive function in select list vs limit clause
Next
From: Adrian Klaver
Date:
Subject: Re: Advise on primary key for detail tables (OS: RaspberryPi)