Oracle数据库用SQL实现快速分页

Oracle采用嵌套3层的查询语句结合rownum来实现分页,这在Oracle上是最快的方式,如果只是一层或者两层的查询语句的rownum不能支持order by。

程序:
public String getLimitString(String sql) {
StringBuffer pagingSelect = new StringBuffer(100);
pagingSelect.append("select * from ( select row_.*, rownum rownum_ from ( ");
pagingSelect.append(sql);
pagingSelect.append(" ) row_ where rownum ?");
return pagingSelect.toString();
}

sql实例:

select * from ( select row_.*, rownum rownum_ from (select * from scott.emp ) row_ where rownum 2;

相关推荐