How do I limit the number of rows returned by an Oracle query after ordering?

前端 未结 17 1546
夕颜
夕颜 2020-11-21 04:56

Is there a way to make an Oracle query behave like it contains a MySQL limit clause?

In MySQL, I can do this:

         


        
17条回答
  •  南方客
    南方客 (楼主)
    2020-11-21 05:13

    I did some performance testing for the following approaches:

    Asktom

    select * from (
      select a.*, ROWNUM rnum from (
        
    ) where myrow between MIN_ROW and MAX_ROW
    

    Short Alternative

    select * from (
      select statement, rownum as RN with order by clause
    ) where a.rn >= MIN_ROW and a.rn <= MAX_ROW
    

    Results

    Table had 10 million records, sort was on an unindexed datetime row:

    • Explain plan showed same value for all three selects (323168)
    • But the winner is AskTom (with analytic following close behind)

    Selecting first 10 rows took:

    • AskTom: 28-30 seconds
    • Analytical: 33-37 seconds
    • Short alternative: 110-140 seconds

    Selecting rows between 100,000 and 100,010:

    • AskTom: 60 seconds
    • Analytical: 100 seconds

    Selecting rows between 9,000,000 and 9,000,010:

    • AskTom: 130 seconds
    • Analytical: 150 seconds

提交回复
热议问题