Re: ORDER BY random() LIMIT 1 slowness

Поиск
Список
Период
Сортировка
От Greg Stark
Тема Re: ORDER BY random() LIMIT 1 slowness
Дата
Msg-id 87u1hc4h65.fsf@stark.dyndns.tv
обсуждение исходный текст
Ответ на Re: ORDER BY random() LIMIT 1 slowness  (Tom Lane <tgl@sss.pgh.pa.us>)
Список pgsql-general
Tom Lane <tgl@sss.pgh.pa.us> writes:

> "Gavin M. Roy" <gmr@justsportsusa.com> writes:
> > SELECT * FROM poetry ORDER BY random() LIMIT 1;
> > [ is slow for 35000 rows ]
>
> Yeah.  Basically this query is implemented as
>   (a) select all 35000 rows of "poetry";
>   (b) compute a random() value for each row;
>   (c) sort by the random() values;
>   (d) take the first row, discard the rest.

If you can generate a random value from your application layer you could do

select * from poetry LIMIT 1 OFFSET <random value>

Can offset values be placeholders in prepared queries? If not then this has
that disadvantage.

--
greg

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

Предыдущее
От: Barry Lind
Дата:
Сообщение: Re: JDBC, PgSQL 7.2: transactions not supported!!
Следующее
От: Medi Montaseri
Дата:
Сообщение: Re: Using Image datatype in Postgres