【问题标题】:select over partiton by ... order by .. used long timeselect over partition by ... order by .. used long time
【发布时间】:2021-06-28 10:21:52
【问题描述】:
select a.id
from (select /*+index(test_table, test_index)*/ 
             row_number() over (partition by a, b, c order by d desc) rn,
             id
      from test_table
     ) a
where a.rn = 1

test_index(a, b, c, d) 
limit access to 500, cost 9s) 

我应该如何解决它

【问题讨论】:

  • 您要解决什么问题?请澄清您的问题:描述您目前的情况以及它到底出了什么问题,添加minimal reproducible example 或至少一个query plan 以及您已经执行了哪些步骤来解决问题。
  • 我会先删除该提示。没有针对您的 test_table 的 where 子句,因此全表扫描可能比强制它使用索引更快(除非您的索引实际上包含所有列,并且至少其中一个列不可为空)。索引扫描并不总是好的,全表扫描也不总是坏的。

标签: sql oracle analytic-functions


【解决方案1】:

出于好奇,使用相关子查询需要多长时间?

select t.id
from test_table t
where t.d = (select max(t2.d)
             from test_table t2
             where t2.a = t.a and t2.b = t.b and t2.c = t.c
            );

还是使用聚合?

select max(t.id) keep (dense_rank first order by d desc)
from test_table t
group by a, b, c;

【讨论】:

  • 我有测试选择 max(t.id) keep (dense_rank first order by d desc) from test_table t group by a, b, c;花费4秒
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-21
  • 2011-10-09
相关资源
最近更新 更多