关于oracle中使用rownum的问题

2025-05-15 14:17:29
推荐回答(1个)
回答(1):

除了索引组织表(IOT),其他的表存储都是堆表(Heap表)是无序的,查询出的数据不一定就是入库顺序,切记
所以 用rownum 最好加上order by
解决方案:
查询:
select * from table_a where
rowid in ( select rowid from table_a where rownum <= 2000 order by id)
删除:
delete from table_a where
rowid in ( select rowid from table_a where rownum <= 2000 order by id)
这里按照id 排序,因为id是有索引的,索引本身就是有序的,所以 order by id 不会产生性能问题