哪个窗口函数更快?

发布于 2025-01-12 07:36:22 字数 611 浏览 0 评论 0原文

count(*) OVER (PARTITION BY a, b ORDER BY a, b, c) * 10

这会产生与以下内容相同的结果:

dense_rank() OVER (PARTITION BY a, b ORDER BY a, b, c) * 10

在这样的查询中使用:

SELECT
    dense_rank() OVER (ORDER BY a, b) ,
    a || b,
    count(*) OVER (
        PARTITION BY a, b
        ORDER BY a, b, c
    ) * 10 ,
    a2,
    b1,
    c1,
    cc1,
    c2,
FROM
  join ....
ORDER BY 1, 6;

我对查询结果感到满意。
但我应该欣赏其中一种方法而不是另一种方法吗?为什么?

count(*) OVER (PARTITION BY a, b ORDER BY a, b, c) * 10

This produces the same result as:

dense_rank() OVER (PARTITION BY a, b ORDER BY a, b, c) * 10

Used in a query like this:

SELECT
    dense_rank() OVER (ORDER BY a, b) ,
    a || b,
    count(*) OVER (
        PARTITION BY a, b
        ORDER BY a, b, c
    ) * 10 ,
    a2,
    b1,
    c1,
    cc1,
    c2,
FROM
  join ....
ORDER BY 1, 6;

I'm happy with my query result.
But should I appreciate one approach over the other and why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

渡你暖光 2025-01-19 07:36:22

PARTITION BY a, b 之后,将 ab 添加到 ORDER BY 是没有意义的,例如 大卫评论了

所以我们简化为:

count(*)     OVER (PARTITION BY a, b ORDER BY c) * 10
dense_rank() OVER (PARTITION BY a, b ORDER BY c) * 10

cUNIQUE时,这两个恰好是等价的。否则他们就不是。

您需要准确定义数字的含义,并显示表定义和准确的查询,因为联接可能会引入重复项和 NULL 值。

row_numer()rank() 类似 窗口函数 ...

它们的性能实际上是相同的。

After PARTITION BY a, b there is no point in adding aor b to ORDER BY, like David commented.

So we simplify to:

count(*)     OVER (PARTITION BY a, b ORDER BY c) * 10
dense_rank() OVER (PARTITION BY a, b ORDER BY c) * 10

These two only happen to be equivalent while c is UNIQUE. Else they are not.

You'd need to define exactly what the number is supposed to signify, and show your table definition, and the exact query because joins can introduce duplicates and NULL values.

row_numer() or rank() are similar window functions ...

Performance is practically the same for all of them.

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