我做了一些测试,似乎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;
这种行为是设计还是巧合?
答案 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()
可能会增加一点开销。