问题是,有没有办法限制 ORDERED 行的数量
在子查询中返回?
以下是我通常用于 top-n 类型查询(在本例中为分页查询):
select * from (
select a.*, rownum r
from (
select *
from your_table
where ...
order by ...
) a
where rownum <= :upperBound
)
where r >= :lowerBound;
我通常在内部查询中使用索引列进行排序,使用rownum意味着Oracle可以使用count(stopkey)优化。所以,不一定要做全表扫描:
create table t3 as select * from all_objects;
alter table t3 add constraint t_pk primary key(object_id);
analyze table t3 compute statistics;
delete from plan_table;
commit;
explain plan for
select * from (
select a.*, rownum r
from (
select object_id, object_name
from t3
order by object_id
) a
where rownum <= 2000
)
where r >= 1;
select operation, options, object_name, id, parent_id, position, cost, cardinality, other_tag, optimizer
from plan_table
order by id;
您会发现 Oracle 使用 t_pk 进行全索引扫描。还要注意 stopkey 选项的使用。
希望能解释我的回答;)