dt3999 2012-05-15 02:57
浏览 73
已采纳

ORDER BY RAND()的性能命中率? [重复]

Possible Duplicate:
MySQL: Alternatives to ORDER BY RAND()

I'm pulling about 20 random rows, most or all with WHERE clauses attached, in a table of 1000~2000 records. This won't grow by much, maybe 150 every year.

I'm also looking at a few thousand users at most, but with this query being part of a popular area of the site.

Do I really need to be concerned about performance hits where ORDER BY RAND() is concerned? I've heard all the horror stories about performance issues, but I haven't found much when such a small table is being hit constantly.

I can't do a cache for this.

  • 写回答

1条回答 默认 最新

  • dongyutan1703 2012-05-15 03:03
    关注

    The ORDER BY RAND() is easily used and forgotten, but you have to realize that MySQL needs to inspect every row because RAND() is not constant.

    Inspecting 1000 rows shouldn't be a problem, but keep an eye on the query performance over time. If you just needed one record, you could have found an answer here: ORDER BY RAND() alternative

    Edit

    You said you can't do caching, but even a small generated .php file is a cache :)

    For instance, you can store a list of ID's in a cache; you update it every time you insert or remove a database record. Then, you pull 20 random ID's using PHP and use those to query the database (using e.g. IN (4, 7, 10, 45, etc.)

    Update

    As pointed out by OP, this is not possible if the query also has conditions (i.e. not every item in the table is eligable to be picked).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