Re: Strange performance boost with random()

Поиск
Список
Период
Сортировка
От Heikki Linnakangas
Тема Re: Strange performance boost with random()
Дата
Msg-id 52F93098.2090002@vmware.com
обсуждение исходный текст
Ответ на Strange performance boost with random()  (M Putz <puma@c007.de>)
Ответы Re: Strange performance boost with random()  (Sébastien Lorion <sl@thestrangefactory.com>)
Список pgsql-performance
On 02/10/2014 09:52 PM, M Putz wrote:
>
> Hello,
>
> While analyzing performance, we encountered the following phenomenon,
>
>     SELECT sum(pow(.5*generate_series,.5))
>     FROM generate_series(1,1000000);
>
> is much much (a hundred times) slower than
>
>     SELECT sum(pow(random()*generate_series,.5))
>     FROM generate_series(1,1000000);
>
> and asymptotic difference is even more astounding.
> This seems counter-intuitive, considering the cost of
> an additional random() call instead of a constant factor.
> What are the reasons for this strange performance boost?

Different data type. The first uses numeric, which is pretty slow for
doing calculations. random() returns a double, which makes the pow and
sum to also use double, which is a lot faster.

To see the effect, try these variants:

SELECT sum(pow(.5::float8 * generate_series,.5))
FROM generate_series(1,1000000);

SELECT sum(pow(random()::numeric * generate_series,.5))
FROM generate_series(1,1000000);

- Heikki


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

Предыдущее
От: M Putz
Дата:
Сообщение: Strange performance boost with random()
Следующее
От: Sébastien Lorion
Дата:
Сообщение: Re: Strange performance boost with random()