Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

quick selection of a random row from a large table in mysql

What is a fast way to select a random row from a large mysql table?

I'm working in php, but I'm interested in any solution even if it's in another language.

like image 926
lajos Avatar asked Oct 17 '08 07:10

lajos


People also ask

How do you select a single random row from a table?

To get a single row randomly, we can use the LIMIT Clause and set to only one row. ORDER BY clause in the query is used to order the row(s) randomly. It is exactly the same as MYSQL. Just replace RAND( ) with RANDOM( ).

How do I select a random row by group in SQL?

Now let's find how to do Random Sampling within Groups in SQL using RAND() function. Below SQL statement is to display rows in random order using RAND() function: Query: SELECT * FROM table_name order by RANDOM();

How do I select multiple rows in mysql?

To select last two rows, use ORDER BY DESC LIMIT 2.


2 Answers

Grab all the id's, pick a random one from it, and retrieve the full row.

If you know the id's are sequential without holes, you can just grab the max and calculate a random id.

If there are holes here and there but mostly sequential values, and you don't care about a slightly skewed randomness, grab the max value, calculate an id, and select the first row with an id equal to or above the one you calculated. The reason for the skewing is that id's following such holes will have a higher chance of being picked than ones that follow another id.

If you order by random, you're going to have a terrible table-scan on your hands, and the word quick doesn't apply to such a solution.

Don't do that, nor should you order by a GUID, it has the same problem.

like image 54
Lasse V. Karlsen Avatar answered Sep 23 '22 09:09

Lasse V. Karlsen


I knew there had to be a way to do it in a single query in a fast way. And here it is:

A fast way without involvement of external code, kudos to

http://jan.kneschke.de/projects/mysql/order-by-rand/

SELECT name   FROM random AS r1 JOIN        (SELECT (RAND() *                      (SELECT MAX(id)                         FROM random)) AS id)         AS r2  WHERE r1.id >= r2.id  ORDER BY r1.id ASC  LIMIT 1; 
like image 39
Vinko Vrsalovic Avatar answered Sep 22 '22 09:09

Vinko Vrsalovic