Of course, this is a bad idea if you have a table with lots of rows, and you'd be better off rolling an N-sided dice M times, where N is the number of rows in the table.
Of course, this is a bad idea if you have a table with lots of rows, and you'd be better off rolling an N-sided dice M times, where N is the number of rows in the table.
This is the plan I get:
Limit (cost=56.39..56.41 rows=10 width=40)
-> Sort (cost=56.39..59.79 rows=1360 width=40)
Sort Key: (random())
-> Seq Scan on test (cost=0.00..27.00 rows=1360 width=40)
One should use "FROM table TABLESAMPLE SYSTEM (size)", this is the plan: Sample Scan on test (cost=0.00..5.36 rows=136 width=32)
Sampling: system ('10'::real)
You can also use different distribution method than "SYSTEM", and you can make it reproducible with a seed.https://www.postgresql.org/docs/current/sql-select.html#SQL-...
ORDER BY random() is a really un-optimized bad practice to get a random sample on PostgreSQL. If you use another SQL database, the best thing to do is to use UUIDv4 as primary key, and use "ORDER BY pk LIMIT size".
> The SYSTEM method does block-level sampling with each block having the specified chance of being selected; all rows in each selected block are returned.
What if I want a different random order tomorrow?
> What if I want a different random order tomorrow?
If your data is large, and you want different samples. You can always always "select * from pk > gen_random_uuid() order by pk limit 10"
You have a very small chance to get ffffffff-... and get zero elements.
This is not perfect, I admit, but ORDER BY random() is one of the most wasteful thing.
If getting random samples is very important, the best you can you is migrate to postgres and use TABLESAMPLE.
Er, no, it will assign values to the result set, which may not involve a full table scan.
Also, any case of "WHERE filter ORDER BY random() LIMIT n" means "I want a tiny sample from a broad filter". So it might not be a full table scan, but it will still assign value to a lot of rows.
And that has nothing to do with the ORDER BY random().
Just a side note: TABLESAMPLE is part of the SQL standard, and e.g. Oracle supports this as well.
I guess some projects need to randomly select a few rows from a table? I was thinking of the wikipedia random topic, but that's only one-at-a-time, not a set of records.