I want a random selection of rows in PostgreSQL, I tried this:
select * from table where random() < 0.01;
But some other recommend this:
Add a column called r
with type serial
. Index r
.
Assume we have 200,000 rows, we are going to generate a random number n
, where 0 < n
<= 200, 000.
Select rows with r > n
, sort them ASC
and select the smallest one.
Code:
select * from YOUR_TABLE
where r > (
select (
select reltuples::bigint AS estimate
from pg_class
where oid = 'public.YOUR_TABLE'::regclass) * random()
)
order by r asc limit(1);
The code is self-explanatory. The subquery in the middle is used to quickly estimate the table row counts from https://stackoverflow.com/a/7945274/1271094 .
In application level you need to execute the statement again if n
> the number of rows or need to select multiple rows.