Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1efjuH-0005GM-ER for pgsql-general@arkaria.postgresql.org; Sun, 28 Jan 2018 10:09:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1efjuF-00050R-Si for pgsql-general@arkaria.postgresql.org; Sun, 28 Jan 2018 10:09:47 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1efjuF-00050D-GZ; Sun, 28 Jan 2018 10:09:47 +0000 Received: from stz-bg.com ([46.40.123.212] helo=mail.stzbg.com) by magus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1efjuC-00074p-4S; Sun, 28 Jan 2018 10:09:46 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.stzbg.com (Postfix) with ESMTP id A8B872E0857; Sun, 28 Jan 2018 12:09:42 +0200 (EET) Authentication-Results: mail.stzbg.com (amavisd-new); dkim=pass (1024-bit key) reason="pass (just generated, assumed good)" header.d=stz-bg.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=stz-bg.com; h= user-agent:message-id:references:in-reply-to:subject:subject :from:from:date:date:content-transfer-encoding:content-type :content-type:mime-version; s=mail; t=1517134176; x=1518948577; bh=U9DePtpFpWXJsZyxlUI6j1CTK1lRTvdRbdcLpMTpScY=; b=JyoKm41C8PYm 8Smi6sY8v+OOOWVProVtP+J5+7wxryrIhlVC0JtWg+qQcqy0zEwBsE3H7nBbI2O8 5qZ12RVfVxn5ZAkSlYEzabSzSoE0cnYQjiP7VWIbZBcicq5ktmWGdz9Dk0wgyJ+n 7RAEmpuBQNR5pXnN/YIpvcb9u661FKc= X-Virus-Scanned: amavisd-new at stzbg.com Received: from mail.stzbg.com ([127.0.0.1]) by localhost (mail.stzbg.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id HYd0hcmFtNqd; Sun, 28 Jan 2018 12:09:36 +0200 (EET) Received: from www.stz-bg.com (stz-bg.com [IPv6:2a02:6800:0:99::2]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: condor@stz-bg.com) by mail.stzbg.com (Postfix) with ESMTPSA id EF70F2E1608; Sun, 28 Jan 2018 12:09:35 +0200 (EET) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit Date: Sun, 28 Jan 2018 12:09:35 +0200 From: Condor To: Ryan Murphy Cc: PostgreSQL mailing lists , pgsql-general@postgresql.org Subject: Re: Best way to select a random row from a derived table In-Reply-To: References: Message-ID: <9289a7a28f356e9b27ae2e93f4551a86@stz-bg.com> X-Sender: condor@stz-bg.com User-Agent: Roundcube Webmail/1.3.4 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 28-01-2018 08:39, Ryan Murphy wrote: > Hello hackers and postgressers, > > I am aware of 2 ways to select a random row from a table: > > 1) select * from table_name order by random() limit 1; > -- terribly inefficient > > 2) select * from table_name tablesample system_rows(1) limit 1; > -- only works on tables, not views or subqueries > > Is there an option that is reasonably efficient and can be used on > views and subqueries? > > Thanks! > Ryan I do it with: SELECT * FROM table_name OFFSET RANDOM() * LIMIT 1; Regards, HC