X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id ED120D1CA0D for ; Thu, 11 Mar 2004 06:40:16 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 33313-04 for ; Thu, 11 Mar 2004 02:40:11 -0400 (AST) Received: from sss.pgh.pa.us (unknown [192.204.191.242]) by svr1.postgresql.org (Postfix) with ESMTP id B8708D1BB52 for ; Thu, 11 Mar 2004 02:40:09 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.11/8.12.11) with ESMTP id i2B6e6H1016600; Thu, 11 Mar 2004 01:40:06 -0500 (EST) To: Bruno Wolff III Cc: Enver ALTIN , pgsql-sql@postgresql.org Subject: Re: randomized order in select? In-reply-to: <20040310190250.GA6629@wolff.to> References: <1078937297.29628.12.camel@enver.office.frontsite.com.tr> <20040310190250.GA6629@wolff.to> Comments: In-reply-to Bruno Wolff III message dated "Wed, 10 Mar 2004 13:02:50 -0600" Date: Thu, 11 Mar 2004 01:40:06 -0500 Message-ID: <16599.1078987206@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200403/128 X-Sequence-Number: 17227 Bruno Wolff III writes: > Enver ALTIN 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