Oracle rownum usage for splitting a Table into two equal parts.

Hi All,
I have a table which has like 1.2 billion records and i would have to split the table in two parts for my Archiving needs.Unfortunately that table does not have any unique key or primary key or data stamp which i can rely for.
I would have to use the rownum concept to divide the table.
I am using the below
SELECT * FROM (SELECT ENAME, SAL FROM EMP ORDER BY SAL DESC) WHERE ROWNUM < 5000000;
But the problem is that the table is taking forever to retrieve as it has to do a order by and then retrieve the data as per the where clause.
The question i have is that instead of using a orderby clause to get the same rownum for the row every time, can i directly rely on the fact that the database is read only and the Rownum would remain same even without oder by clause....
Thanks....

WARNING! There is a bug in the code, see EDIT: at bottom of post for details
Justin,
It makes sense that Oracle could order over rowid without sorting, but I see a sort in the explain plan:
SQL> create table t as select 1 as data
  2  from all_objects
  3  where rownum <= 100000;
Table created.
SQL> explain plan for select *
  2  from (select t.*, row_number() over (order by rowid) rn from t)
  3  where rn < 50000;
Explained.
SQL> select * from table(DBMS_XPLAN.DISPLAY);
PLAN_TABLE_OUTPUT
Plan hash value: 327232321
| Id  | Operation                | Name | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     |
|   0 | SELECT STATEMENT         |      | 99651 |  2530K|       |   489   (3)| 00:00:07 |
|*  1 |  VIEW                    |      | 99651 |  2530K|       |   489   (3)| 00:00:07 |
|*  2 |   WINDOW SORT PUSHED RANK|      | 99651 |  2432K|  7056K|   489   (3)| 00:00:07 |
|   3 |    TABLE ACCESS FULL     | T    | 99651 |  2432K|       |    31   (7)| 00:00:01 |
Predicate Information (identified by operation id):
   1 - filter("RN"<50000)
   2 - filter(ROW_NUMBER() OVER ( ORDER BY ROWID)<50000)875820,
What are you doing with the results of the select to archive the table in two pieces? If the archive is in the DB in two seperate tables, multi table insert would be an option:
SQL> create table archive_1 (data number);
Table created.
SQL> create table archive_2 (data number);
Table created.
SQL> explain plan for insert when mod(rn, 2) = 0 then into archive_2 (data) values (data)
  2  else into archive_1 (data) values(data)
  3  select rownum as rn, data
  4  from t;
Explained.
SQL> select * from table(DBMS_XPLAN.DISPLAY);
PLAN_TABLE_OUTPUT
Plan hash value: 828723766
| Id  | Operation             | Name      | Rows  | Bytes | Cost (%CPU)| Time     |
|   0 | INSERT STATEMENT      |           | 99651 |  2530K|    31   (7)| 00:00:01 |
|   1 |  MULTI-TABLE INSERT   |           |       |       |            |          |
|   2 |   INTO                | ARCHIVE_2 |       |       |            |          |
|   3 |   INTO                | ARCHIVE_1 |       |       |            |          |
|   4 |    VIEW               |           | 99651 |  2530K|    31   (7)| 00:00:01 |
|   5 |     COUNT             |           |       |       |            |          |
|   6 |      TABLE ACCESS FULL| T         | 99651 |  1265K|    31   (7)| 00:00:01 |
SQL> insert when mod(rn, 2) = 0 then into archive_2 (data) values (data)
  2  else into archive_1 (data) values(data)
  3  select rownum as rn, data
  4  from t;
100000 rows created.Another option would be to use the last digit of rowid to split the table into two groups, but they will not be equal sized.
SQL> explain plan for select *
  2  from t
  3  where substr(rowid, length(rowid), 1) = upper(substr(rowid, length(rowid), 1))
  4  or substr(rowid, length(rowid), 1) in ('0', '1', '2', '3', '4');
Explained.
SQL> select * from table(DBMS_XPLAN.DISPLAY);
PLAN_TABLE_OUTPUT
Plan hash value: 2153619298
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
|   0 | SELECT STATEMENT  |      | 59025 |  1441K|    98  (71)| 00:00:02 |
|*  1 |  TABLE ACCESS FULL| T    | 59025 |  1441K|    98  (71)| 00:00:02 |
Predicate Information (identified by operation id):
   1 - filter(SUBSTR(ROWIDTOCHAR(ROWID),LENGTH(ROWIDTOCHAR(ROWID)),1)='0
              ' OR SUBSTR(ROWIDTOCHAR(ROWID),LENGTH(ROWIDTOCHAR(ROWID)),1)='1' OR
              SUBSTR(ROWIDTOCHAR(ROWID),LENGTH(ROWIDTOCHAR(ROWID)),1)='2' OR
              SUBSTR(ROWIDTOCHAR(ROWID),LENGTH(ROWIDTOCHAR(ROWID)),1)='3' OR
              SUBSTR(ROWIDTOCHAR(ROWID),LENGTH(ROWIDTOCHAR(ROWID)),1)='4' OR
              SUBSTR(ROWIDTOCHAR(ROWID),LENGTH(ROWIDTOCHAR(ROWID)),1)=UPPER(SUBSTR(ROW
              IDTOCHAR(ROWID),LENGTH(ROWIDTOCHAR(ROWID)),1)))
