pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Bruno Wolff III <bruno@wolff.to>
Cc: Enver ALTIN <enver.altin@frontsite.com.tr>
Cc: pgsql-sql@postgresql.org
Subject: Re: randomized order in select?
Date: Thu, 11 Mar 2004 01:40:06 -0500
Message-ID: <16599.1078987206@sss.pgh.pa.us> (raw)
In-Reply-To: <20040310190250.GA6629@wolff.to>
References: <1078937297.29628.12.camel@enver.office.frontsite.com.tr>
	<20040310190250.GA6629@wolff.to>

Bruno Wolff III <bruno@wolff.to> writes:
>   Enver ALTIN <enver.altin@frontsite.com.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



view thread (7+ messages)  latest in thread

Message-ID: <16599.1078987206@sss.pgh.pa.us>
Permalink:  ../16599.1078987206@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/16599.1078987206@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, bruno@wolff.to, enver.altin@frontsite.com.tr
  Subject: Re: randomized order in select?
  In-Reply-To: <16599.1078987206@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox