I have two columns in a row: min_value, max_value. Is there a way to do a select like:
SELECT RAND(`min_v`, `max_v`) `foo` [..]
Depending on how many rows you have in the table(s), using rand() in a query or subquery can be extremely slow.
You can seriously improve the speed by first putting the random value in a variable and then just using that in your query.
For example on a table with over 4 million rows...
This took over 10 minutes:
SELECT
*
FROM
`customers` `Customer`
WHERE
`id` = (
SELECT
FLOOR((RAND() * (max(`CustomerRand`.`id`) - min(`CustomerRand`.`id`) + 1)) + min(`CustomerRand`.`id`)) `random_id`
FROM
`customers` `CustomerRand`
);
While this took about 3 seconds on average:
SELECT
FLOOR((RAND() * (max(`CustomerRand`.`id`) - min(`CustomerRand`.`id`) + 1)) + min(`CustomerRand`.`id`)) `random_id`
FROM `customers` `CustomerRand` INTO @rand_id;
SELECT * FROM `customers` WHERE `id` = @rand_id;
You might even be able to put this into a stored procedure then if it's something you would want to do or re-use often. The stored procedure could then be called from PHP or Python or wherever