Best practice for pagination in Oracle?

前端 未结 7 1358
别那么骄傲
别那么骄傲 2020-11-28 07:22

Problem: I need write stored procedure(s) that will return result set of a single page of rows and the number of total rows.

Solution A: I create tw

相关标签:
7条回答
  • 2020-11-28 07:58

    If you're already using analytics (ROW_NUMBER() OVER ...) then adding another analytic function on the same partitioning will add a negligible cost to the query.

    On the other hand, there are many other ways to do pagination, one of them using rownum:

    SELECT * 
      FROM (SELECT A.*, rownum rn
              FROM (SELECT *
                      FROM your_table
                     ORDER BY col) A
             WHERE rownum <= :Y)
     WHERE rn >= :X
    

    This method will be superior if you have an appropriate index on the ordering column. In this case, it might be more efficient to use two queries (one for the total number of rows, one for the result).

    Both methods are appropriate but in general if you want both the number of rows and a pagination set then using analytics is more efficient because you only query the rows once.

    0 讨论(0)
  • 2020-11-28 07:59

    Try this:

    select * from ( select * from "table" order by "column" desc ) where ROWNUM > 0 and ROWNUM <= 5;
    
    0 讨论(0)
  • 2020-11-28 08:15

    This may help:

       SELECT * FROM 
         ( SELECT deptno, ename, sal, ROW_NUMBER() OVER (ORDER BY ename) Row_Num FROM emp)
         WHERE Row_Num BETWEEN 5 and 10;
    
    0 讨论(0)
  • 2020-11-28 08:15

    Sorry, this one works with sorting:

    SELECT * FROM (SELECT ROWNUM rnum,a.* FROM (SELECT * FROM "tabla" order by "column" asc) a) WHERE rnum BETWEEN "firstrange" AND "lastrange";
    
    0 讨论(0)
  • 2020-11-28 08:17

    I also faced a similar issue. I tried all the above solutions and none gave me a better performance. I have a table with millions of records and I need to display them on screen in pages of 20. I have done the below to solve the issue.

    1. Add a new column ROW_NUMBER in the table.
    2. Make the column as primary key or add a unique index on it.
    3. Use the population program (in my case, Informatica), to populate the column with rownum.
    4. Fetch Records from the table using between statement. (SELECT * FROM TABLE WHERE ROW_NUMBER BETWEEN LOWER_RANGE AND UPPER_RANGE).

    This method is effective if we need to do an unconditional pagination fetch on a huge table.

    0 讨论(0)
  • 2020-11-28 08:20

    In Oracle 12C you can use limit LIMIT and OFFSET for the pagination.

    Example - Suppose you have Table tab from which data needs to be fetched on the basis of DATE datatype column dt in descending order using pagination.

    page_size:=5
    
    select * from tab
    order by dt desc
    OFFSET nvl(page_no-1,1)*page_size ROWS FETCH NEXT page_size ROWS ONLY;
    

    Explanation:

    page_no=1 page_size=5

    OFFSET 0 ROWS FETCH NEXT 5 ROWS ONLY - Fetch 1st 5 rows only

    page_no=2 page_size=5

    OFFSET 5 ROWS FETCH NEXT 5 ROWS ONLY - Fetch next 5 rows

    and so on.

    Refrence Pages -

    https://dba-presents.com/index.php/databases/oracle/31-new-pagination-method-in-oracle-12c-offset-fetch

    https://oracle-base.com/articles/12c/row-limiting-clause-for-top-n-queries-12cr1#paging

    0 讨论(0)
提交回复
热议问题