从 PARTITION BY 子句中删除 ORDER BY 子句?
有没有办法减少下面内部查询的 OVER 部分中“ORDER BY lro_pid”子句的影响?
SELECT *
FROM (SELECT a.*,
Row_Number() over (PARTITION BY search_point_type
ORDER BY lro_pid) spt_rank
FROM lro_search_point a
ORDER BY spt_rank)
WHERE spt_rank = 1;
我不在乎在分区内对这个结果进行排序,因为我想通过完全不同的变量对其进行排序。 lro_pid 是一个索引列,但目前看来这仍然是浪费资源。 (也许有一种方法可以将排序限制在单行的范围内?希望根本不会花费时间/精力在分区内进行排序)
Is there a way I can reduce the impact of the 'ORDER BY lro_pid' clause in the OVER portion of the inner query below?
SELECT *
FROM (SELECT a.*,
Row_Number() over (PARTITION BY search_point_type
ORDER BY lro_pid) spt_rank
FROM lro_search_point a
ORDER BY spt_rank)
WHERE spt_rank = 1;
I don't care to order this result within the partition since I want to order it by a different variable entirely. lro_pid is an indexed column, but this still seems like a waste of resources as it currently stands. (Perhaps there is a way to limit the ordering to a range of a single row?? Hopefully no time/energy would be spent on sorting within the partition at all)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有几点需要尝试:
您可以在 OVER 子句中使用
ORDER BY 'constant'
吗?如果不允许按常量排序,那么
ORDER BY (lro_pid * 0)
怎么样?我不是 Oracle 专家(MSSQL 更适合我) - 因此有问题来回答您的问题!
A couple of things to try:
Can you e.g.
ORDER BY 'constant'
in the OVER clause?If ordering by a constant is not permitted, how about
ORDER BY (lro_pid * 0)
?I'm not an Oracle expert (MSSQL is more my thing) - hence questions to answer your question!
在分析 ORDER BY 中使用常量(如 @Will A 建议)似乎是最快的方法。
优化器仍然执行排序,但比对列进行排序要快。
另外,您可能想要删除第二个 ORDER BY,或者至少将其移至外部查询。
下面是我的测试用例:
Using a constant in the analytic ORDER BY as @Will A suggested appears to be the fastest method.
The optimizer still performs a sort, but it's faster than sorting a column.
Also, you probably want to remove the second ORDER BY, or at least move it to the outer query.
Below is my test case:
要省略子句
ORDER BY
,您可以使用ORDER BY
rownum。To obmit the clause
ORDER BY
you could useORDER BY
rownum.