Best way to select random rows PostgreSQL

前端 未结 12 1206
借酒劲吻你
借酒劲吻你 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:44

    select * from table order by random() limit 1000;
    

    If you know how many rows you want, check out tsm_system_rows.

    tsm_system_rows

    module provides the table sampling method SYSTEM_ROWS, which can be used in the TABLESAMPLE clause of a SELECT command.

    This table sampling method accepts a single integer argument that is the maximum number of rows to read. The resulting sample will always contain exactly that many rows, unless the table does not contain enough rows, in which case the whole table is selected. Like the built-in SYSTEM sampling method, SYSTEM_ROWS performs block-level sampling, so that the sample is not completely random but may be subject to clustering effects, especially if only a small number of rows are requested.

    First install the extension

    CREATE EXTENSION tsm_system_rows;
    

    Then your query,

    SELECT *
    FROM table
    TABLESAMPLE SYSTEM_ROWS(1000);
    

提交回复
热议问题