Best way to select random rows PostgreSQL

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

    I know I'm a little late to the party, but I just found this awesome tool called pg_sample:

    pg_sample - extract a small, sample dataset from a larger PostgreSQL database while maintaining referential integrity.

    I tried this with a 350M rows database and it was really fast, don't know about the randomness.

    ./pg_sample --limit="small_table = *" --limit="large_table = 100000" -U postgres source_db | psql -U postgres target_db
    

提交回复
热议问题