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 1efi9x-00065M-RH for pgsql-general@arkaria.postgresql.org; Sun, 28 Jan 2018 08:17:53 +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 1efi9w-0008G3-NP for pgsql-general@arkaria.postgresql.org; Sun, 28 Jan 2018 08:17:52 +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 1efi9w-0008Fp-Fb; Sun, 28 Jan 2018 08:17:52 +0000 Received: from smtp-3.mines-paristech.fr ([194.214.158.139] helo=cascavel.ensmp.fr) by magus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1efi9t-0004lZ-CV; Sun, 28 Jan 2018 08:17:51 +0000 Received: from cri.ensmp.fr (234.237.219.88.rev.sfr.net [88.219.237.234]) (authenticated bits=0) by cascavel.ensmp.fr (8.15.2/8.15.2/JMMC-22/Oct/2013) with ESMTPSA id w0S8HkTD009728 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Sun, 28 Jan 2018 09:17:46 +0100 Date: Sun, 28 Jan 2018 09:17:46 +0100 (CET) From: Fabien COELHO X-X-Sender: fabien@lancre 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: Message-ID: References: User-Agent: Alpine 2.20 (DEB 67 2015-01-07) MIME-Version: 1.0 Content-Type: multipart/mixed; BOUNDARY="8323329-1053042354-1517127467=:17596" X-Miltered: at cascavel.ensmp.fr with ID 5A6D872A.000 by Joe's j-chkmail (http : // j-chkmail dot ensmp dot fr)! X-j-chkmail-Auth: USER-ID coelho X-j-chkmail-Enveloppe: 5A6D872A.000 from 234.237.219.88.rev.sfr.net/234.237.219.88.rev.sfr.net/88.219.237.234/cri.ensmp.fr/ List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --8323329-1053042354-1517127467=:17596 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8BIT > 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? My 0.02€: I'd say this is not possible. In order to choose a item from a set randomly, you need to consider somehow the size of the set. Solution (2) can be done because the size of the table is known to TABLESAMPLE. Solution (1) does it by actually generating the set, hence the cost, so that its size is implicitely known as well. I cannot see a way out of this conundrum for a general query for which the size is not known without executing it. I'd like to be proven false, though:-) -- Fabien. --8323329-1053042354-1517127467=:17596--