可以安全地使用row_number()over(order by null)作为Oracle 11g中的rownum替代方法吗?

时间:2018-03-22 12:05:34

标签: oracle oracle11g row-number rownum

我做了一些测试,似乎row_number()over(order by null)可以用作oracle的rownum伪列的替代

select rownum, i.* from (
    select row_number() over (order by null) as rnum, i.*
    from c_invoice i
    order by i.dateinvoiced desc
) i;

这种行为是设计还是巧合?

1 个答案:

答案 0 :(得分:1)

我不推荐它。无法保证order by null会尊重外部order by,但实际上优化程序可能会这样做。

相反,你可以安全地做到:

select rownum, i.*
from (select row_number() over (order by i.dateinvoiced desc) as rnum, i.*
      from c_invoice i
      order by i.dateinvoiced desc
     ) i;

您可以检查查询执行计划,但我怀疑row_number()可能会增加一点开销。