Selecting random row

From: "Michal Taborsky" <michal(at)taborsky(dot)cz>
To: pgsql-general(at)postgresql(dot)org
Subject: Selecting random row
Date: 2002-08-02 12:38:44
Message-ID: aiduid$2e4m$1@news.hub.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi everyone,

I am facing a performance problem with selecting a random row from a table.
I do it like this:

SELECT * FROM table ORDER BY random() LIMIT 1

But it seems that the random() function is quite slow and backend apparently
evaluates the function for all rows (which is logical). On a table with few
thousand rows it takes seconds. Does anybody know a better solution to
selecting one (or more) random rows from a table ?

Thanks for any ideas,
Michal

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Mario Weilguni 2002-08-02 12:50:16 Re: very slow updates
Previous Message Elielson Fontanezi 2002-08-02 12:08:34 Locale Style - American numeric values to European numeric values