Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Retrieve N Record.

Retrieve N Record.

Scheduled Pinned Locked Moved Database
databasehelpquestion
17 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Jorgen Andersson

    SELECT *
    FROM (
    SELECT *
    FROM Productlist pl
    ORDER BY somefield
    )
    WHERE ROWNUM BETWEEN 9991 AND 10000

    Note that you can never trust the DB to give you the records in the same order every time, so you need the order by clause. The field you're ordering by should be indexed if you want to get any speed to your query

    List of common misconceptions

    N Offline
    N Offline
    Nanda_MR
    wrote on last edited by
    #7

    Thanks. Problem is the table is too big it contains 10 lak to 90 lak data. while access perticular data (ex: 1500000 to 1501000) then it is taking so much time. The Inner Query execute all records (90 lak) and the outer query filter 9991 to 10000. This is my problem. Regards, Nanda

    J 1 Reply Last reply
    0
    • N Nanda_MR

      Hey Thanks, Thanks for your valuable reply. I am using Oracle. The rownum is dynamical assign to each row. so SQL server method is not working. and I tried view it is working but when order by used the result is coming wrong. Thanks and regards, Nanda

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #8

      Nanda_MR wrote:

      I am using Oracle

      That would have been useful info, also what is a lak, presumably lots. So you know how to get the paged set, you real question is how to optimise the query (using rownum) or if there is a better method to use in orable. And I'm afraid there I cannot help you.

      Never underestimate the power of human stupidity RAH

      B N L 3 Replies Last reply
      0
      • N Nanda_MR

        Thanks. Problem is the table is too big it contains 10 lak to 90 lak data. while access perticular data (ex: 1500000 to 1501000) then it is taking so much time. The Inner Query execute all records (90 lak) and the outer query filter 9991 to 10000. This is my problem. Regards, Nanda

        J Offline
        J Offline
        Jorgen Andersson
        wrote on last edited by
        #9

        That's why you need a index on the column you order by, it's already sorted. The optimizer will take care of the inner query. Do you have an index on the column you're sorting by?

        List of common misconceptions

        1 Reply Last reply
        0
        • N Nanda_MR

          Hi Guys! I am trying to retrieve middle records. The below query is working perfectly. The problem is my table is too huge (more then 10 lak reocrd) it is taking more time. Is there any other solution? [This is sample query]

          select *
          from (select t.*, rownum rn
          from (SELECT *
          FROM (select * from Productlist)) t
          Where rownum <= 10000)
          Where rn > 9990

          Regards, Nanda

          W Offline
          W Offline
          Wendelius
          wrote on last edited by
          #10

          Hi, If you do have an ascending surrogate key, one efficient way would be to use that. For example if you have a field called Id and the values are coming from ordered sequence your query could be something like:

          select *
          from ProductList a,
          (select b.Id,
          count(*) over (order by b.Id) as runcount
          from productlist b
          where b.Id > :lastFetchedId) subq
          where a.Id = subq.Id
          and subq.runcount <= 10

          The need to optimize rises from a bad design.My articles[^]

          N 1 Reply Last reply
          0
          • M Mycroft Holmes

            Nanda_MR wrote:

            I am using Oracle

            That would have been useful info, also what is a lak, presumably lots. So you know how to get the paged set, you real question is how to optimise the query (using rownum) or if there is a better method to use in orable. And I'm afraid there I cannot help you.

            Never underestimate the power of human stupidity RAH

            B Offline
            B Offline
            Bernhard Hiller
            wrote on last edited by
            #11

            Mycroft Holmes wrote:

            what is a lak, presumably lots

            Indians have a different style of counting big numbers. A lak is one hundred thousand (Indian style: 1,00,000), a crore is ten million (Indian style: 1,00,00,000). But that's still easy. Stay a few days in India to get confused with more puzzling Indian style information....

            1 Reply Last reply
            0
            • W Wendelius

              Hi, If you do have an ascending surrogate key, one efficient way would be to use that. For example if you have a field called Id and the values are coming from ordered sequence your query could be something like:

              select *
              from ProductList a,
              (select b.Id,
              count(*) over (order by b.Id) as runcount
              from productlist b
              where b.Id > :lastFetchedId) subq
              where a.Id = subq.Id
              and subq.runcount <= 10

              The need to optimize rises from a bad design.My articles[^]

              N Offline
              N Offline
              Nanda_MR
              wrote on last edited by
              #12

              Hi Mika, I done similarly in View. Add a new column unique ID and I am adding rownum. In this case I am executing query without any delay. But one problem is there. In case if order by condition added the output of query is coming wrong(Bug) or a block of rows is order. Regards, Nanda

              W 1 Reply Last reply
              0
              • M Mycroft Holmes

                Nanda_MR wrote:

                I am using Oracle

                That would have been useful info, also what is a lak, presumably lots. So you know how to get the paged set, you real question is how to optimise the query (using rownum) or if there is a better method to use in orable. And I'm afraid there I cannot help you.

                Never underestimate the power of human stupidity RAH

                N Offline
                N Offline
                Nanda_MR
                wrote on last edited by
                #13

                Hi RAH, I am trying execute my query but it is taking much time(4 to 10 min) to avoid that problem and searching efficient query or solution. The your last post provide some ideas. it reduce execution time. Regards, Nanda

                1 Reply Last reply
                0
                • M Mycroft Holmes

                  Nanda_MR wrote:

                  I am using Oracle

                  That would have been useful info, also what is a lak, presumably lots. So you know how to get the paged set, you real question is how to optimise the query (using rownum) or if there is a better method to use in orable. And I'm afraid there I cannot help you.

                  Never underestimate the power of human stupidity RAH

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #14

                  Mycroft Holmes wrote:

                  also what is a lak

                  Google would happily have told you ... :-D

                  Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                  Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                  M 1 Reply Last reply
                  0
                  • N Nanda_MR

                    Hi Mika, I done similarly in View. Add a new column unique ID and I am adding rownum. In this case I am executing query without any delay. But one problem is there. In case if order by condition added the output of query is coming wrong(Bug) or a block of rows is order. Regards, Nanda

                    W Offline
                    W Offline
                    Wendelius
                    wrote on last edited by
                    #15

                    Hi,

                    Nanda_MR wrote:

                    I am adding rownum

                    Just to check: Did you notice that I didn't use rownum at all because it's unreliable in this context (for many different reasons). The example I created shouldn't encounter any of the problems rownum has since the example is based on a running total count on an unique and ascending field.

                    The need to optimize rises from a bad design.My articles[^]

                    1 Reply Last reply
                    0
                    • L Luc Pattyn

                      Mycroft Holmes wrote:

                      also what is a lak

                      Google would happily have told you ... :-D

                      Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                      Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                      M Offline
                      M Offline
                      Mycroft Holmes
                      wrote on last edited by
                      #16

                      I naturally did the google thing, the response was nowhere as succinct as the answer I got here :laugh:

                      Never underestimate the power of human stupidity RAH

                      L 1 Reply Last reply
                      0
                      • M Mycroft Holmes

                        I naturally did the google thing, the response was nowhere as succinct as the answer I got here :laugh:

                        Never underestimate the power of human stupidity RAH

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #17

                        Unfortunately conciseness isn't Google's primary strength. :((

                        Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                        Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups