Unnecessary function calls

Поиск
Список
Период
Сортировка
От Markus Schiltknecht
Тема Unnecessary function calls
Дата
Msg-id 1146569874.18433.45.camel@fotomarburg
обсуждение исходный текст
Ответы Re: Unnecessary function calls  (Richard Huxton <dev@archonet.com>)
Re: Unnecessary function calls  (Martijn van Oosterhout <kleptog@svana.org>)
Re: Unnecessary function calls  (Terry Fielder <terry@ashtonwoodshomes.com>)
Список pgsql-general
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.

It does not matter if I declare the function to be IMMUTABLE, STABLE or
VOLATILE - it gets called for every row in category (which normally is
what you want I guess).

How can I rewrite the query to call get_category_text_path() only for
the top five rows?

Thanks for hints.

Markus



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

Предыдущее
От: "Florian G. Pflug"
Дата:
Сообщение: Using a pointer as statetype for an aggregate
Следующее
От: Richard Huxton
Дата:
Сообщение: Re: Unnecessary function calls