I have a big table in my database (potentially millions of records) and I need to select #X random rows (let's say #X between 10 and 50) , but I need this query to be as optimal as possible.
The table looks like this:
CREATE TABLE sample (
id bigint auto_increment PRIMARY KEY,
user_id bigint NOT NULL,
screen_name VARCHAR NOT NULL,
...
);
I've searched around and I found answers like this:
SELECT * FROM sample ORDER BY RAND() limit X.
But It looks to me that this will fetch the full table then sort it, isn't it?
I think it would be best to generate 10 or 50 random integers and do a select * from sample where rowid in (<random integer list>)
. But afaik, rowid concept is missing in H2, so I may opt for using the ID column in my table.
It would be awesome if I can do this task with a single SQL query.
Any better advice?