pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedBest way to select a random row from a derived table
3+ messages / 3 participants
[nested] [flat]
* Best way to select a random row from a derived table
@ 2018-01-28 06:39 Ryan Murphy <ryanfmurphy@gmail.com>
2018-01-28 08:17 ` Re: Best way to select a random row from a derived table Fabien COELHO <coelho@cri.ensmp.fr>
2018-01-28 10:09 ` Re: Best way to select a random row from a derived table Condor <condor@stz-bg.com>
0 siblings, 2 replies; 3+ messages in thread
From: Ryan Murphy @ 2018-01-28 06:39 UTC (permalink / raw)
To: PostgreSQL mailing lists <pgsql-hackers@postgresql.org>; pgsql-general
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
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Best way to select a random row from a derived table
2018-01-28 06:39 Best way to select a random row from a derived table Ryan Murphy <ryanfmurphy@gmail.com>
@ 2018-01-28 08:17 ` Fabien COELHO <coelho@cri.ensmp.fr>
1 sibling, 0 replies; 3+ messages in thread
From: Fabien COELHO @ 2018-01-28 08:17 UTC (permalink / raw)
To: Ryan Murphy <ryanfmurphy@gmail.com>; +Cc: PostgreSQL mailing lists <pgsql-hackers@postgresql.org>; pgsql-general
> 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.
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Best way to select a random row from a derived table
2018-01-28 06:39 Best way to select a random row from a derived table Ryan Murphy <ryanfmurphy@gmail.com>
@ 2018-01-28 10:09 ` Condor <condor@stz-bg.com>
1 sibling, 0 replies; 3+ messages in thread
From: Condor @ 2018-01-28 10:09 UTC (permalink / raw)
To: Ryan Murphy <ryanfmurphy@gmail.com>; +Cc: PostgreSQL mailing lists <pgsql-hackers@postgresql.org>; pgsql-general
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
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2018-01-28 10:09 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-01-28 06:39 Best way to select a random row from a derived table Ryan Murphy <ryanfmurphy@gmail.com>
2018-01-28 08:17 ` Fabien COELHO <coelho@cri.ensmp.fr>
2018-01-28 10:09 ` Condor <condor@stz-bg.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox