哪个窗口函数更快?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
PARTITION BY a, b
之后,将a
或b
添加到ORDER BY
是没有意义的,例如 大卫评论了。所以我们简化为:
当
c
是UNIQUE
时,这两个恰好是等价的。否则他们就不是。您需要准确定义数字的含义,并显示表定义和准确的查询,因为联接可能会引入重复项和 NULL 值。
row_numer()
或rank()
类似 窗口函数 ...它们的性能实际上是相同的。
After
PARTITION BY a, b
there is no point in addinga
orb
toORDER BY
, like David commented.So we simplify to:
These two only happen to be equivalent while
c
isUNIQUE
. 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()
orrank()
are similar window functions ...Performance is practically the same for all of them.