Re: Best way to select a random row from a derived table

From: Fabien COELHO <coelho(at)cri(dot)ensmp(dot)fr>
To: Ryan Murphy <ryanfmurphy(at)gmail(dot)com>
Cc: PostgreSQL mailing lists <pgsql-hackers(at)postgresql(dot)org>, pgsql-general(at)postgresql(dot)org
Subject: Re: Best way to select a random row from a derived table
Date: 2018-01-28 08:17:46
Message-ID: alpine.DEB.2.20.1801280911470.17596@lancre
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers


> 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.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Condor 2018-01-28 10:09:35 Re: Best way to select a random row from a derived table
Previous Message Ryan Murphy 2018-01-28 06:39:33 Best way to select a random row from a derived table

Browse pgsql-hackers by date

  From Date Subject
Next Message Condor 2018-01-28 10:09:35 Re: Best way to select a random row from a derived table
Previous Message Fabien COELHO 2018-01-28 08:10:11 Re: General purpose hashing func in pgbench