是否有“row_number()over(按col1顺序按col2划分)”的替代?

时间:2014-01-21 23:52:01

标签: oracle

我正在使用oracle的分区。我看到更好的I / O和更少的执行时间。但我看到CPU飙升。 在下面提供的场景中是否存在对分区的修改? SQL查询:

SELECT C2, ROW_NUMBER() OVER(PARTITION BY C2 ORDER BY C3) AS DISPLAY_ORDER 
  FROM MYTABLE;

表数据:

C1, C2, C3
1, 1000, 101
2, 1000, 102
3, 1000, 200
4, 1000, 300
5, 1001, 100 
6, 1001, 101
7, 1001, 102
8, 1001, 103

输出:

C1, C2, C3
1, 1000, 1
2, 1000, 2
3, 1000, 3
4, 1000, 4
5, 1001, 1 
6, 1001, 2
7, 1001, 3
8, 1001, 4

2 个答案:

答案 0 :(得分:1)

如果您的数据返回相同的结果(在您发布的样本上),则可以使用DENSE_RANKRANK

with t as 
(select 1 C1, 1000 C2, 101 C3 from dual
union all
select 2, 1000, 102 from dual
union all
select 3, 1000, 200 from dual
union all
select 4, 1000, 300 from dual
union all
select 5, 1001, 100 from dual
union all
select 6, 1001, 101 from dual
union all
select 7, 1001, 102 from dual
union all
select 8, 1001, 103 from dual)
select C1,
       C2,
       ROW_NUMBER() OVER(PARTITION BY C2 ORDER BY C3) AS DISPLAY_ORDER1,
       RANK() OVER(PARTITION BY C2 ORDER BY C3) AS DISPLAY_ORDER2,
       DENSE_RANK() OVER(PARTITION BY C2 ORDER BY C3) AS DISPLAY_ORDER3
  from t;

    C1         C2     DISPLAY_ORDER1 DISPLAY_ORDER2 DISPLAY_ORDER3
---------- ---------- -------------- -------------- --------------
     1       1000              1              1              1
     2       1000              2              2              2
     3       1000              3              3              3
     4       1000              4              4              4
     5       1001              1              1              1
     6       1001              2              2              2
     7       1001              3              3              3
     8       1001              4              4              4

forum thread表示效果如下:dense_rank (fastest) > rank > row_number (slowest)。但是你最好根据自己的数据来比较所有这些数据的性能。

答案 1 :(得分:0)

使用oracle分析功能是最有效的方法。 感谢大家的投入