Using order by and rownum in massive tables

Hi,
I need to retreive paginated and ordered data from a big table.
I know some different tips using rownum or the RANK() function and subqueries, these tips work well for small amount of data, but I need to get the data from a table with 200.000 entries (not under my control), and with all those methods is necessary to order first the data and then select your range, so performance is extremely poor.
Anybody knows a better solution? it doesn't matter if is plain SQL(better) or PL/SQL.
Thanks in advance

but I was looking for something like the LIMIT in MySQL or TOP in msSQL where all the sorting is made internally and it's really fast If the data needs sorting, I do not think Oracle would take any longer to do that than the others mentioned, unless the other DBs mentioned, only sort the rows that are actually being returned.
As for the LIMIT clause, Oracle already has that clause available when FETCHing from a CURSOR:
SQL> DECLARE
  2      CURSOR emp_cur IS
  3          SELECT * FROM scott.emp;
  4      TYPE emp_rec_table IS TABLE OF emp_cur%ROWTYPE INDEX BY BINARY_INTEGER;
  5      emp_tbl emp_rec_table;
  6  BEGIN
  7      OPEN emp_cur;
  8      LOOP
  9          emp_tbl.DELETE;
10          FETCH emp_cur BULK COLLECT
11              INTO emp_tbl LIMIT 5;
12          EXIT WHEN emp_tbl.COUNT <= 0;
13          dbms_output.put_line(emp_tbl.COUNT || ' rows fetched using LIMIT clause.');
14      END LOOP;
15      CLOSE emp_cur;
16  END;
17  /
5 rows fetched using LIMIT clause.
5 rows fetched using LIMIT clause.
4 rows fetched using LIMIT clause.
PL/SQL procedure successfully completed.
SQL> disconnect
Disconnected from Oracle9i Enterprise Edition Release 9.2.0.3.0 - Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.3.0 - Production
SQL>

