八、oracle 分页
oracle的分页一共有三种方式
方法一 根据rowid
SELECT * FROM EMP WHERE ROWID IN (SELECT RID FROM (SELECT ROWNUM RN, RID FROM (SELECT ROWID RID, EMPNO FROM EMP ORDER BY EMPNO DESC) WHERE ROWNUM <= ( (currentPage-1) * pageSize + pageSize )) --每页显示几条 WHERE RN > ((currentPage-1) * pageSize) ) --当前页数 ORDER BY EMPNO DESC; eg、 -- 5 = (currentPage-1) * pageSize + pageSize 每页显示几条 -- 0 = (currentPage-1) * pageSize 当前页数 SELECT * FROM EMP WHERE ROWID IN (SELECT RID FROM (SELECT ROWNUM RN, RID FROM (SELECT ROWID RID, EMPNO FROM EMP ORDER BY EMPNO DESC) WHERE ROWNUM <= ( (1-1) * 5 + 5 )) --每页显示几条 WHERE RN > ((1-1) * 5) ) --当前页数 ORDER BY EMPNO DESC;
方法二 按分析函数
SELECT * FROM (SELECT T.*, ROW_NUMBER() OVER(ORDER BY empno DESC) RK FROM emp T) WHERE RK <= ( (currentPage-1) * pageSize + pageSize ) --每页显示几条 AND RK > ( (currentPage-1) * pageSize ); --当前页数 eg、 -- 5 = (currentPage-1) * pageSize + pageSize 每页显示几条 -- 0 = (currentPage-1) * pageSize 当前页数 SELECT * FROM (SELECT T.*, ROW_NUMBER() OVER(ORDER BY empno DESC) RK FROM emp T) WHERE RK <= 5 AND RK > 0;
方法三 rownum
SELECT * FROM (SELECT T.*, ROWNUM RN FROM (SELECT * FROM EMP ORDER BY EMPNO DESC) T WHERE ROWNUM <= ( (currentPage-1) * pageSize + pageSize )) --每页显示几条 WHERE RN > ( (currentPage-1) * pageSize ); --当前页数 eg、 -- 5 = (currentPage-1) * pageSize + pageSize 每页显示几条 -- 0 = (currentPage-1) * pageSize 当前页数 SELECT * FROM (SELECT T.*, ROWNUM RN FROM (SELECT * FROM EMP ORDER BY EMPNO DESC) T WHERE ROWNUM <= 5) WHERE RN > 0;
感觉方法一的效率最好,方法三 次之,方法二 最差。