Re: Getting a random row - Mailing list pgsql-performance

From Pavel Stehule
Subject Re: Getting a random row
Date
Msg-id 162867790910140020h34842dfdma733a3085d226d4e@mail.gmail.com
Whole thread Raw
In response to Re: Getting a random row  (Thom Brown <thombrown@gmail.com>)
Responses Re: Getting a random row  (Scott Marlowe <scott.marlowe@gmail.com>)
List pgsql-performance
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.

regards
Pavel

> Thom
>
> --
> Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-performance
>

pgsql-performance by date:

Previous
From: Thom Brown
Date:
Subject: Re: Getting a random row
Next
From: Scott Marlowe
Date:
Subject: Re: Getting a random row