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

前端 未结 24 1759
旧时难觅i
旧时难觅i 2020-11-22 09:30

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.

24条回答
  •  悲哀的现实
    2020-11-22 10:07

    I see here a lot of solution. One or two seems ok but other solutions have some constraints. But the following solution will work for all situation

    select a.* from random_data a, (select max(id)*rand() randid  from random_data) b
         where a.id >= b.randid limit 1;
    

    Here, id, don't need to be sequential. It could be any primary key/unique/auto increment column. Please see the following Fastest way to select a random row from a big MySQL table

    Thanks Zillur - www.techinfobest.com

提交回复
热议问题