Re: Getting a random row

Поиск
Список
Период
Сортировка
От Scott Marlowe
Тема Re: Getting a random row
Дата
Msg-id dcc563d10910140030r3e02d327q62924b02ffb1ec4d@mail.gmail.com
обсуждение исходный текст
Ответ на Re: Getting a random row  (Pavel Stehule <pavel.stehule@gmail.com>)
Ответы Re: Getting a random row  (Віталій Тимчишин <tivv00@gmail.com>)
Список pgsql-performance
On Wed, Oct 14, 2009 at 1:20 AM, Pavel Stehule <pavel.stehule@gmail.com> wrote:
> 2009/10/14 Thom Brown <thombrown@gmail.com>:
>> 2009/10/14 Scott Marlowe <scott.marlowe@gmail.com>:
>>>
>>> If what you're trying to do is emulate a real world app which randomly
>>> grabs rows, then you want to setup something ahead of time that has a
>>> pseudo random order and not rely on using anything like order by
>>> random() limit 1 or anything like that.  Easiest way is to do
>>> something like:
>>>
>>> select id into randomizer from maintable order by random();
>>>
>>> then use a cursor to fetch from the table to get "random" rows from
>>> the real table.
>>>
>>>
>>
>> Why not just do something like:
>>
>> SELECT thisfield, thatfield
>> FROM my_table
>> WHERE thisfield IS NOT NULL
>> ORDER BY RANDOM()
>> LIMIT 1;
>>
>
> this works well on small tables. On large tables this query is extremely slow.

Exactly.  If you're running that query over and over your "performance
test" is on how well pgsql can run that very query. :)  Anything else
you do is likely to be noise by comparison.

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

Предыдущее
От: Pavel Stehule
Дата:
Сообщение: Re: Getting a random row
Следующее
От: Віталій Тимчишин
Дата:
Сообщение: Re: Getting a random row