MsSQL NEWID 是否像 MySQL Rand() 一样不好

发布于 2024-10-28 19:51:58 字数 434 浏览 6 评论 0原文

根据这篇文章: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

西瓜 2024-11-04 19:51:58

以下是 MySQLRAND() 调用的代码:

double my_rnd(struct rand_struct *rand_st)
{
  rand_st->seed1=(rand_st->seed1*3+rand_st->seed2) % rand_st->max_value;
  rand_st->seed2=(rand_st->seed1+rand_st->seed2+33) % rand_st->max_value;
  return (((double) rand_st->seed1)/rand_st->max_value_dbl);
}

您可以评估一些间接、加法和取模所需的处理能力。

ORDER BY RAND() 的真正问题是它需要对(可能很大的)随机数数组进行排序,而不是计算它们。

当然,SQL Server 也存在同样的问题。

MySQL中,您可以避免对随机记录进行排序:

但是,此解决方案在 SQL Server 中不起作用,因为它不支持会话变量。

Here's the code invoked by MySQL's RAND():

double my_rnd(struct rand_struct *rand_st)
{
  rand_st->seed1=(rand_st->seed1*3+rand_st->seed2) % rand_st->max_value;
  rand_st->seed2=(rand_st->seed1+rand_st->seed2+33) % rand_st->max_value;
  return (((double) rand_st->seed1)/rand_st->max_value_dbl);
}

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.

ゞ花落谁相伴 2024-11-04 19:51:58

这是一篇关于如何选择随机行的优秀文章,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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文