[20130803]ORACLE 12C TOP N SQL實現分頁功能.txt

lfree發表於2013-08-05
[20130803]ORACLE 12C TOP N SQL實現分頁功能.txt

參考連結:

SQL> @ver
BANNER                                                                               CON_ID
-------------------------------------------------------------------------------- ----------
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production              0

select * fro emp;

SQL> SELECT * FROM emp ORDER BY empno ;
     EMPNO ENAME      JOB              MGR HIREDATE                   SAL       COMM     DEPTNO
---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 1980-12-17 00:00:00        800                    20
      7499 ALLEN      SALESMAN        7698 1981-02-20 00:00:00       1600        300         30
      7521 WARD       SALESMAN        7698 1981-02-22 00:00:00       1250        500         30
      7566 JONES      MANAGER         7839 1981-04-02 00:00:00       2975                    20
      7654 MARTIN     SALESMAN        7698 1981-09-28 00:00:00       1250       1400         30
      7698 BLAKE      MANAGER         7839 1981-05-01 00:00:00       2850                    30
      7782 CLARK      MANAGER         7839 1981-06-09 00:00:00       2450                    10
      7788 SCOTT      ANALYST         7566 1987-04-19 00:00:00       3000                    20
      7839 KING       PRESIDENT            1981-11-17 00:00:00       5000                    10
      7844 TURNER     SALESMAN        7698 1981-09-08 00:00:00       1500          0         30
      7876 ADAMS      CLERK           7788 1987-05-23 00:00:00       1100                    20
      7900 JAMES      CLERK           7698 1981-12-03 00:00:00        950                    30
      7902 FORD       ANALYST         7566 1981-12-03 00:00:00       3000                    20
      7934 MILLER     CLERK           7782 1982-01-23 00:00:00       1300                    10

14 rows selected.


SQL> SELECT * FROM emp ORDER BY empno offset 5 rows FETCH next 5 ROWS ONLY;
     EMPNO ENAME      JOB              MGR HIREDATE                   SAL       COMM     DEPTNO
---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
      7698 BLAKE      MANAGER         7839 1981-05-01 00:00:00       2850                    30
      7782 CLARK      MANAGER         7839 1981-06-09 00:00:00       2450                    10
      7788 SCOTT      ANALYST         7566 1987-04-19 00:00:00       3000                    20
      7839 KING       PRESIDENT            1981-11-17 00:00:00       5000                    10
      7844 TURNER     SALESMAN        7698 1981-09-08 00:00:00       1500          0         30
--注意一個細節,這種方式sqlplus沒有14 rows selected.相似的提示資訊。除非next N能全部取完。

--檢視執行計劃:

SQL> @dpc '' ''
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------
SQL_ID  dnncqfs9xrpc7, child number 0
-------------------------------------
SELECT * FROM emp ORDER BY empno offset 5 rows FETCH next 5 ROWS ONLY

Plan hash value: 2801941731

----------------------------------------------------------------------
| Id  | Operation                     | Name   | E-Rows | Cost (%CPU)|
----------------------------------------------------------------------
|   0 | SELECT STATEMENT              |        |        |     2 (100)|
|*  1 |  VIEW                         |        |     14 |     2   (0)|
|*  2 |   WINDOW NOSORT STOPKEY       |        |     14 |     2   (0)|
|   3 |    TABLE ACCESS BY INDEX ROWID| EMP    |     14 |     2   (0)|
|   4 |     INDEX FULL SCAN           | PK_EMP |     14 |     1   (0)|
----------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter(("from$_subquery$_002"."rowlimit_$$_rownumber"<=CASE
              WHEN (5>=0) THEN 5 ELSE 0 END +5 AND
              "from$_subquery$_002"."rowlimit_$$_rownumber">5))
   2 - filter(ROW_NUMBER() OVER ( ORDER BY "EMP"."EMPNO")<=CASE  WHEN
              (5>=0) THEN 5 ELSE 0 END +5)

--實際上本質還是使用分析函式ROW_NUMBER()。只不過這樣寫簡單一些。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-767753/,如需轉載,請註明出處,否則將追究法律責任。

相關文章