Similar Messages

  • Sales Order no and IDOC No Link table for Sales Out bound IDOC ORDRSP

    Hi Friends
    How to find Sales Order no and IDOC No Link table for Sales Out bound IDOC ORDRSP.
    Thanks in Advance.
    SR

    Hi
    If the idoc is managed by message, the link are stored in GOS (Obeject Service)
    U can try to use the function module NREL_GET_NEIGHBOURHOOD
    If you put
    IS_OBJECT-OBJKEY = <sales document>
    IS_OBJECT-OBJTYPE = <business object> (I think BUS2032)
    the function returns the idoc
    or if you put
    IS_OBJECT-OBJKEY = <idoc number>
    IS_OBJECT-OBJTYPE = <business object> (I think IDOC)
    the function returns the sales order
    You can investigate that function module in order to find out the tables
    Max

  • Sales order, Delivery and Invoice changes history tables

    Dear All,
    I want to know how to find out the changes done in Sales Order, Delivery and Invoice.
    As far as sales order is concerned, I know CDHDR and CDPOS are tables can be used to find out the changes.
    But I am not sure about the "Object ID". Where we need to find out this Object ID?
    In the Data element "VBELN_VA" "Change Document" option has been checked under "Further Characteristics" tab.
    Please confirm about the "Object ID".
    Similarly give inputs for Delivery and Invoice change history also.
    Regards,
    Mullairaja

    Hi,
    Could you please explain, how to use these programs?
    Regards,
    Mullairaja

  • Without using order by and connect by clause

    hi all,
    can i write a tree query without using start with clause and connect by clause
    and
    can i order the employee names in a select query without using order by clause
    can those things are possible in select statement...

    dipuna wrote:
    hi all,
    can i write a tree query without using start with clause and connect by clause
    and
    can i order the employee names in a select query without using order by clause
    can those things are possible in select statement...A very odd question.
    Those clauses are the means to do what you are asking. Why would you be looking for some other method?

  • How it use Filter Id and FilterPanelFocusid in JSF Table component

    How it use FilterId and FilterPanelFocusid in JSF Table component

    How it use FilterId and FilterPanelFocusid in JSF Table component

  • Order by AND rownum in a function?

    I have a table with articles sorted by an id. In the table is also a date field to tell when the article is written.
    I want a query that returns the 20 latest articles, and this is what i have come up with:
    SELECT * FROM (
    SELECT articleid,writtendate
    FROM articles
    ORDER BY writtendate desc)
    WHERE ROWNUM < 21;
    This works alright, BUT I want it in a function! There it doesnt compile!!! Anyone got any suggestions?
    I have tried to separate the stuff, like first sorting the records in a view and then selecting the top 20 after that. I could try a cursor and fetching out of that, but havent suceeded with that either.
    Bye!

    ok, the sql statement works perfectly in an sql editor, but wont compile in a function.
    FUNCTION fnc_getLatest20 (
    cur IN OUT pkg_cursor.refcur
    )RETURN pkg_exception.return_type IS
    BEGIN
    OPEN cur FOR
    SELECT * FROM (
    SELECT articleid,writtendate
    FROM articles
    ORDER BY writtendate DESC)
    WHERE ROWNUM < 21;
    RETURN pkg_exception.err_none;
    END;
    The best I have is that either the rows returned in the cursor is all rows, ordered correctly or the top twenty in some other order.
    I most be on the wrong track here, there has to be some easier way of doing this...
    BTW, thanks for the help so far.

  • Question about order by and rownum and subqueries.

    Can you explain why test 4 below results as it results- with returning no rows?
    Identificator "PKG_INTRA_CUSTOMER_SEARCH.NAME" is a package function, which returns value from a variable declared in package body, the function returns value 'e%'.
    Please note that tests 1-3 all returned data, but test 4 didn't. Why? My porpouse is that test 4 would also return data.
    1. Query without "rownum" and with "Order by" returns 2 records:
    select q.*--, rownum
                ,PKG_INTRA_CUSTOMER_SEARCH.NAME
             from
                select c.ID,
                   c.NAME,
                   c.CODE     
                from V_CUSTOMER c  
                where 1=1 and  lower(c.NAME) like PKG_INTRA_CUSTOMER_SEARCH.NAME 
                order by c.NAME, c.ID
                ) q
    10020     Ees Nimi     37810010237     e%
    10040     ewewrwe werwerwer          e%2. Query with "rownum" and without "Order by" returns 2 records:
    select q.*, rownum
                ,PKG_INTRA_CUSTOMER_SEARCH.NAME
             from
                select c.ID,
                   c.NAME,
                   c.CODE     
                from V_CUSTOMER c  
                where 1=1 and  lower(c.NAME) like PKG_INTRA_CUSTOMER_SEARCH.NAME 
                --order by c.NAME, c.ID
                ) q
    10020     Ees Nimi     37810010237     e%
    10040     ewewrwe werwerwer          e%3.Query without "rownum" and with "Order by" returns 2 records:
    select q.*--, rownum
                ,PKG_INTRA_CUSTOMER_SEARCH.NAME
             from
                select c.ID,
                   c.NAME,
                   c.CODE     
                from V_CUSTOMER c  
                where 1=1 and  lower(c.NAME) like PKG_INTRA_CUSTOMER_SEARCH.NAME 
                order by c.NAME, c.ID
                ) q
    10020     Ees Nimi     37810010237     e%
    10040     ewewrwe werwerwer          e% 4. Query with "rownum" and with "Order by" returns 0 records:
    select q.*, rownum
                ,PKG_INTRA_CUSTOMER_SEARCH.NAME
             from
                select c.ID,
                   c.NAME,
                   c.CODE     
                from V_CUSTOMER c  
                where 1=1 and  lower(c.NAME) like PKG_INTRA_CUSTOMER_SEARCH.NAME 
                order by c.NAME, c.ID
                ) q

    Hi,
    please reproduce the question in your test database with script below.
    My general question is that wht Test5 below returns only 1 row, but Test5 returns 3 rows.
    The difference between those two tests is only that one has "order by" clause, the other doesn't have.
    I need the "order by" clause to stay, but seems like it is not allowed.
    CREATE OR REPLACE
    PACKAGE PACKAGE1 AS
    function NAME return varchar2;
    END PACKAGE1;
    CREATE OR REPLACE
    PACKAGE body PACKAGE1 AS
    function NAME return varchar2
    is
    begin
       return 'e%';
    end NAME;
    END PACKAGE1;
    select PACKAGE1.name from dual--e%
    create table Tbl AS
       (SELECT 'e2b' Col1, 'A' Col2, 'AA' Col3 FROM dual
       UNION
       SELECT 'e3b', 'B','BB' FROM dual
    --Test5:  
    select q.*, rownum pos, PACKAGE1.name f         
             from
                select c.col1,
                   c.col2,
                   c.col3     
                from Tbl c  
                where 1=1 and  lower(c.col1) like PACKAGE1.name                      
                order by c.col2, c.col1
                ) q                               
                union all
             select '111' , '111' , '111' , 0 pos, PACKAGE1.name f from dual   --return 1 row
    --Test6
    select q.*, rownum pos, PACKAGE1.name f         
             from
                select c.col1,
                   c.col2,
                   c.col3     
                from Tbl c  
                where 1=1 and  lower(c.col1) like PACKAGE1.name                      
                --order by c.col2, c.col1
                ) q                               
                union all
             select '111' , '111' , '111' , 0 pos, PACKAGE1.name f from dual   --return 3 rowsEdited by: CharlesRoos on Feb 17, 2010 5:30 AM

  • Using union all and rownum

    Hello again.
    Another question.
    Can I query with union all and stop it when I get N rows.
    For example:
    select 1 from dba_segments
    union all
    select 2 from dba_segments where
    union all
    select 3 from dba_segments where;
    and get the 100 first rows without doing the whole query:(not like that-->)
    select * from (
    select 1 from dba_segments
    union all
    select 2 from dba_segments where
    union all
    select 3 from dba_segments where)
    where rownum < 100);
    I want the query will stop when there are 100 rows in the result set.
    thank you!

    You already posted your own answer. It just seems you don't want to use it.
    ROWNUM is NOT assigned until the rows are selected to be returned. So you need to wrap the three inner queries into a query that uses ROWNUM.

  • How to use filter operator with ROWNUM

    I would like to add filter operator in my mapping with ROWNUM to reduce the ETL loading time during testing to ensure mapping is working, but I don't know how, please help provide me some guideline. Thank you.

    what is your owb version?
    Starting with Oracle Warehouse Builder 10.2.0.3, you can use the pseudocolumns ROWID and ROWNUM in mappings. The ROWNUM pseudocolumn returns a number indicating the order in which a row was selected from a table. The ROWID pseudocolumn returns the rowid (binary address) of a row in a database table.
    You can use the ROWID and ROWNUM pseudocolumns in Table, View, and Materialized View operators in a mapping. These operators contain an additional column called COLUMN USAGE that is used to identify attributes used as ROWID or ROWNUM. For normal attributes, this column defaults to TABLE USAGE. To use an attribute for ROWID or ROWNUM values, set the COLUMN USAGE to ROWID or ROWNUM respectively.
    You can map a ROWID column to any attribute of data type ROWID, UROWID, or VARCHAR2. You can map ROWNUM column to an attribute of data type NUMBER or to any other data type that allows implicit conversion from NUMBER.
    Note that ROWID and ROWNUM pseudocolumns are not displayed in the Data Object Editor since they are not real columns.
    Edited by: Darthvader-647181 on Oct 29, 2008 9:18 AM

  • How to use XML Gateway for Order Import and Item Import?

    Hello XML gurus,
    I'm working in Oracle Apps area and do not have much knowledge on XML or web programming. Currently in my project there is a need to use Order Import and Item Import using interface tables and XML Gateway. The items/orders information will be in the XML format from Siebel system, there will be no custom processing...just process the data as-is.
    I've read the XML Gateway user guide but did not understand much.
    Anyone with similar implementation experience?
    I'm looking for a step-by-step guide on what needs to be done in Oracle Apps side to populate the interface tables from XML file and call the import APIs?
    Your inputs, suggestions, tips are highly appreciated. :)
    Thx,
    Jags

    I have a similar requirement to automate the order import and was looking at XML Gateway. Please update if you were able to find more information.
    - Ayyappa

  • Do you know the link between the sales order item and functional location ?

    How know the functional location if I know de sales order number and item ?
    Wich table or bapi ?
    Tks.

    Hi,
    I think there's all details in my question.
    You answer is right only if I need to know the functional location at AUFNR level.
    Is not my case, I need this information at sales order level.
    Regards,
    Roberto.

  • What is the relation between delivery num , sales order num and invoice

    what is the relation between delivery num , sales order num and invoice

    Look at VBFA Table
    goto VBFA table ,enter order number number vbelv ,vbtyp_n is C,then VBELN is the delivery
    if you enter delivery number in vbelv ,vbtyp_n is J,then vbeln is Invoice.
    here VBTYP_N is the import.
    VBFA is the sales document flow table,and very important table
    Thanks
    seshu

  • Link Between Sales Order Type and Customer/Material Exclusion

    Hi,
    Any one know the  Link Between Sales Order Type  and Customer/Material exclusion tables (Transaction: VB03 ) ?

    Hello,
    For Tcode VB03 you can check KOTG001 for Customer/Material.
    Thanks &Regards,
    Abhijit

  • Trouble with subquery and rownum and ordering

    I'm having trouble making this subquery work. The basic idea is that the web app will show only so many rows of results on the page, but right now I'm just playing around in SQL*Plus. The address book holds mixed case, so in order to sort by name properly I need to use UPPER to ignore case sensitivity. This SQL statement works fine for me:
    select addressbookid from addressbook where addressbookid like '905430931|%' order by upper(addressbookid);I know that if you mention ROWNUM in the WHERE clause, you're referring to the row numbers of the ResultSet that Oracle returns. How do I use both ORDER BY UPPER(ADDRESSBOOKID) and WHERE ROWNUM > 25 AND ROWNUM <= 50? I figured a subquery would do it, but I can't write a correct one that does it! Below are my 2 attempts with the errors, and then I just tried to play with rownum:
    SQL> select addressbookid from addressbook where addressbookid in (select addressbookid from address book where addressbookid like '905430931|%' order by upper(addressbookid));
    select addressbookid from addressbook where addressbookid in (select addressbookid from addressbook
    ERROR at line 1:
    ORA-00907: missing right parenthesis
    SQL> select addressbookid from addressbook where addressbookid in upper(select addressbookid from addressbook where addressbookid like '905430931|%');
    select addressbookid from addressbook where addressbookid in upper(select addressbookid from address
    ERROR at line 1:
    ORA-00936: missing expression
    SQL> select addressbookid from addressbook where addressbookid like '905430931|%' and rownum > 25 and rownum <= 50 order by upper(addressbookid);
    no rows selected
    SQL> select addressbookid from addressbook where addressbookid like '905430931|%' and rownum > 25 and rownum <= 50;
    no rows selectedLike I said, if I can get a working subquery, then I'd like to attach that restriction on the rownum stuff. I'm wondering if it will be a problem trying to get the ordering to happen and then the rownum restriction after that, and all in the same query...
    Btw, we've made all the table and column names in our database uppercase, so that shouldn't matter.

    This is probably the most efficient way ...
    select addressbookid
    from
       select addressbookid,
       rownum rn
       from
          select addressbookid
          from addressbook
          where addressbookid like '905430931|%'
          order by addressbookid
       where rownum <= 50
    where rn > 25

  • [b]how to use EJB QL for LIKE and ROWNUM[/b]

    Hi,
    i am using CMP EJB with OC4J server
    i face two problems in it when i make any query like
    select object(o) from Partymas o where o.stationCode like '?1' and o.rownum &gt;='?2' and o.rownum &lt;='?3' order by party_name
    then it shows error on rownum so i cant understand how to solve it
    second is when i use LIKE statment in EJB QL like this:-
    select object(o) from Partymas o where o.pinCode like '%?%'
    then it shows error so can any one help me
    thanks
    warm regards
    vikassheelgupta

    <ejb-ql>select object(o) from Partymas o where o.stationCode like '?1' and
    rownum >='?2' and rownum <='?3' order by party_name</ejb-ql>Try this:
    rownum >= ?2 and rownum <= ?3
    <ejb-ql>select object(o) from Partymas o where o.partyName like '%?%'</ejb-ql>Try this:
    like ?1

Maybe you are looking for

  • ITunes 7 -error message when downloading

    Hi Sorry if anybody else has raised this question, I currently have itunes 4 downloaded and am trying to download itunes 7, however i keep getting the following error from Apple support. "Service'IPod service' could not be stopped. verfiy that you ha

  • Keyhole DRM Application has encountered  a problem and needs to close

    Every time I try to run one of the games I have downloaded from Shockwave e.g Ciao Bella I get the error message "shockwave.com Keyhole DRM Application has encountered an problem and needs to close".

  • Equation editor in word for mac 2011

           I apologize if this is straying too far from elements of the Lion discussion but I have seen others compliment the capability of the equation editor in Word for Mac 2011. It's icon is at the right end of the Document Elements toolbar.      But

  • What is indexing and why does it take 122 hours?

    What is indexing and why has it slowed my computer and why does it take 122 hours?

  • Frozen at apple logo?

    frozen at apple logo, can't turn off.  Just stuck, how do I fix this?