Re: Unnecessary function calls

From: Richard Huxton <dev(at)archonet(dot)com>
To: Markus Schiltknecht <markus(at)bluegap(dot)ch>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Unnecessary function calls
Date: 2006-05-02 11:52:40
Message-ID: 44574808.4010602@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Markus Schiltknecht wrote:
> Hi,
>
> when using LIMIT, how do I tell the planner to only call a function for
> rows it returns?
>
> An example: I want to fetch the top five categories. A function
> get_category_text_path(cat_id int) returns the textual representation of
> the category. For that I do something like:
>
> SELECT id, get_category_text_path(id)
> FROM category
> ORDER BY rank
> LIMIT 5
>
> Unfortunately this takes very long because it calls
> get_category_text_path() for all of the 450'000 categories in the table.
> But I only need the full text path of the top five rows.

SELECT id, get_category_text_path(id)
FROM (
SELECT id FROM category ORDER BY rank LIMIT 5
) AS foo

HTH
--
Richard Huxton
Archonet Ltd

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Martijn van Oosterhout 2006-05-02 12:02:19 Re: Unnecessary function calls
Previous Message Terry Fielder 2006-05-02 11:39:01 Re: Unnecessary function calls