Note
   - dynamic sampling used for this statement
23 rows selected.
SQL> explain plan for select *
  2  from t
  3  where substr(rowid, length(rowid), 1) <> upper(substr(rowid, length(rowid), 1))
  4  and substr(rowid, length(rowid), 1) not in ('0', '1', '2', '3', '4');
Explained.
SQL> select * from table(DBMS_XPLAN.DISPLAY);
PLAN_TABLE_OUTPUT
Plan hash value: 2153619298
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
|   0 | SELECT STATEMENT  |      | 40627 |   991K|    41  (30)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| T    | 40627 |   991K|    41  (30)| 00:00:01 |
Predicate Information (identified by operation id):
   1 - filter(SUBSTR(ROWIDTOCHAR(ROWID),LENGTH(ROWIDTOCHAR(ROWID)),1)<>'
              0' AND SUBSTR(ROWIDTOCHAR(ROWID),LENGTH(ROWIDTOCHAR(ROWID)),1)<>'1' AND
              SUBSTR(ROWIDTOCHAR(ROWID),LENGTH(ROWIDTOCHAR(ROWID)),1)<>'2' AND
              SUBSTR(ROWIDTOCHAR(ROWID),LENGTH(ROWIDTOCHAR(ROWID)),1)<>'3' AND
              SUBSTR(ROWIDTOCHAR(ROWID),LENGTH(ROWIDTOCHAR(ROWID)),1)<>'4' AND
              SUBSTR(ROWIDTOCHAR(ROWID),LENGTH(ROWIDTOCHAR(ROWID)),1)<>UPPER(SUBSTR(RO
              WIDTOCHAR(ROWID),LENGTH(ROWIDTOCHAR(ROWID)),1)))
Note
   - dynamic sampling used for this statement
23 rows selected.
SQL> select count(*)
  2  from t
  3  where substr(rowid, length(rowid), 1) = upper(substr(rowid, length(rowid), 1))
  4  or substr(rowid, length(rowid), 1) in ('0', '1', '2', '3', '4');
  COUNT(*)
     59242
SQL> select count(*)
  2  from t
  3  where substr(rowid, length(rowid), 1) <> upper(substr(rowid, length(rowid), 1))
  4  and substr(rowid, length(rowid), 1) not in ('0', '1', '2', '3', '4');
  COUNT(*)
     40758
EDIT:
I realized that I screwed up above. In hind sight I don't know what I was thinking. I was attempting to use X = upper(X) to find the upper case characters A-Z. So the two queries to split rows based on the last character of rowid are not right. I don't have time to fix now, but wanted to leave a note of warning.
Edited by: Shannon Severance on Jul 29, 2011 1:34 AM

