MySQL select 10 random rows from 600K rows fast

后端 未结 26 2934
粉色の甜心
粉色の甜心 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:23

    This is how I do it:

    select * 
    from table_with_600k_rows
    where rand() < 10/600000
    limit 10
    

    I like it because does not require other tables, it is simple to write, and it is very fast to execute.

提交回复
热议问题