ITPub博客

首页 > Linux操作系统 > Linux操作系统 > [20130803]ORACLE 12C TOP N SQL实现分页功能.txt

[20130803]ORACLE 12C TOP N SQL实现分页功能.txt

原创 Linux操作系统 作者:lfree 时间:2013-08-05 09:35:32 0 删除 编辑
[20130803]ORACLE 12C TOP N SQL实现分页功能.txt

参考链接:http://www.xifenfei.com/4257.html

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/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
熟悉oracle相关技术,擅长sql优化,rman备份与恢复,熟悉linux shell编程。

注册时间:2008-01-03

  • 博文量
    2599
  • 访问量
    6371519