MySQL select 10 random rows from 600K rows fast

后端 未结 26 2921
粉色の甜心
粉色の甜心 2020-11-21 05:06

How can I best write a query that selects 10 rows randomly from a total of 600k?

26条回答
  •  说谎
    说谎 (楼主)
    2020-11-21 05:24

    How to select random rows from a table:

    From here: Select random rows in MySQL

    A quick improvement over "table scan" is to use the index to pick up random ids.

    SELECT *
    FROM random, (
            SELECT id AS sid
            FROM random
            ORDER BY RAND( )
            LIMIT 10
        ) tmp
    WHERE random.id = tmp.sid;
    

提交回复
热议问题