X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 8CFE1D1B8B5 for ; Sat, 30 Aug 2003 14:04:10 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 33566-07 for ; Sat, 30 Aug 2003 11:04:01 -0300 (ADT) Received: from lakemtao04.cox.net (lakemtao04.cox.net [68.1.17.241]) by svr1.postgresql.org (Postfix) with ESMTP id 7E6F5D1B8AA for ; Sat, 30 Aug 2003 11:03:59 -0300 (ADT) Received: from lhosts ([68.11.66.83]) by lakemtao04.cox.net (InterMail vM.5.01.06.04 201-253-122-130-104-20030726) with ESMTP id <20030830140401.CDNM419.lakemtao04.cox.net@lhosts> for ; Sat, 30 Aug 2003 10:04:01 -0400 Subject: Re: Selecting random rows efficiently From: Ron Johnson To: PgSQL Performance ML 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> Content-Type: text/plain Message-Id: <1062252240.667.453.camel@haggis> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.4 Date: Sat, 30 Aug 2003 09:04:01 -0500 Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200308/530 X-Sequence-Number: 3346 On Sat, 2003-08-30 at 08:09, Richard Jones wrote: > 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. What datatype is the selected by key? Also, where is rand() defined? Is that a UDF? Could it be that there is a type mismatch? > 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 Are you really in Micronesia? -- ----------------------------------------------------------------- Ron Johnson, Jr. ron.l.johnson@cox.net Jefferson, LA USA "The greatest dangers to liberty lurk in insidious encroachment by men of zeal, well-meaning, but without understanding." Justice Louis Brandeis, dissenting, Olmstead v US (1928)