Selecting random rows efficiently

From: Richard Jones <rj(at)last(dot)fm>
To: PgSQL Performance ML <pgsql-performance(at)postgresql(dot)org>
Subject: Selecting random rows efficiently
Date: 2003-08-30 13:09:03
Message-ID: 200308301309.03412.rj@last.fm
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-performance

Hi,
i have a table of around 3 million rows from which i regularly (twice a second
at the moment) need to select a random row from

currently i'm doing "order by rand() limit 1" - but i suspect this is
responsible for the large load on my db server - i guess that PG is doing far
too much work just to pick one row.

one way i can think of is to read in all the primary keys from my table, and
select one of the keys at random then directly fetch that row.

are there any other ways to do this? i need to keep the load down :)

Thanks,
Richard

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Rod Taylor 2003-08-30 13:58:12 Re: Hardware recommendations to scale to silly load
Previous Message Jeff 2003-08-30 13:08:51 Re: Selecting random rows efficiently

Browse pgsql-performance by date

  From Date Subject
Next Message Rob Nagler 2003-08-30 13:16:13 How to force Nested Loop plan?
Previous Message Jeff 2003-08-30 13:08:51 Re: Selecting random rows efficiently