Re: randomized order in select?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Bruno Wolff III <bruno(at)wolff(dot)to>
Cc: Enver ALTIN <enver(dot)altin(at)frontsite(dot)com(dot)tr>, pgsql-sql(at)postgresql(dot)org
Subject: Re: randomized order in select?
Date: 2004-03-11 06:40:06
Message-ID: 16599.1078987206@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Bruno Wolff III <bruno(at)wolff(dot)to> writes:
> Enver ALTIN <enver(dot)altin(at)frontsite(dot)com(dot)tr> wrote:
>> I wish I could do something like:
>> select tiptext from tips order by random limit 1
>> in PostgreSQL.

> You can but it won't be very efficient (for large tables) as it will generate
> a random ordering for the whole table, probably do a sort and then return the
> first record. The only thing different you need to do is add () after random:
> select tiptext from tips order by random() limit 1

If you do need to do this for a large table, I seem to recall that
we've previously worked out reasonable ways to select a random entry
efficiently using an index. Dig around in the mail list archives for
details.

regards, tom lane

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Oleg Bartunov 2004-03-11 09:01:37 Re: Tsearch2 question: getting histogram of the vector elements
Previous Message Iain 2004-03-11 05:31:26 Re: randomized order in select?