Home  >  Article  >  Database  >  oracle分页查询sql原理与语句

oracle分页查询sql原理与语句

WBOY
WBOYOriginal
2016-06-07 17:46:271689browse

很多朋友使用多了mysql limit进行分页到了oracle中就不知道如何写分页了,其实oracle分页与mssql分页很像的,下面我来给大家介绍oracle分页语句。

很多朋友使用多了mysql limit进行分页到了oracle中就不知道如何写分页了,其实oracle分页与mssql分页很像的,下面我来给大家介绍oracle分页语句。

最简单使用方法

 代码如下 复制代码

t2.*
from (select rownum r,t1.* from youtable t1 where rownum) t2
where t2.r>?

小于最大的,大于最小的,就是你需要的条数.如果你取40-50条之间的

查询格式:

 代码如下 复制代码

SELECT * FROM
(
SELECT A.*, ROWNUM RN
FROM (SELECT * FROM TABLE_NAME) A
WHERE ROWNUM )
WHERE RN >= 21

其中最内层的查询SELECT * FROM TABLE_NAME表示不进行翻页的原始查询语句。ROWNUM = 21控制分页查询的每页的范围。
上面给出的这个分页查询语句,在大多数情况拥有较高的效率。分页的目的就是控制输出结果集大小,将结果尽快的返回。在上面的分页查询语句中,这种考虑主要体现在WHERE ROWNUM 选择第21到40条记录存在两种方法,一种是上面例子中展示的在查询的第二层通过ROWNUM

 代码如下 复制代码

SELECT * FROM

(
SELECT A.*, ROWNUM RN
FROM (SELECT * FROM TABLE_NAME) A
)
WHERE RN BETWEEN 21 AND 40

后来发现数据越大就查询越慢了,后来得出一些测试结果分享给大家。

--1:无ORDER BY排序的写法。(效率最高)
--(经过测试,此方法成本最低,只嵌套一层,速度最快!即使查询的数据量再大,也几乎不受影响,速度依然!)

 代码如下 复制代码
SELECT *
  FROM (Select ROWNUM AS ROWNO, T.*
           from k_task T
          where Flight_date between to_date('20060501', 'yyyymmdd') and
                to_date('20060731', 'yyyymmdd')
            AND ROWNUM WHERE TABLE_ALIAS.ROWNO >= 10;

--2:有ORDER BY排序的写法。(效率最高)
--(经过测试,此方法随着查询范围的扩大,速度也会越来越慢哦!)

 代码如下 复制代码
SELECT *
  FROM (SELECT TT.*, ROWNUM AS ROWNO
           FROM (Select t.*
                    from k_task T
                   where flight_date between to_date('20060501', 'yyyymmdd') and
                         to_date('20060531', 'yyyymmdd')
                   ORDER BY FACT_UP_TIME, flight_no) TT
          WHERE ROWNUM where TABLE_ALIAS.rowno >= 10;

--3:无ORDER BY排序的写法。(建议使用方法1代替)
--(此方法随着查询数据量的扩张,速度会越来越慢哦!)

 代码如下 复制代码
SELECT *
  FROM (Select ROWNUM AS ROWNO, T.*
           from k_task T
          where Flight_date between to_date('20060501', 'yyyymmdd') and
                to_date('20060731', 'yyyymmdd')) TABLE_ALIAS
WHERE TABLE_ALIAS.ROWNO    AND TABLE_ALIAS.ROWNO >= 10;
--TABLE_ALIAS.ROWNO  between 10 and 100;

--4:有ORDER BY排序的写法.(建议使用方法2代替)
--(此方法随着查询范围的扩大,速度会越来越慢哦!)

 代码如下 复制代码
SELECT *
  FROM (SELECT TT.*, ROWNUM AS ROWNO
           FROM (Select *
                    from k_task T
                   where flight_date between to_date('20060501', 'yyyymmdd') and
                         to_date('20060531', 'yyyymmdd')
                   ORDER BY FACT_UP_TIME, flight_no) TT) TABLE_ALIAS
where TABLE_ALIAS.rowno BETWEEN 10 AND 20;


--5另类语法。(有ORDER BY写法)
--(语法风格与传统的SQL语法不同,不方便阅读与理解,为规范与统一标准,不推荐使用。)

 代码如下 复制代码
With partdata as(
  SELECT ROWNUM AS ROWNO, TT.*  FROM (Select *
                  from k_task T
                 where flight_date between to_date('20060501', 'yyyymmdd') and
                       to_date('20060531', 'yyyymmdd')
                 ORDER BY FACT_UP_TIME, flight_no) TT
   WHERE ROWNUM     Select * from partdata where rowno >= 10;

--6另类语法 。(无ORDER BY写法)

 代码如下 复制代码

With partdata as(
  Select ROWNUM AS ROWNO, T.*
    From K_task T
   where Flight_date between to_date('20060501', 'yyyymmdd') and
         To_date('20060531', 'yyyymmdd')
     AND ROWNUM     Select * from partdata where Rowno >= 10;   

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn