Best way to select random rows PostgreSQL

前端 未结 12 990
借酒劲吻你
借酒劲吻你 2020-11-22 06:57

I want a random selection of rows in PostgreSQL, I tried this:

select * from table where random() < 0.01;

But some other recommend this:

12条回答
  •  伪装坚强ぢ
    2020-11-22 07:24

    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.

提交回复
热议问题