MySQL get a random value between two values

前端 未结 5 1248
日久生厌
日久生厌 2020-12-03 01:01

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` [..]
         


        
5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-03 01:29

    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

提交回复
热议问题