---说明:案例来自《 收获,不止SQL优化》
创建测试数据:
---drop table test_rownum purge;
SQL > create table test_rownum as select * from dba_objects ;
SQL > select count () from test_rownum ; ---75793*
SQL > alter session set statistics_level = all ;
SQL > set linesize 1000
SQL > set pagesize 500
分页写法 1 :
SQL > select * from ( select t. *, rownum as rn from test_rownum t ) a where a.rn >= 1 and a.rn <= 10 ;

查看执行计划: SQL > select * from table ( dbms_xplan.display_cursor ( null , null , 'allstats last' ));

分页写法 2 : SQL >select * from ( select t. *, rownum as rn from test_rownum t where rownum <= 10 ) a where a.rn >= 1 ;

查看执行计划: SQL > select * from table ( dbms_xplan.display_cursor ( null , null , 'allstats last' ));

总结: 写法1的buffer为1080,扫描真实数据为75793条,写法2的buffer只有5,扫描真实数据为10条,性能较写法1有很大改善。
转载自chenoracle

最后修改:2022 年 02 月 27 日
如果觉得我的文章对你有用,请随意赞赏