pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Jones <rj@last.fm>
To: PgSQL Performance ML <pgsql-performance@postgresql.org>
Subject: Selecting random rows efficiently
Date: Sat, 30 Aug 2003 13:09:03 +0000
Message-ID: <200308301309.03412.rj@last.fm> (raw)
In-Reply-To: <1062220877.669.406.camel@haggis>
References: <200308300244.h7U2i4S12751@candle.pha.pa.us>
	<1062220877.669.406.camel@haggis>

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 



view thread (10+ messages)  latest in thread

Message-ID: <200308301309.03412.rj@last.fm>
Permalink:  ../200308301309.03412.rj@last.fm/
Also on:    postgresql.org/message-id/200308301309.03412.rj@last.fm

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-performance@postgresql.org
  Cc: rj@last.fm
  Subject: Re: Selecting random rows efficiently
  In-Reply-To: <200308301309.03412.rj@last.fm>
  References: <200308300244.h7U2i4S12751@candle.pha.pa.us> <1062220877.669.406.camel@haggis> <200308301309.03412.rj@last.fm>

* 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