Creating a clustered-index in Oracle using an IOT? Confusion

前端 未结 1 680
-上瘾入骨i
-上瘾入骨i 2021-01-23 14:26

I\'m having a little trouble understanding how to use an IOT as a clustered index in oracle. Say I have a table order(order_id,part_id,customer_id,order_date), and I want to cre

相关标签:
1条回答
  • 2021-01-23 15:14

    Quote from Oracle's Concepts Guide

    "... In an index-organized table, rows are stored in an index defined on the primary key for the table. Each index entry in the B-tree also stores the non-key column values. Thus, the index is the data, and the data is the index."

    Suppose we have an ORDER table as described in your question.

    -- create table ORDER_, with test data
    -- table name with trailing underscore avoids ORA-00903: invalid table name
    create table order_
    as
    select 
      level * 10000 + trunc( dbms_random.value * 100 ) order_id
    , trunc( dbms_random.value * 100000 )              part_id
    , dbms_random.string( 'x', 10 )                    customer_id
    , trunc( sysdate + level * 10 )                    order_date
    from dual connect by level <= 10 ;
    

    Test data

    SQL> select * from order_ ;
    ORDER_ID  PART_ID  CUSTOMER_ID  ORDER_DATE  
    10069     74711    KBGHAHWTL8   27-MAR-18   
    20034     99571    7VUNFJER44   06-APR-18   
    30038     64160    ORXP2RRA3K   16-APR-18   
    40005     81247    B9N43NSVQ7   26-APR-18   
    50019     90889    8H5G12D82E   06-MAY-18   
    60017     34107    9O4OSETJ4H   16-MAY-18   
    70078     53959    77MUCKJW82   26-MAY-18   
    80015     9496     U5J6Z85KXR   05-JUN-18   
    90081     88450    2LEUPZGFOS   15-JUN-18   
    100031    38487    NX4BHBF3TN   25-JUN-18  
    

    If you now just create an IOT (index organized table), it will be empty.

    -- your original code
    CREATE TABLE clust_order(
        order_id number,
        part_id number,
        CONSTRAINT part_pk PRIMARY KEY (part_id)
    )ORGANIZATION INDEX;
    
    Table CLUST_ORDER created.
    
    SQL> select * from clust_order ;
    
    no rows selected
    

    What you could do instead, is: create the IOT by SELECTing from the original table (see also: Parallelizing Index-Organized Table Creation here).

    create table clust_order 
    ( 
      part_id constraint part_pk primary key
    , order_id 
    ) 
    organization index
    parallel
    as
    select 
      part_id
    , order_id 
    from order_;
    

    The resulting IOT contains ...

    SQL> select * from clust_order;
    PART_ID  ORDER_ID  
    9496     80015     
    34107    60017     
    38487    100031    
    53959    70078     
    64160    30038     
    74711    10069     
    81247    40005     
    88450    90081     
    90889    50019     
    99571    20034 
    

    You may find this discussion useful.

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