Similar Messages

  • Splitting a string into 4 equal parts

    Hi All,
    I have a string with maximum no of characters as 100,
    I want to split this string into 4 equal parts.
    Any help will be appreciated.
    Regards

    Hi Rajeev,
    Use this sample code
    class SplitString {
           public static void main(String[] arguments) {
              StringTokenizer ex1, ex2; // Declare StringTokenizer Objects
              int count = 0;
              String strOne = "one two  three      four   five";
              ex1 = new StringTokenizer(strOne); //Split on Space (default)
              while (ex1.hasMoreTokens()) {
                  count++;
                  wdComponentAPI.getMessageManager().reportSuccess("Token " + count + " is" +    ex1.nextToken() );
              count = 0;  // Reset counter
              String strTwo = "item one,item two,item three,item four"; // Comma Separated
              ex2 = new StringTokenizer(strTwo, ",");  //Split on comma
              while (ex2.hasMoreTokens()) {
                  count++;
                  wdComponentAPI.getMessageManager().reportSuccess("Token " + count + " is "+ ex2.nextToken() );
    Thanks
    Anup
    Edited by: Anup Bharti on Oct 27, 2008 12:36 PM

  • I need help dividing an image into two equal parts in Adobe Illustrator CC--ASAP!!!

    I need help dividing a single, already created image into 2 equal parts...Am getting nowhere so far

    Hi Willi, thanks so much for responding! Below is the image I need to divide in half. The left half where it says "Click here for the definition" links to a landing page where people can read the definition of the Hebrew Word. The right half links to an audio recording of the Hebrew word being spoken aloud. I am trying to figure out how to use the scissors or knife tool in Adobe Illustrator and am having no luck. Plus I believe there's a way to include URLs on each separated part, but I can't get past figuring out how to cut it. My background is not graphic design

  • Error when splitting a Table into two columns - Please help!

    Hi!
    I have created a table in which I have created a header which has been split into 3 parts conisting of 2 columns. These all work fine. However, when I try to split the row below the header row into 2 columns, it does so but, in a way, connects the line inbetween the two columns with the line inbetween the two columns above and will not allow me to move it left or right by manually changing specs or dragging the line. Please help! (I'm using CS5 by the way!) Thank you!

    I suggest you begin with a pre-built CSS Layout.  DW has several to help jump start your projects.  Go to File > New Blank Page > HTML.  Select a layout from the 3rd panel and hit CREATE.  See screenshot.
    Save this layout as test.html and begin building your prototype page saving and validating code often during your work sessions.
    Code Validation Tools
    CSS - http://jigsaw.w3.org/css-validator/
    HTML - http://validator.w3.org/
    Good luck with your project!
    Nancy O.

  • Splitting Long Table Into Shorter Ones

    I have a very long table in Pages and now I need to add a little text in between some of the rows.  Is there any way to split the table into a bunch of smaller ones where I need to?  Or am I going to have to recreate each new table individually somehow...?!
    Thanks so much for any help!

    You could insert a row and put your text where you want it,
    or
    Duplicate the table and delete the parts you don't want.
    Peter

  • Ask split long text into two line into same cell for ALV

    Dear All,
    Is it possible split long text into two line into same cell for ALV display data?
    Regards,
    Luke
    Moderator message: please search for information before asking.
    Edited by: Thomas Zloch on Feb 24, 2011 10:12 AM

    Keep options: Select the paragraph, then CtrlAltK (Mac: CmdOptK, I
    think).
    If you want the paragraphs in pairs, create two paragraph styles. Style
    A has keep options set to start on a new page, and also, its Next Style
    is style B.
    Style B has no keep options, but it's Next Style is Style A.
    Select all the text.
    From the flyout menu of the paragraph styles palette, apply Style A
    "then next style."
    Now all paragraphs will be alternating -- style A, style B, style A, etc.
    Now do what I wrote initially, and you'll have pairs of paragraph in
    separate text frames.

  • SLT - Splitting one source table into two tables in the destination

    Hi,
    I am wondering if we can split content of one source table into two different tables in the destination (HANA DB in my case) with SLT based on the codified mapping rules?
    We have the VBAK table in ERP which has the header information about various business objects (quote, sales order, invoice, outbound delivery to name a few). I want this to be replicated into tables specific to business object (like VBAK_QUOT, VBAK_SO, VBAK_INV, etc) based on document type column.
    There is one way to do it as far as i know - have multiple configurations and replicate to different schema. But we might have to be content with 4 different config at the max.
    Any help here will be highly appreciated
    Regards,
    Sesh

    Please take a look at these links related to your query.
    http://stackoverflow.com/questions/5145637/querying-data-by-joining-two-tables-in-two-database-on-different-servers
    http://stackoverflow.com/questions/7037228/joining-two-tables-together-in-one-database

  • HOW to enable oracle advance compression for EXIST partitioned table

    Hi All,
    I have to enable oracle advance compression for existing table which PARTITION BY RANGE then SUBPARTITION BY HASH.
    ORacle version: 11.2.0.2.0
    Please provide me any relevant doc or any exp.
    Thanks in advance.

    could not see any text for how to enable oracle advance compression for EXIST partitioned table.RTFM.
    From the resource above:
    How do I compress an existing table?
    There are multiple options available to compress existing tables. For offline compression, one could use ALTER TABLE Table_Name MOVE COMPRESS statement. A compressed copy of an existing table can be created by using CREATE TABLE Table_Name COMPRESS FOR ALL OPERATIONS AS SELECT *. For online compression, Oracle’s online redefinition utility can be used. More details for online redefinition are available here.
    "

  • Help needed with SQL to split huge data into two excel or text files.

    Hi,
    I have a table which has around 1850000 records. I want to split the data into two sets and import the data in .txt or execl file.
    How can i break upthe records using rownum or any thing
    select * from tablename where rownum<940000 fetched some records
    but
    when i gave select * from tablename where rownum>940000 it was not fetching records.
    Guideme

    when i gave select * from tablename where rownum>940000 it was not fetching records.try this
    select * from (select tablename.*, rownum rn from tablename) where rn >940000

  • How to split a file into two small file

    Hi All,
              I want to raed a file from FTP server.Then i have to split a file into two small file.File format look like this.
             R01!service_order!item_guid!resource_guid!assignment_guid
             R02!Service_order!product_id!product_discription!quantity
             R02!Service_order!product_id!product_discription!quantity
             R02!Service_order!product_id!product_discription!quantity
    i want split file into 2 file,according to header and item details.one table containt header information (label R01) and second table containt (label R02).
    can anybody help me for this.how can i split into 2 file.
    Thanks
    Vishwas Sahu

    Create 2 internal tables. sat it_header, it_detail
    Check for 1st 3 characters and if it is R01 then send it into it_header and if it comes out to be R02 then send it to it_detail.
    Once done... You can either attach these tables into mail as two seperate files
    Or you can download each internal table using GUI_DOWNLOAD.
    Hope this helps!!
    Kanchan

  • Why is it when i update my status on facebook from iohone it splits the updates into two differents boxes with some funky symbol. I have uninstall facebook and installed it over and it still does it. only the facebook IPHONE app.

    why is it when i update my status on facebook from iphone it splits the updates into two differents boxes with some funky symbols at the beginning.. I have uninstall facebook and installed it over and it still does it. only the facebook IPHONE app where it says to update staus text to FBOOK  i have already update my phone too.
    if i go on the facebook site and update there through a brower on iphone it will work fine just cant us IPHONE mobile upload,  . But its a pain of not using my IPHONE APP for facebook cause it does that.

    The warranty entitles you to complimentary phone support for the first 90 days of ownership.

  • Split one IDoc into two IDocs ?

    hello all,
    I wanna split one IDoc into two IDocs in XI?
    how can implement it?
    plx give some suggestions.
    thx in advance
    best regards
    Yaning
    Message was edited by:
            Yaning Liu

    Yaning,
    Please find the below blog for changing the ocurrences of IDOC
    /people/michal.krawczyk2/blog/2005/12/04/xi-idoc-bundling--the-trick-with-the-occurance-change
    Best regards,
    raj.

  • How to split  the records into two parts

    Hi experts,
    I have a field with 75 char length, this field have records also, Now i want to split the field into two differnt fields. That means upto first 40 char goes to one field, from 41st char to 70 char goes to another field, for that how to split record into two parts.
    Plz advice this,
    Mohana

    Hi,
    Do the following:
    f1 = fsource(40).
    f2 = fsource+40(30).
    where fsource is the 70 character original string and target strings are f1 (length 40) and f2 (length 30).
    Cheers,
    Aditya
    Edited by: Aditya Laud on Feb 22, 2008 2:10 AM

  • How to split one monitor into two, differently configured desktops

    Hello,
    I have a 27" iMac. I would like to split the screen into two differently configured desktops or monitors. Apps like TwoUp or Divy don't exactly do this. I'll explain it with an example:
    Suppose I'm working on a document and I need to open many folders to retrieve files. One common problem is that opened folders overlap each other and sometimes they overlap with the document I'm working on, or they go underneath the document. I would like to split the screen vertically in, say, two virtual, independent desktops/monitors, like this:
    - one window/space on one side (say, on the left) of the screen would contain the document  from top to bottom, with no dock bar on the bottom
    - the other window/space (right) would behave as a regular, full desktop, with the entire dock on the bottom
    In this way, if I need to navigate to find a file to use in the document, I would move the cursor to the right. The Finder would work only in this window/space, thus windows or other applications would never overlap or clutter the left side of the screen. Drag-and-drop from right to left should be possible.
    One way to imagine it is as if the 16x9 monitor were comprised by two vertical, 8x9 independent monitors side by side, each with its own configuration.
    Is this possible? Can anyone recommend an application or type of setup?
    Thank you,
    -celso

    Looking for something like this?
    You can tell Display Maid to save the positions of your open windows across many apps and later restore those positions when things become a mess. With Display Maid you don’t have to restore windows one at a time, or even one app at a time. Display Maid restores all saved window positions across all apps with one command. It will also restore window positions automatically when it detects a workspace change.
    http://www.funk-isoft.com/index.php/display-maid

  • IMessage split conversations up into two threads

    I've got an iPad 2 and iPhone 4S running iOS 7. I've got an extremely annoying issue I can't solve. iMessage split conversations up into two threads. And sometimes certain messages never reach a device. Not syncing, how it fix it.
    Thanks.

    Settings>Messages>Send/Receive...look here for what addresses/number you have listed for "You can be reached at". Are they the same? Also, on each device, "Start New Conversations", are they the same?
    Also, you don't necessarily control how the sender chooses to contact you...email or phone number.

Maybe you are looking for