撒拉嘿哟木头 2010-12-01 21:35 采纳率: 100%
浏览 312
已采纳

从600K 行中随机选择10行,速度很快

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

转载于:https://stackoverflow.com/questions/4329396/mysql-select-10-random-rows-from-600k-rows-fast

  • 写回答

22条回答 默认 最新

  • 斗士狗 2010-12-01 21:41
    关注

    A great post handling several cases, from simple, to gaps, to non-uniform with gaps.

    http://jan.kneschke.de/projects/mysql/order-by-rand/

    For most general case, here is how you do it:

    SELECT name
      FROM random AS r1 JOIN
           (SELECT CEIL(RAND() *
                         (SELECT MAX(id)
                            FROM random)) AS id)
            AS r2
     WHERE r1.id >= r2.id
     ORDER BY r1.id ASC
     LIMIT 1
    

    This supposes that the distribution of ids is equal, and that there can be gaps in the id list. See the article for more advanced examples

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(21条)

报告相同问题?

悬赏问题

  • ¥15 拓扑学,凸集,紧集。。
  • ¥15 如何扩大AIS数据容量
  • ¥15 单纯型python实现编译报错
  • ¥15 c++2013读写oracle
  • ¥15 c++ gmssl sm2验签demo
  • ¥15 关于模的完全剩余系(关键词-数学方法)
  • ¥15 有没有人懂这个博图程序怎么写,还要跟SFB连接,真的不会,求帮助
  • ¥15 PVE8.2.7无法成功使用a5000的vGPU,什么原因
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音