MsSQL NEWID 是否像 MySQL Rand() 一样不好
根据这篇文章: http://net.tutsplus.com /tutorials/other/top-20-mysql-best-practices/ 使用 ORDER BY RAND() 是一个坏主意,因为:
问题是,MySQL 必须对表中的每一行执行 RAND() 操作(这需要处理能力),然后再对其进行排序并只给出 1 行。
现在,工作中的一些家伙最近发现,在使用 MsSQL 时可以使用 ORDER BY NEWID() (我对此不太了解)。现在,我的问题是,使用 NEWID() 和使用 RAND() 一样都是一个坏主意 - 如果是这样,那么应该如何做?
according to this article: http://net.tutsplus.com/tutorials/other/top-20-mysql-best-practices/
using ORDER BY RAND() is a bad idea because:
The problem is, MySQL will have to perform RAND() operation (which takes processing power) for every single row in the table before sorting it and giving you just 1 row.
Now some dude from work, recently discovered that you can use ORDER BY NEWID() when using MsSQL (which i do not know much about). Now, my question is, is it just as much a bad idea using NEWID() as it is using RAND() - if so, how should it be done then?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是
MySQL
的RAND()
调用的代码:您可以评估一些间接、加法和取模所需的处理能力。
ORDER BY RAND() 的真正问题是它需要对(可能很大的)随机数数组进行排序,而不是计算它们。
当然,
SQL Server
也存在同样的问题。在
MySQL
中,您可以避免对随机记录进行排序:但是,此解决方案在
SQL Server
中不起作用,因为它不支持会话变量。Here's the code invoked by
MySQL
'sRAND()
:You can evaluate the processing power it takes for a couple of indirections, additions and moduli.
The real problem with
ORDER BY RAND()
is that it need to sort on the (possibly huge) array of randoms, not to calculate them.The same problems holds for
SQL Server
of course.In
MySQL
, you can avoid sorting for sampling random records:However, this solution won't work in
SQL Server
since it does not support session variables.这是一篇关于如何选择随机行的优秀文章,MSSQL 和 MySQL 的伪代码:改进按 rand 排序和按 newid 排序
Here is an excellent article on how to select random rows, pseudo code for both MSSQL and MySQL: Improving Order by rand and order by newid