dongzheng8463 2014-10-11 10:42
浏览 82
已采纳

ORDER BY RAND()函数需要很长时间才能在mysql中执行[复制]

This question already has an answer here:

I have to use RAND function in MYSQL query. If I use this function in sql query then this take near about 0.7962 sec. But if I use it without then this work fine with 0.0009 sec. How I can make sql query faster with RAND function.

MY QUERY

  SELECT 
         posts.ID,
         posts.post_content, 
         posts.post_title, 
         posts.post_date, 
         posts.post_name 
    FROM posts 
         WHERE posts.post_type = 'post' 
         AND posts.post_status = 'publish' 
    ORDER BY RAND() LIMIT 0, 24 
</div>
  • 写回答

4条回答 默认 最新

  • dongxin0031 2014-10-11 11:06
    关注

    I go the solution.

     SELECT p1.ID, p1.post_content, p1.post_title, p1.post_date, p1.post_name
         FROM posts as p1 JOIN
              (SELECT CEIL(RAND() *
                     (SELECT MAX(ID)
                        FROM posts)) AS id)
              AS p2
      WHERE p1.ID >= p2.id
      ORDER BY p1.ID ASC
      LIMIT 0, 24
    

    This is faster than my query.

    MySQL select 10 random rows from 600K rows fast

    Here is the solution.

    Thanks

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

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献