Oracle Partitioned Tables and Indexes
Version 12.1.0.1

General Information
Library Note Morgan's Library Page Footer
The Library is currently in the process of being upgraded from Oracle Database Version 11.2.0.3 to 12.1.0.1. Demos are being upgraded to reflect the new Container paradigm as well as EBR (Edition Based Redefinition) and may contain references to CDBs, PDBs, and other objects you may not be familiar with such as CDB_OBJECTS_AE: Welcome to 12c.
Purpose Partitioning and subpartitioning of tables and indexes is a technique for creating a single logical entity, a table or index, mapping multiple separate segments allowing the optimizer to access a smaller number of blocks to respond to a SQL statement.

Oracle supports partitioning only for tables, indexes on tables, materialized views, and indexes on materialized views. Oracle does not support partitioning of clustered tables or indexes on clustered tables.
Partition Types
Dependencies
ALL_IND_PARTITIONS CDB_PART_KEY_COLUMNS INDPART$
ALL_IND_SUBPARTITIONS CDB_PART_LOBS INDPART_PARAM$
ALL_LOB_PARTITIONS CDB_PART_TABLES INDSUBPART$
ALL_LOB_SUBPARTITIONS CDB_SUBPARTITION_TEMPLATES INSERT_TSN_LIST$
ALL_MVIEW_DETAIL_PARTITION CDB_SUBPART_COL_STATISTICS PARTLOB$
ALL_MVIEW_DETAIL_SUBPARTITION CDB_SUBPART_HISTOGRAMS PARTOBJ$
ALL_PART_COL_STATISTICS CDB_SUBPART_KEY_COLUMNS SUBPARTCOL$
ALL_PART_HISTOGRAMS CDB_TAB_PARTITIONS TABPART$
ALL_PART_INDEXES CDB_TAB_SUBPARTITIONS TABSUBPART$
ALL_PART_KEY_COLUMNS DBA_IND_PARTITIONS USER_IND_PARTITIONS
ALL_PART_LOBS DBA_IND_SUBPARTITIONS USER_IND_SUBPARTITIONS
ALL_PART_TABLES DBA_LOB_PARTITIONS USER_LOB_PARTITIONS
ALL_SUBPARTITION_TEMPLATES DBA_LOB_SUBPARTITIONS USER_LOB_SUBPARTITIONS
ALL_SUBPART_COL_STATISTICS DBA_MVIEW_DETAIL_PARTITION USER_MVIEW_DETAIL_PARTITION
ALL_SUBPART_HISTOGRAMS DBA_MVIEW_DETAIL_SUBPARTITION USER_MVIEW_DETAIL_SUBPARTITION
ALL_SUBPART_KEY_COLUMNS DBA_PART_COL_STATISTICS USER_PART_COL_STATISTICS
ALL_TAB_PARTITIONS DBA_PART_HISTOGRAMS USER_PART_HISTOGRAMS
ALL_TAB_SUBPARTITIONS DBA_PART_INDEXES USER_PART_INDEXES
CDB_IND_PARTITIONS DBA_PART_KEY_COLUMNS USER_PART_KEY_COLUMNS
CDB_IND_SUBPARTITIONS DBA_PART_LOBS USER_PART_LOBS
CDB_LOB_PARTITIONS DBA_PART_TABLES USER_PART_TABLES
CDB_LOB_SUBPARTITIONS DBA_SUBPARTITION_TEMPLATES USER_SUBPARTITION_TEMPLATES
CDB_MVIEW_DETAIL_PARTITION DBA_SUBPART_COL_STATISTICS USER_SUBPART_COL_STATISTICS
CDB_MVIEW_DETAIL_SUBPARTITION DBA_SUBPART_HISTOGRAMS USER_SUBPART_HISTOGRAMS
CDB_PART_COL_STATISTICS DBA_SUBPART_KEY_COLUMNS USER_SUBPART_KEY_COLUMNS
CDB_PART_HISTOGRAMS DBA_TAB_PARTITIONS USER_TAB_PARTITIONS
CDB_PART_INDEXES DBA_TAB_SUBPARTITIONS USER_TAB_SUBPARTITIONS
 
Definitions
Composite Partitioning Composite partitioning is a combination of other partitioning methods. Oracle currently supports range-hash and range-list composite partitioning.
Explain Plan PSTART/PSTOP Values
KEY(I) IN subquery
KEY(SQ) Recursive subquery
Global Index A single index covering all partitions.
Hash Partitioning Enables partitioning of data that does not lend itself to range or list partitioning.
-- to view the value Oracle is using within a specific session for hashing

SELECT program, sql_hash_value, prev_hash_value
FROM gv$session;
Interval Partitioning Interval partitioning is an extension to range partitioning in which, beyond a point in time, partitions are defined by an interval. Interval partitions are automatically created by the database when data is inserted into the partition.
Invalidating Indexes By default, the following operations on partitions on a heap-organized table mark all global indexes as unusable:
  • ADD (HASH)
  • COALESCE (HASH)
  • DROP
  • EXCHANGE
  • MERGE
  • MOVE
  • SPLIT
  • TRUNCATE
List Partitioning Explicitly controls how rows map to partitions. You do this by specifying a list of discrete values for the partitioning key in the description for each partition.
Local Index Separate indexes for each partition. A local index can be unique. However, in order for a local index to be unique, the partitioning key of the table must be part of the index's key columns. Unique local indexes are useful for OLTP environments. You cannot explicitly add a partition to a local index. Instead, new partitions are added to local indexes only when you add a partition to the underlying table.
Partition Decompose a table or index into smaller, more manageable pieces, called partitions. Each partition of a table or index must have the same logical attributes, such as column names, datatypes, and constraints, but each partition can have separate physical attributes such as pctfree, pctused, and tablespaces.
Partition Key Each row in a partitioned table is unambiguously assigned to a single partition. The partition key is a set of from 1 to 16 columns that determines the partition for each row.
Partitioning Pruning Oracle optimizes SQL statements to mark the partitions or subpartitions that need to be accessed and eliminates (prunes) unnecessary partitions or subpartitions from access. Partition pruning is the skipping of unnecessary index and data partitions or subpartitions by a query.
Range Partitioning Maps data to partitions based on ranges of partition key values that you establish for each partition.
Referential Partitioning Data is mapped to partitions based on values defined in a referential constraint (foreign key)
Subpartition Partitions created within partitions. They are just partitions themselves and there is nothing special about them.
 
Tablespaces
Create demo tablespaces conn sys@pdbdev as sysdba

CREATE TABLESPACE part1
DATAFILE 'c:\temp\part01.dbf' SIZE 10M
BLOCKSIZE 8192
EXTENT MANAGEMENT LOCAL UNIFORM SIZE 256K
SEGMENT SPACE MANAGEMENT AUTO
ONLINE;

CREATE TABLESPACE part2
DATAFILE 'c:\temp\part02.dbf' SIZE 10M
BLOCKSIZE 8192
EXTENT MANAGEMENT LOCAL UNIFORM SIZE 256K
SEGMENT SPACE MANAGEMENT AUTO
ONLINE;

CREATE TABLESPACE part3
DATAFILE 'c:\temp\part03.dbf' SIZE 10M
BLOCKSIZE 8192
EXTENT MANAGEMENT LOCAL UNIFORM SIZE 256K
SEGMENT SPACE MANAGEMENT AUTO
ONLINE;

CREATE TABLESPACE part4
DATAFILE 'c:\temp\part04.dbf' SIZE 10M
BLOCKSIZE 8192
EXTENT MANAGEMENT LOCAL UNIFORM SIZE 256K
SEGMENT SPACE MANAGEMENT AUTO
ONLINE;

ALTER USER uwclass QUOTA UNLIMITED ON part1;
ALTER USER uwclass QUOTA UNLIMITED ON part2;
ALTER USER uwclass QUOTA UNLIMITED ON part3;
ALTER USER uwclass QUOTA UNLIMITED ON part4;
Drop Demo Tablespaces conn sys@pdbdev as sysdba

DROP TABLESPACE part1 INCLUDING CONTENTS AND DATAFILES;
DROP TABLESPACE part2 INCLUDING CONTENTS AND DATAFILES;
DROP TABLESPACE part3 INCLUDING CONTENTS AND DATAFILES;
DROP TABLESPACE part4 INCLUDING CONTENTS AND DATAFILES;
 
Table Partitions
Hash Partitioned Table CREATE TABLE hash_part (
prof_history_id  NUMBER(10),
person_id        NUMBER(10) NOT NULL,
organization_id  NUMBER(10) NOT NULL,
record_date      DATE NOT NULL,
prof_hist_comments VARCHAR2(2000))
PARTITION BY HASH (prof_history_id)
PARTITIONS 3
STORE IN (part1, part2, part3);

desc hash_part

SELECT table_name, tablespace_name, partitioned
FROM user_tables
ORDER BY 3;

desc user_tab_partitions

SELECT partition_name, tablespace_name
FROM user_tab_partitions;
Interval-Numeric Range Partitioned Table

Also possible are Interval-Hash and Interval-List
CREATE TABLE interval_part (
person_id   NUMBER(5) NOT NULL,
first_name  VARCHAR2(30),
last_name   VARCHAR2(30))
PARTITION BY RANGE (person_id)
INTERVAL (100) STORE IN (part1) (
PARTITION p1 VALUES LESS THAN (101))
TABLESPACE uwdata;

desc interval_part

SELECT table_name, tablespace_name, partitioned
FROM user_tables
ORDER BY 3;

col high_value format a20

SELECT table_name, partition_name, tablespace_name, high_value
FROM user_tab_partitions
ORDER BY 1, 2;

INSERT INTO interval_part
(person_id, first_name, last_name)
VALUES
(100, 'Dan', 'Morgan');

SELECT partition_name, tablespace_name, high_value
FROM user_tab_partitions
WHERE table_name = 'INTERVAL_PART';

INSERT INTO interval_part
(person_id, first_name, last_name)
VALUES
(101, 'Heli', 'Helskyaho');

SELECT partition_name, tablespace_name, high_value
FROM user_tab_partitions
WHERE table_name = 'INTERVAL_PART';

INSERT INTO interval_part
(person_id, first_name, last_name)
VALUES
(567, 'Tara', 'Havemeyer');

SELECT partition_name, tablespace_name, high_value
FROM user_tab_partitions
WHERE table_name = 'INTERVAL_PART';
Interval-Date Range Partitioned Table CREATE TABLE interval_date (
person_id NUMBER(5) NOT NULL,
last_name VARCHAR2(30),
dob       DATE)
PARTITION BY RANGE (dob)
INTERVAL (NUMTOYMINTERVAL(1,'MONTH'))
STORE IN
(part2, part4, uwdata) (
PARTITION p1 VALUES LESS THAN (TO_DATE('2008-03-15','YYYY-MM-DD')));

INSERT INTO interval_date
(person_id, last_name, dob)
VALUES
(1, 'Morgan', SYSDATE-365);

INSERT INTO interval_date
(person_id, last_name, dob)
VALUES
(2, 'Lofstrom', SYSDATE-365);

INSERT INTO interval_date
(person_id, last_name, dob)
VALUES
(3, 'Havemeyer', SYSDATE-200);

INSERT INTO interval_date
(person_id, last_name, dob)
VALUES
(4, 'Catz', SYSDATE-60);

INSERT INTO interval_date
(person_id, last_name, dob)
VALUES
(5, 'Ellison', SYSDATE+60);

col partition_name format a14
col tablespace_name format a15
col high_value format a85

SELECT partition_name, tablespace_name, high_value
FROM user_tab_partitions
WHERE table_name = 'INTERVAL_DATE';

conn sys@pdbdev as sysdba

SELECT * FROM sys.insert_tsn_list$;
Interval-Interval Range Partitioned Table with new partitions created every six months CREATE TABLE interval_interval (
program_id    NUMBER,
line_number   NUMBER,
order_date    DATE)
PARTITION BY RANGE (order_date)
INTERVAL (NUMTOYMINTERVAL(6,'MONTH'))
STORE IN (uwdata) (
PARTITION p2000 VALUES LESS THAN (TO_DATE('01-JAN-2025', 'DD-MON-YYYY')))
ENABLE ROW MOVEMENT;

INSERT INTO interval_interval
SELECT program_id, line_number, order_Date
FROM airplanes;

COMMIT;

SELECT partition_name, high_value
FROM user_tab_partitions
WHERE table_name = 'INTERVAL_INTERVAL';
Interval-Interval Range Partitioned Table with new partitions created every hour using OLTP compression CREATE TABLE hourly_interval (
datetime  DATE,
some_data NUMBER)
PARTITION BY RANGE (datetime)
INTERVAL (NUMTODSINTERVAL(1,'HOUR'))
STORE IN (part1, part2, part3) (
PARTITION p1 VALUES LESS THAN (TO_DATE('01-JAN-2015 00:00:00', 'DD-MON-YYYY HH24:MI:SS')))
COMPRESS FOR OLTP;

INSERT INTO hourly_interval
(datetime, some_data)
VALUES
(SYSDATE, 1);

INSERT INTO hourly_interval
(datetime, some_data)
VALUES
(SYSDATE+(1/24), 1);

INSERT INTO hourly_interval
(datetime, some_data)
VALUES
(SYSDATE+(1/24), 1);

INSERT INTO hourly_interval
(datetime, some_data)
VALUES
(SYSDATE+(3/24), 1);

COMMIT;

exec dbms_stats.gather_table_stats(USER,'HOURLY_INTERVAL', CASCADE=>TRUE);

col table_name format a11
col partition_name format a9
col compress_for format a8

SELECT table_name, partition_name, high_value, compression, compress_for
FROM user_tab_partitions
WHERE table_name = 'HOURLY_INTERVAL';
List Partitioned Table CREATE TABLE list_part(
deptno          NUMBER(10),
deptname        VARCHAR2(20),
quarterly_sales NUMBER(10,2),
state           VARCHAR2(2))
PARTITION BY LIST (state) (
PARTITION q1_northwest VALUES ('OR', 'WA') TABLESPACE part1,
PARTITION q1_southwest VALUES ('AZ', 'CA', 'NM') TABLESPACE part2,
PARTITION q1_northeast VALUES ('NY', 'VT', 'NJ') TABLESPACE part3,
PARTITION q1_southeast VALUES ('FL', 'GA') TABLESPACE part4,
PARTITION q1_northcent VALUES ('MN', 'WI') TABLESPACE part1,
PARTITION q1_southcent VALUES ('OK', 'TX') TABLESPACE part2);

SELECT table_name, tablespace_name, partitioned
FROM user_tables;

SELECT partition_name, tablespace_name, high_value
FROM user_tab_partitions;

INSERT INTO list_part VALUES (10, 'A', 1000, 'OR');
INSERT INTO list_part VALUES (20, 'B', 1000, 'AZ');
INSERT INTO list_part VALUES (10, 'A', 1000, 'WA');
INSERT INTO list_part VALUES (20, 'B', 1000, 'WA');
INSERT INTO list_part VALUES (10, 'A', 1000, 'AZ');
INSERT INTO list_part VALUES (20, 'B', 1000, 'CA');
COMMIT;

SELECT * FROM list_part;

SELECT * FROM list_part PARTITION(q1_northwest);
Range Partitioned Table - By Date CREATE TABLE range_part (
prof_history_id NUMBER(10),
person_id       NUMBER(10) NOT NULL,
organization_id NUMBER(10) NOT NULL,
record_date     DATE NOT NULL)
PARTITION BY RANGE (record_date) (
PARTITION yrmin VALUES LESS THAN (TO_DATE('01-JAN-2013','DD-MON-YYYY'))
TABLESPACE part1,
PARTITION yr13 VALUES LESS THAN (TO_DATE('01-JAN-2014','DD-MON-YYYY'))
TABLESPACE part2,
PARTITION yr14 VALUES LESS THAN (TO_DATE('01-JAN-2015','DD-MON-YYYY'))
TABLESPACE part3,
PARTITION yrmax VALUES LESS THAN (MAXVALUE) TABLESPACE part4);

SELECT table_name, tablespace_name, partitioned
FROM user_tables
WHERE table_name = 'RANGE_PART';

col part_name format a9
col tbsp_name format a9

SELECT partition_name PART_NAME, tablespace_name TBSP_NAME, high_value
FROM user_tab_partitions
WHERE table_name = 'RANGE_PART'
ORDER BY 2;

INSERT INTO range_part VALUES (1, 1, 1, SYSDATE-720);
INSERT INTO range_part VALUES (2, 2, 2, SYSDATE);
INSERT INTO range_part VALUES (3, 3, 3, SYSDATE+180);
INSERT INTO range_part VALUES (4, 4, 4, SYSDATE+720);

SELECT * FROM range_part;

SELECT * FROM range_part PARTITION(yrmin);
SELECT * FROM range_part PARTITION(yr13);
SELECT * FROM range_part PARTITION(yr14);
SELECT * FROM range_part PARTITION(yrmax);

conn sys@pdbdev as sysdba

ALTER SESSION SET tracefile_identifier = 'range_part';
ALTER SESSION SET EVENTS '10128 trace name context forever, level 1';
SELECT * FROM uwclass.range_part PARTITION(yr13);
ALTER SESSION SET SQL_TRACE=FALSE;

Trace file C:\APP\ORACLE\diag\rdbms\orabase2\orabase2\trace\orabase2_ora_19240_range_part.trc
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options
Windows NT Version V6.1 Service Pack 1
CPU : 4 - type 8664, 2 Physical Cores
Process Affinity : 0x0x0000000000000000
Memory (Avail/Total): Ph:2889M/8075M, Ph+PgF:10763M/16148M
Instance name: orabase2
Redo thread mounted by this instance: 1
Oracle process number: 45
Windows thread id: 19240, image: ORACLE.EXE (SHAD)


*** 2013-12-31 21:19:47.830
*** SESSION ID:(131.40091) 2013-12-31 21:19:47.830
*** CLIENT ID:() 2013-12-31 21:19:47.830
*** SERVICE NAME:(pdbdev) 2013-12-31 21:19:47.830
*** MODULE NAME:(SQL*Plus) 2013-12-31 21:19:47.830
*** ACTION NAME:() 2013-12-31 21:19:47.830
*** CONTAINER ID:(4) 2013-12-31 21:19:47.830

partition pruning descriptor (aggregate information):
level = 1
flags = {single, known}
dumping each kkpap ...
<start single kkpap dump>
  type = 0
  flags = {single, known, set by parser, }
<end single kkpap dump>
partition mapping descriptor:
  partitioning method = range
  number of partitions = 4
 
number of partitioning keys = 1
  partitioning columns = (4)
partition pruning descriptor (aggregate information):
level = 1
flags = {single, known}
dumping each kkpap ...
<start single kkpap dump>
  type = 0
  flags = {single, known, set by parser, }
<end single kkpap dump>
partition mapping descriptor:
  partitioning method = range
  number of partitions = 4
  number of partitioning keys = 1
  partitioning columns = (4)

Range Partitioned Table - By Alpha CREATE TABLE students (
student_id NUMBER(6),
student_fn VARCHAR2(25),
student_ln VARCHAR2(25),
PRIMARY KEY (student_id))
PARTITION BY RANGE (student_ln) (
PARTITION student_ae VALUES LESS THAN ('F%') TABLESPACE part1,
PARTITION student_fl VALUES LESS THAN ('M%') TABLESPACE part2,
PARTITION student_mr VALUES LESS THAN ('S%') TABLESPACE part3,
PARTITION student_sz VALUES LESS THAN (MAXVALUE) TABLESPACE part4);

SELECT table_name, tablespace_name, partitioned
FROM user_tables
ORDER BY 3,1;

col high_value format a30

SELECT partition_name, tablespace_name, high_value
FROM user_tab_partitions;
Referential Partitioned Table SELECT MIN(num_rows), MAX(num_rows)
FROM all_tables
WHERE num_rows IS NOT NULL;

CREATE TABLE ref_parent (
table_name   VARCHAR2(30),
order_date   DATE,
num_rows     NUMBER)
PARTITION BY RANGE(num_rows) (
PARTITION num_rows1 VALUES LESS THAN (100) TABLESPACE part1,
PARTITION num_rows2 VALUES LESS THAN (1000) TABLESPACE part2,
PARTITION num_rows3 VALUES LESS THAN (10000) TABLESPACE part3,
PARTITION num_rows4 VALUES LESS THAN (MAXVALUE) TABLESPACE part4);

ALTER TABLE ref_parent
ADD CONSTRAINT pk_ref_parent
PRIMARY KEY (table_name)
USING INDEX;

desc ref_parent

SELECT table_name, tablespace_name, partitioned
FROM user_tables
WHERE table_name = 'REF_PARENT';

SELECT partition_name, tablespace_name
FROM user_tab_partitions
WHERE table_name = 'REF_PARENT';

CREATE TABLE ref_child (
table_name VARCHAR2(30) NOT NULL,
index_name VARCHAR2(30) NOT NULL,
CONSTRAINT fk_ref_child_parent
FOREIGN KEY(table_name) REFERENCES ref_parent(table_name))
PARTITION BY REFERENCE(fk_ref_child_parent);

col ref_ptn_constraint_name format a30

SELECT table_name, partitioning_type, ref_ptn_constraint_name
FROM user_part_tables
WHERE table_name LIKE 'REF%';
Partition by System CREATE TABLE syst_part (
tx_id   NUMBER(5),
begdate DATE)
PARTITION BY SYSTEM (
PARTITION p1 TABLESPACE part1,
PARTITION p2 TABLESPACE part2,
PARTITION p3 TABLESPACE part3);

INSERT INTO syst_part VALUES (1, SYSDATE-10);
            *
ERROR at line 1:
ORA-14701: partition-extended name or bind variable must be used for DMLs on tables partitioned by the System method


INSERT INTO syst_part PARTITION (p1) VALUES (1, SYSDATE-10);
INSERT INTO syst_part PARTITION (p2) VALUES (2, SYSDATE);
INSERT INTO syst_part PARTITION (p3) VALUES (3, SYSDATE+10);

SELECT * FROM syst_part PARTITION(p2);
Partition by Virtual Column CREATE TABLE vcol_part (
tx_id   NUMBER(5),
begdate DATE,
enddate DATE,
staylen NUMBER(5) AS (enddate-begdate))
PARTITION BY RANGE (staylen)
INTERVAL (10) STORE IN (uwdata) (
PARTITION p1 VALUES LESS THAN (11))
TABLESPACE part3;

desc vcol_part

SELECT table_name, tablespace_name, partitioned
FROM user_tables
ORDER BY 3;

col high_value format a20

SELECT partition_name, tablespace_name, high_value
FROM user_tab_partitions
WHERE table_name = 'VCOL_PART';

desc user_tab_cols

SELECT column_name, virtual_column, data_default
FROM user_tab_cols
WHERE table_name = 'VCOL_PART';

INSERT INTO vcol_part
(tx_id, begdate, enddate)
VALUES
(1, SYSDATE-5, SYSDATE);

INSERT INTO vcol_part
(tx_id, begdate, enddate)
VALUES
(2, SYSDATE-10, SYSDATE);

INSERT INTO vcol_part
(tx_id, begdate, enddate)
VALUES
(3, SYSDATE-15, SYSDATE);

INSERT INTO vcol_part
(tx_id, begdate, enddate)
VALUES
(4, SYSDATE-25, SYSDATE);

COMMIT;

SELECT partition_name, tablespace_name, high_value
FROM user_tab_partitions
WHERE table_name = 'VCOL_PART';

EXPLAIN PLAN FOR
SELECT *
FROM vcol_part;

SELECT * FROM TABLE(dbms_xplan.display);

EXPLAIN PLAN FOR
SELECT *
FROM vcol_part
WHERE staylen < 11;

SELECT * FROM TABLE(dbms_xplan.display);

EXPLAIN PLAN FOR
SELECT *
FROM vcol_part
WHERE staylen BETWEEN 11 AND 20;

SELECT * FROM TABLE(dbms_xplan.display);

EXPLAIN PLAN FOR
SELECT *
FROM vcol_part
WHERE staylen >= 11;

SELECT * FROM TABLE(dbms_xplan.display);
Nested Table Partition <xs:element name="PurchaseOrder" type="PurchaseOrderType"
  xdb:defaultTable="PURCHASEORDER"
  xdb:tableProps =
    "VARRAY XMLDATA.LINEITEMS.LINEITEM
      STORE AS TABLE lineitem_table
        ((PRIMARY KEY (NESTED_TABLE_ID, SYS_NC_ARRAY_INDEX$)))
        PARTITION BY RANGE (XMLDATA.Reference)
          (PARTITION p1 VALUES LESS THAN (1000)
            VARRAY XMLDATA.LINEITEMS.LINEITEM
              STORE AS TABLE lineitem_p1 (STORAGE (MINEXTENTS 13)),
           PARTITION p2 VALUES LESS THAN (2000)
             VARRAY XMLDATA.LINEITEMS.LINEITEM
               STORE AS TABLE lineitem_p2 (STORAGE (MINEXTENTS 13)))"/>

CREATE TABLE purchaseorder OF XMLType
  XMLSCHEMA "http://localhost:8080/source/schemas/poSource/xsd/purchaseOrder.xsd"
  ELEMENT "PurchaseOrder"
  VARRAY "XMLDATA"."LINEITEMS"."LINEITEM" STORE AS TABLE lineitem_table
    ((PRIMARY KEY (NESTED_TABLE_ID, SYS_NC_ARRAY_INDEX$)))
    PARTITION BY RANGE (XMLDATA.Reference)
      (PARTITION p1 VALUES LESS THAN (1000)
        VARRAY "XMLDATA"."LINEITEMS"."LINEITEM" STORE AS TABLE lineitem_p1
          (STORAGE (MINEXTENTS 13)),
       PARTITION p2 VALUES LESS THAN (2000)
         VARRAY "XMLDATA"."LINEITEMS"."LINEITEM" STORE AS TABLE lineitem_p2
           (STORAGE (MINEXTENTS 13)));
*
ERROR at line 1:
ORA-31000: Resource 'http://localhost:8080/source/schemas/poSource/xsd/purchaseOrder.xsd' is not an XDB schema document
Virtual Column Partitioning with XML Binary storage CREATE TABLE orders OF XMLType
XMLTYPE STORE AS BINARY XML
VIRTUAL COLUMNS (SITE_ID AS (XMLCast(XMLQuery('/Order/@SiteId' PASSING OBJECT_VALUE RETURNING CONTENT) AS NUMBER)))
PARTITION BY RANGE (site_id) (
PARTITION p1 VALUES LESS THAN (10),
PARTITION p2 VALUES LESS THAN (20),
PARTITION pm VALUES LESS THAN (MAXVALUE));

desc orders

set describe depth all

desc orders

set describe depth 1

SELECT column_id, column_name, virtual_column, data_default
FROM user_tab_cols
WHERE table_name = 'ORDERS'
ORDER BY 1;

SELECT partition_name, high_value
FROM dba_tab_partitions
WHERE table_name = 'ORDERS';

DECLARE
 x XMLTYPE;
BEGIN
  x := XMLTYPE('<?xml version="1.0" encoding="utf-8"?>
         <Order orderId="1" orderRevision="1" orderTimeStamp="01-JAN-2012">
           <OrderHeader>
             <AlternateIds>
               <AlternateId altIdType="SiteId">2</AlternateId>
               <AlternateId altIdType="MerchantOrderNumber">Merch</AlternateId>
               <AlternateId altIdType="MarketplaceOrderNumber">Place</AlternateId>
               <AlternateId altIdType="CustomerReferenceId">Ref</AlternateId>
               <AlternateId altIdType="CartId">Cart</AlternateId>
               <AlternateId altIdType="SessionId">1</AlternateId>
             </AlternateIds>
           </OrderHeader>
         </Order>');
  INSERT INTO orders VALUES (x);

  x := XMLTYPE('<?xml version="1.0" encoding="utf-8"?>
         <Order orderId="1" orderRevision="1" orderTimeStamp="01-JAN-2012">
           <OrderHeader>
             <AlternateIds>
               <AlternateId altIdType="SiteId">245</AlternateId>
               <AlternateId altIdType="MerchantOrderNumber">Merch</AlternateId>
               <AlternateId altIdType="MarketplaceOrderNumber">Place</AlternateId>
               <AlternateId altIdType="CustomerReferenceId">Ref</AlternateId>
               <AlternateId altIdType="CartId">Cart</AlternateId>
               <AlternateId altIdType="SessionId">2</AlternateId>
             </AlternateIds>
           </OrderHeader>
         </Order>');
  INSERT INTO orders VALUES (x);
END;
/

SELECT * FROM orders PARTITION(P1);
SELECT * FROM orders PARTITION(P2);
SELECT * FROM orders PARTITION(PM);

SYS_NC_ROWINFO$
----------------------------------------------------------------------------
<?xml version="1.0" encoding="WINDOWS-1252"?>
  <Order orderId="1" orderRevision="1" orderTimeStamp="01-JAN-2012">
    <OrderHeader>
      <AlternateIds>
        <AlternateId altIdType="SiteOrderNumber">123</AlternateId>
        <AlternateId altIdType="MerchantOrderNumber">Merch</AlternateId>
        <AlternateId altIdType="MarketplaceOrderNumber">Place</AlternateId>
        <AlternateId altIdType="CustomerReferenceId">Ref</AlternateId>
        <AlternateId altIdType="CartId">Cart</AlternateId>
        <AlternateId altIdType="SessionId">123</AlternateId>
      </AlternateIds>
    </OrderHeader>
  </Order>
Partitioning on Exadata Hybrid Columnar Compression ALTER TABLE <table_name>
MODIFY PARTITION <partition_name>
COMPRESS FOR <compression_level>
STORAGE (cell_flash_cache <KEEP | NONE>);
ALTER TABLE t
MODIFY PARTITION q12012
COMPRESS FOR QUERY HIGH
STORAGE (cell_flash_cache KEEP);
 
Composite Partitions
Composite Partitioned Table - By Range And Hash conn uwclass/uwclass@pdbdev

CREATE TABLE composite_rng_hash (
cust_id     NUMBER(10),
cust_name   VARCHAR2(25),
amount_sold NUMBER(10,2),
time_id     DATE)
PARTITION BY RANGE(time_id)
SUBPARTITION BY HASH(cust_id)
SUBPARTITION TEMPLATE(
SUBPARTITION sp1 TABLESPACE part1,
SUBPARTITION sp2 TABLESPACE part2,
SUBPARTITION sp3 TABLESPACE part3,
SUBPARTITION sp4 TABLESPACE part4) (
PARTITION sales_pre11
VALUES LESS THAN (TO_DATE('01/01/2011','DD/MM/YYYY')),
PARTITION sales_2011
VALUES LESS THAN(TO_DATE('01/01/2012','DD/MM/YYYY')),
PARTITION sales_2012
VALUES LESS THAN(TO_DATE('01/01/2013','DD/MM/YYYY')),
PARTITION sales_2013
VALUES LESS THAN(TO_DATE('01/01/2014','DD/MM/YYYY')),
PARTITION sales_2014
VALUES LESS THAN(TO_DATE('01/01/2015','DD/MM/YYYY')),
PARTITION sales_future
VALUES LESS THAN(MAXVALUE));

set linesize 121
col table_name format a20

SELECT table_name, partitioned, secondary
FROM user_tables
ORDER BY 2,1;

desc user_tab_partitions

col partition_name format a15
col spc format 99999
col high_value format a50

SELECT table_name, partition_name, composite, subpartition_count SPC, high_value
FROM user_tab_partitions;

desc user_tab_subpartitions

col subpartition_name format a20

SELECT table_name, partition_name, subpartition_name, subpartition_position
FROM user_tab_subpartitions;

desc user_subpartition_templates

col high_bound format a20

SELECT subpartition_name, tablespace_name, high_bound
FROM user_subpartition_templates;

CREATE DATABASE LINK pdborcl
CONNECT TO sh IDENTIFIED BY sh
USING 'PDBORCL';

INSERT INTO composite_rng_hash
SELECT c.cust_id, c.cust_first_name || ' ' || c.cust_last_name, s.amount_sold, s.time_id+5000
FROM sh.sales@orcl s, sh.customers@orcl c
WHERE s.cust_id = c.cust_id
AND rownum < 250001;

exec dbms_stats.gather_table_stats(USER, 'COMPOSITE_RNG_HASH', 'SALES_PRE11');
exec dbms_stats.gather_table_stats(USER, 'COMPOSITE_RNG_HASH', 'SALES_2011');
exec dbms_stats.gather_table_stats(USER, 'COMPOSITE_RNG_HASH', 'SALES_2012');
exec dbms_stats.gather_table_stats(USER, 'COMPOSITE_RNG_HASH', 'SALES_2013');
exec dbms_stats.gather_table_stats(USER, 'COMPOSITE_RNG_HASH', 'SALES_2014');
exec dbms_stats.gather_table_stats(USER, 'COMPOSITE_RNG_HASH', 'SALES_FUTURE');

SELECT table_name, partition_name, num_rows
FROM user_tab_partitions;

SELECT table_name, partition_name, subpartition_name, num_rows
FROM user_tab_subpartitions;

exec dbms_stats.gather_table_stats(USER, 'COMPOSITE_RNG_HASH', GRANULARITY=>'ALL');

SELECT table_name, partition_name, subpartition_name, num_rows
FROM user_tab_subpartitions;

set long 1000000

SELECT dbms_metadata.get_ddl('TABLE', 'COMPOSITE_RNG_HASH')
FROM dual;
Composite Partitioned Table - By Range And List CREATE TABLE composite_rng_list (
cust_id     NUMBER(10),
cust_name   VARCHAR2(25),
cust_state  VARCHAR2(2),
time_id     DATE)
PARTITION BY RANGE(time_id)
SUBPARTITION BY LIST (cust_state)
SUBPARTITION TEMPLATE(
SUBPARTITION west VALUES ('OR', 'WA') TABLESPACE part1,
SUBPARTITION east VALUES ('NY', 'CT') TABLESPACE part2,
SUBPARTITION cent VALUES ('OK', 'TX') TABLESPACE part3) (
PARTITION
per1 VALUES LESS THAN (TO_DATE('01/01/2000','DD/MM/YYYY')),
PARTITION per2 VALUES LESS THAN (TO_DATE('01/01/2010','DD/MM/YYYY')),
PARTITION per3 VALUES LESS THAN (TO_DATE('01/01/2020','DD/MM/YYYY')),
PARTITION future VALUES LESS THAN(MAXVALUE));

desc composite_rng_list

SELECT table_name, partition_name, composite, high_value
FROM user_tab_partitions;

SELECT table_name, partition_name, subpartition_name, num_rows
FROM user_tab_subpartitions;
Composite Partitioned Table - By Range And Range CREATE TABLE composite_rng_rng (
cust_id     NUMBER(10),
cust_name   VARCHAR2(25),
cust_state  VARCHAR2(2),
time_id     DATE)
PARTITION BY RANGE(time_id)
SUBPARTITION BY RANGE (cust_id)
SUBPARTITION TEMPLATE(
SUBPARTITION original VALUES LESS THAN (1001) TABLESPACE part1,
SUBPARTITION acquired VALUES LESS THAN (8001) TABLESPACE part2,
SUBPARTITION recent VALUES LESS THAN (MAXVALUE) TABLESPACE part3) (
PARTITION
per1 VALUES LESS THAN (TO_DATE('01/01/2000','DD/MM/YYYY')),
PARTITION per2 VALUES LESS THAN (TO_DATE('01/01/2010','DD/MM/YYYY')),
PARTITION per3 VALUES LESS THAN (TO_DATE('01/01/2020','DD/MM/YYYY')),
PARTITION future VALUES LESS THAN (MAXVALUE));

desc composite_rng_rng

SELECT table_name, partition_name, composite, high_value
FROM user_tab_partitions;

SELECT table_name, partition_name, subpartition_name, num_rows
FROM user_tab_subpartitions;
Composite Partitioned Table - By List And Hash CREATE TABLE composite_list_hash (
cust_id     NUMBER(10),
cust_name   VARCHAR2(25),
cust_state  VARCHAR2(2),
time_id     DATE)
PARTITION BY LIST(cust_state)
SUBPARTITION BY HASH (cust_id)
SUBPARTITION TEMPLATE(
SUBPARTITION sp1 TABLESPACE part1,
SUBPARTITION sp2 TABLESPACE part2,
SUBPARTITION sp3 TABLESPACE part3,
SUBPARTITION sp4 TABLESPACE part4) (
PARTITION west VALUES ('OR', 'WA'),
PARTITION east VALUES ('NY', 'CT'),
PARTITION cent VALUES ('IL', 'MN'));
Composite Partitioned Table - By List And List CREATE TABLE composite_list_list (
cust_id     NUMBER(10),
cust_name   VARCHAR2(25),
cust_state  VARCHAR2(2),
time_id     DATE)
PARTITION BY LIST(cust_state)
SUBPARTITION BY LIST (cust_id)
SUBPARTITION TEMPLATE(
SUBPARTITION beg VALUES (1,3,5) TABLESPACE part1,
SUBPARTITION mid VALUES (2,4,6) TABLESPACE part2,
SUBPARTITION end VALUES (7,8,9,0) TABLESPACE part3) (
PARTITION west VALUES ('OR', 'WA'),
PARTITION east VALUES ('NY', 'CT'),
PARTITION cent VALUES ('IL', 'MN'));
Composite Partitioned Table - By List And Range CREATE TABLE composite_list_rng (
cust_id     NUMBER(10),
cust_name   VARCHAR2(25),
cust_state  VARCHAR2(2),
time_id     DATE)
PARTITION BY LIST(cust_state)
SUBPARTITION BY RANGE (cust_id)
SUBPARTITION TEMPLATE(
SUBPARTITION original VALUES LESS THAN (1001) TABLESPACE part1,
SUBPARTITION acquired VALUES LESS THAN (8001) TABLESPACE part2,
SUBPARTITION recent VALUES LESS THAN (MAXVALUE) TABLESPACE part3) (
PARTITION west VALUES ('OR', 'WA'),
PARTITION east VALUES ('NY', 'CT'),
PARTITION cent VALUES ('IL', 'MN'));
Composite Interval Partition CREATE TABLE t(
sequence_id      NUMBER,
reservation_date DATE,
location_code    VARCHAR2(5))
PARTITION BY RANGE (reservation_date)
INTERVAL (NUMTOYMINTERVAL(1, 'MONTH'))
SUBPARTITION BY LIST(location_code)
SUBPARTITION TEMPLATE (
SUBPARTITION spart01 VALUES ('USA'),
SUBPARTITION spart02 VALUES ('IND'),
SUBPARTITION spart03 VALUES ('GER')) (
PARTITION root VALUES LESS THAN (TO_DATE('01-JAN-2015','DD-MON-YYYY')));
 
Compressed Partitions
Partition Level Compression CREATE TABLE <table_name> (
<column_definitions>) COMPRESS
<partitioning_clauses> [NOCOMPRESS];
CREATE TABLE sales (
saleskey    NUMBER,
quarter     NUMBER,
product     NUMBER,
salesperson NUMBER,
amount      NUMBER(12,2),
region      VARCHAR2(10)) COMPRESS
PARTITION BY LIST (region) (
PARTITION northwest VALUES ('NORTHWEST'),
PARTITION southwest VALUES ('SOUTHWEST'),
PARTITION northeast VALUES ('NORTHEAST') NOCOMPRESS,
PARTITION southeast VALUES ('SOUTHEAST'),
PARTITION western VALUES ('WESTERN'));

SELECT table_name, tablespace_name, partitioned, compression
FROM user_tables;

SELECT partition_name, tablespace_name, high_value, compression
FROM user_tab_partitions;
 
Alter Table For Partitions
Moving Non-Composite Partitions ALTER TABLE <table_name>
MOVE PARTITION <partition_name>
TABLESPACE <tablespace_name>;
SELECT table_name, partition_name, tablespace_name
FROM user_tab_partitions;

ALTER TABLE hash_part
MOVE PARTITION sys_p26
TABLESPACE uwdata;

ALTER TABLE list_part
MOVE PARTITION q1_southcent
TABLESPACE uwdata NOLOGGING;

ALTER TABLE range_part
MOVE PARTITION yr0
TABLESPACE uwdata;

ALTER TABLE composite_rng_hash
MOVE PARTITION sales_pre98
TABLESPACE uwdata;

SELECT table_name, partition_name, tablespace_name
FROM user_tab_partitions;
Moving Partition and Rebuild Global Index ALTER TABLE <table_name>
MOVE SUBPARTITION <subpartition_name>
TABLESPACE <tablespace_name>
UPDATE INDEXES [(<index_name>(<partition_name>));
ALTER TABLE composite_rng_hash
MOVE PARTITION sales_pre11
TABLESPACE uwdata
UPDATE INDEXES;
Moving Subpartitions ALTER TABLE <table_name>
MOVE SUBPARTITION <subpartition_name>
TABLESPACE <tablespace_name>;
SELECT partition_name, subpartition_name, tablespace_name
FROM user_tab_subpartitions
WHERE TABLE_NAME = 'COMPOSITE_RNG_HASH';

ALTER TABLE composite_rng_hash
MOVE SUBPARTITION sales_pre11_sp1
TABLESPACE uwdata
PARALLEL (DEGREE 2);

SELECT partition_name, subpartition_name, tablespace_name
FROM user_tab_subpartitions
WHERE TABLE_NAME = 'COMPOSITE_RNG_HASH';
Setting a new default tablespace ALTER TABLE <table_owner>.<table_name>
MODIFY DEFAULT ATTRIBUTES TABLESPACE <tablespace_name>;
SELECT DISTINCT 'ALTER TABLE ' || table_owner || '.' || table_name ||
' MODIFY DEFAULT ATTRIBUTES TABLESPACE newtbs;'
FROM user_tab_partitions;
Merging List Subpartitions ALTER TABLE <table_name>
MERGE SUBPARTITIONS <subpartition_name>
INTO SUBPARTITION <subpartition_name
TABLESPACE <tablespace_name>;
ALTER TABLE composite_rng_hash
MERGE SUBPARTITIONS sales_pre11_sp1, sales_pre11_sp2
INTO SUBPARTITION sales_pre11_sp12
TABLESPACE part1;

CREATE TABLE range_list (
cust_id     NUMBER(10),
channel_id  NUMBER(3),
amount_sold NUMBER(10,2),
time_id     DATE)
PARTITION BY RANGE(time_id)
SUBPARTITION BY LIST(channel_id)
SUBPARTITION TEMPLATE(
SUBPARTITION sp1 VALUES (2, 3) TABLESPACE part1,
SUBPARTITION sp2 VALUES (4, 5) TABLESPACE part2,
SUBPARTITION sp3 VALUES (6, 7) TABLESPACE part3,
SUBPARTITION sp4 VALUES (8, 9) TABLESPACE part4)
(PARTITION sp10
VALUES LESS THAN(TO_DATE('01/01/2011','DD/MM/YYYY')),
PARTITION s11
VALUES LESS THAN(TO_DATE('01/01/2012','DD/MM/YYYY')),
PARTITION s12
VALUES LESS THAN(TO_DATE('01/01/2013','DD/MM/YYYY')),
PARTITION s13
VALUES LESS THAN(TO_DATE('01/01/2014','DD/MM/YYYY')),
PARTITION s14
VALUES LESS THAN(TO_DATE('01/01/2015','DD/MM/YYYY')),
PARTITION pm
VALUES LESS THAN(MAXVALUE));

col high_value format a20

SELECT partition_name, subpartition_name, tablespace_name, high_value
FROM user_tab_subpartitions
WHERE table_name = 'RANGE_LIST';

ALTER TABLE range_list
MERGE SUBPARTITIONS sp10_sp1, sp10_sp2 INTO SUBPARTITION spmin
PARALLEL (DEGREE 2)
TABLESPACE part1;

SELECT partition_name, subpartition_name, tablespace_name, high_value
FROM user_tab_subpartitions
WHERE table_name = 'RANGE_LIST';
Modify A Subpartition Template ALTER TABLE <table_name>
SET SUBPARTITION TEMPLATE (
SUBPARTITION <subpartition_name>, TABLESPACE <tablespace_name>,
SUBPARTITION <subpartition_name>, TABLESPACE <tablespace_name>);
SELECT partition_name, subpartition_name, tablespace_name
FROM user_tab_subpartitions
WHERE table_name = 'RANGE_LIST';

ALTER TABLE range_list
SET SUBPARTITION TEMPLATE (
SUBPARTITION sp1 VALUES (2, 3) TABLESPACE part1,
SUBPARTITION sp2 VALUES (4, 5) TABLESPACE part2,
SUBPARTITION sp3 VALUES (6, 7) TABLESPACE part3,
SUBPARTITION sp4 VALUES (8, 9) TABLESPACE part4,
SUBPARTITION sp5 VALUES (0, 1) TABLESPACE uwdata);

col partition_name format a15
col high_value format a30

SELECT partition_name, subpartition_name, tablespace_name, high_value
FROM user_tab_subpartitions
WHERE table_name = 'RANGE_LIST';

ALTER TABLE range_list DROP PARTITION sf;

ALTER TABLE range_list
ADD PARTITION s02
VALUES LESS THAN(TO_DATE('01/01/2015','DD/MM/YYYY'));

ALTER TABLE range_list
ADD PARTITION sf
VALUES LESS THAN(MAXVALUE);

SELECT partition_name, subpartition_name, tablespace_name, high_value
FROM user_tab_subpartitions
WHERE table_name = 'RANGE_LIST';

set long 1000000
SELECT dbms_metadata.get_ddl('TABLE', 'RANGE_LIST');
Change The Tablespace Name For A Future Partition ALTER TABLE <table_name>
MODIFY DEFAULT ATTRIBUTES FOR PARTITION <partition_name>
TABLESPACE <tablespace_name>;
SELECT partition_name, tablespace_name, high_value
FROM user_tab_partitions
WHERE table_name = 'RANGE_LIST';

ALTER TABLE range_list
MODIFY DEFAULT ATTRIBUTES FOR PARTITION s11 TABLESPACE part1;

SELECT partition_name, tablespace_name, high_value
FROM user_tab_partitions
WHERE table_name = 'RANGE_LIST';

SELECT partition_name, subpartition_name, tablespace_name
FROM user_tab_subpartitions
WHERE table_name = 'RANGE_LIST';
Change The Tablespace Store In List For An Interval Partitioned Table ALTER TABLE <table_name>
SET STORE IN <tablespace_name_list>;
SELECT dbms_metadata.get_ddl('TABLE', 'HOURLY_INTERVAL') FROM dual;

ALTER TABLE hourly_interval SET STORE IN (UWDATA, EXAMPLE);

SELECT dbms_metadata.get_ddl('TABLE', 'HOURLY_INTERVAL') FROM dual;

ALTER TABLE hourly_interval SET STORE IN (UWDATA, EXAMPLE, USERS);

SELECT dbms_metadata.get_ddl('TABLE', 'HOURLY_INTERVAL') FROM dual;
Modify A List Partitioned List ALTER TABLE <table_name>
MODIFY PARTITION <partition_name>
ADD VALUES (<values_list>);
SELECT partition_name, tablespace_name, high_value
FROM user_tab_partitions
WHERE table_name = 'LIST_PART';

ALTER TABLE list_part
MODIFY PARTITION q1_northcent
ADD VALUES ('MI', 'OH');

SELECT partition_name, tablespace_name, high_value
FROM user_tab_partitions
WHERE table_name = 'LIST_PART';
Drop Values From A List Partitioned List ALTER TABLE <table_name>
MODIFY PARTITION <partition_name>
DROP VALUES (<values_list>);
ALTER TABLE list_part
MODIFY PARTITION q1_southwest
DROP VALUES ('NM');

SELECT partition_name, tablespace_name, high_value
FROM user_tab_partitions
WHERE table_name = 'LIST_PART';
Convert a partition into a stand-alone table ALTER TABLE <table_name>
EXCHANGE PARTITION <partition_name>
WITH TABLE <new_table_name>
<including | excluding> INDEXES
<with | without> VALIDATION
EXCEPTIONS INTO <schema.table_name>;
SELECT table_name, partition_name, num_rows
FROM user_tab_partitions
WHERE table_name = 'LIST_PART';

CREATE TABLE q1_northwest AS
SELECT *
FROM list_part
WHERE 1=2;

SELECT * FROM list_part;

SELECT * FROM list_part PARTITION(q1_northwest);

ALTER TABLE list_part
EXCHANGE PARTITION q1_northwest WITH TABLE q1_northwest
INCLUDING INDEXES
WITHOUT VALIDATION
EXCEPTIONS INTO uwclass.problems;

SELECT * FROM q1_northwest;

SELECT * FROM list_part;
Convert a stand-alone table into a partition ALTER TABLE <table_name>
EXCHANGE PARTITION <partition_name> WITH TABLE <table_name>
[INCLUDING INDEXES <WITH | WITHOUT> VALIDATION];
CREATE TABLE range_part (
rid  NUMBER,
col1 VARCHAR2(10),
col2 VARCHAR2(100))
PARTITION BY RANGE(rid) (
partition p1 VALUES LESS THAN (1000),
partition p3 VALUES LESS THAN (3000),
partition pm VALUES LESS THAN (MAXVALUE));

CREATE TABLE new_part (
rid  NUMBER,
col1 VARCHAR2(10),
col2 VARCHAR2(100));

INSERT /*+ APPEND ORDERED FULL(s1) USE_NL(s2) */
INTO new_part
SELECT 3000 + TRUNC((rownum-1)/500,6), TO_CHAR(rownum), RPAD('x',100)
FROM sys.source$ s1, sys.source$ s2
WHERE rownum <= 100000;

COMMIT;

SELECT COUNT(*) FROM range_part;
SELECT COUNT(*) FROM new_part;

col high_value format a20

SELECT table_name, partition_name, high_value
FROM user_tab_partitions;

set timing on

ALTER TABLE range_part
EXCHANGE PARTITION pm WITH TABLE new_part;

set timing off
DROP TABLE range_part PURGE;
DROP TABLE new_part PURGE;

-- recreate and populate tables

set timing on

ALTER TABLE range_part
EXCHANGE PARTITION pm WITH TABLE new_part
WITHOUT VALIDATION;

-- again drop the tables, recreate, and load them

-- add some realistic constraints

ALTER TABLE range_part
ADD CONSTRAINT pk_range_part
PRIMARY KEY(rid)
USING INDEX LOCAL;

ALTER TABLE new_part
ADD CONSTRAINT pk_new_part
PRIMARY KEY(rid)
USING INDEX;

set timing on

ALTER TABLE range_part
EXCHANGE PARTITION pm WITH TABLE new_part
INCLUDING INDEXES WITHOUT VALIDATION;

-- repeat again but this time do the following before the exchange
ALTER TABLE range_part MODIFY PRIMARY KEY NOVALIDATE;
ALTER TABLE new_part MODIFY PRIMARY KEY NOVALIDATE;

set timing on

ALTER TABLE range_part
EXCHANGE PARTITION pm WITH TABLE new_part
INCLUDING INDEXES WITHOUT VALIDATION;
Rename a partition ALTER TABLE <table_name>
RENAME PARTITION <existing_partition_name>
TO <new_partition_name>;
SELECT table_name, partition_name
FROM user_tab_partitions;

ALTER TABLE range_list RENAME PARTITION sf TO sales_future;

SELECT table_name, partition_name
FROM user_tab_partitions;
Split Partition

Note: If splitting the MAXVALUE partition perform a first split at a value higher than the maximum value in the MAXVALUE partition.
ALTER TABLE <table_name>
SPLIT PARTITION <partition_name>
AT <range_definition>
INTO (PARTITION <first_partition>, PARTITION <second_partition>)
UPDATE GLOBAL INDEXES;
SELECT table_name, partition_name, high_value
FROM user_tab_partitions
WHERE table_name = 'RANGE_PART';

INSERT INTO range_part VALUES (1, 1, 1, TO_DATE('01-JAN-1998'), 'A');
INSERT INTO range_part VALUES (1, 1, 1, TO_DATE('01-JAN-1999'), 'A');
INSERT INTO range_part VALUES (1, 1, 1, TO_DATE('01-JAN-2000'), 'A');
INSERT INTO range_part VALUES (1, 1, 1, TO_DATE('01-JAN-2001'), 'A');
INSERT INTO range_part VALUES (1, 1, 1, TO_DATE('15-MAR-2001'), 'A');
INSERT INTO range_part VALUES (1, 1, 1, TO_DATE('16-SEP-2001'), 'A');
INSERT INTO range_part VALUES (1, 1, 1, TO_DATE('20-DEC-2001'), 'A');
INSERT INTO range_part VALUES (1, 1, 1, TO_DATE('01-JAN-2002'), 'A');
INSERT INTO range_part VALUES (1, 1, 1, TO_DATE('01-JAN-2003'), 'A');
COMMIT;

col ph_comments format a10

SELECT * FROM range_part;

SELECT * FROM range_part PARTITION(yr2a);

ALTER TABLE range_part
SPLIT PARTITION yr2
AT (TO_DATE('30-JUN-2001','DD-MON-YYYY'))
INTO (PARTITION yr2a, PARTITION yr2b)
UPDATE GLOBAL INDEXES;

SELECT * FROM range_part PARTITION(yr2a);
SELECT * FROM range_part PARTITION(yr2b);

SELECT table_name, partition_name, high_value
FROM user_tab_partitions
WHERE table_name = 'RANGE_PART';
Truncate a partition Follow the TRUNCATE PARTITION link at page bottom
Split An LOB Partition ALTER TABLE <table_name>
SPLIT PARTITION <partition_name> AT <split location> INTO
(PARTITION <new_partition_name> TABLESPACE <tablespace_name>"
LOB <column_name> STORE AS (TABLESPACE <tablespace_name>),
PARTITION <new_partition_name>
LOB (<column_name>) STORE AS (TABLESPACE <tablespace_name>);
CREATE TYPE adheader_typ AS OBJECT (
header_name   VARCHAR2(256),
creation_date DATE,
header_text   VARCHAR2(1024),
logo          BLOB);
/

CREATE TABLE print_media_part (
product_id       NUMBER(6),
ad_id            NUMBER(6),
ad_composite     BLOB,
ad_sourcetext    CLOB,
ad_finaltext     CLOB,
ad_fltextn       NCLOB,
ad_textdocs_ntab TEXTDOC_TAB,
ad_photo         BLOB,
ad_graphic       BFILE,
ad_header        ADHEADER_TYP)
NESTED TABLE ad_textdocs_ntab STORE AS textdoc_nt
PARTITION BY RANGE (product_id) (
PARTITION p1 VALUES LESS THAN (100),
PARTITION p2 VALUES LESS THAN (200));

ALTER TABLE print_media_part
SPLIT PARTITION p2 AT (150) INTO (
PARTITION p2a TABLESPACE uwdata
LOB (ad_photo, ad_composite) STORE AS (TABLESPACE part1),
PARTITION p2b
LOB (ad_photo, ad_composite) STORE AS (TABLESPACE part2));
Coalesce Hash Partitions ALTER TABLE <table_name> COALESCE PARTITION;
ALTER TABLE hash_part COALESCE PARTITION;
Add Partition And Specify BLOB/LOB Storage ALTER TABLE <table_name>
ADD PARTITION <new_partition_name> VALUES LESS THAN (MAXVALUE)
LOB (<column_name>) STORE AS (TABLESPACE <tablespace_name);
ALTER TABLE print_media_part
ADD PARTITION p3 VALUES LESS THAN (MAXVALUE)
LOB (ad_photo, ad_composite)      STORE AS (TABLESPACE part3)
LOB (ad_sourcetext, ad_finaltext) STORE AS (TABLESPACE part4);
 
Index Partitions
Global Index Creation CREATE INDEX <index_name>
ON <table_name> <column_name_list>;
SELECT i.index_name, i.composite, i.partition_name, i.high_value
FROM user_ind_partitions i, user_tab_partitions t
WHERE i.partition_name = t.partition_name
AND t.table_name = 'RANGE_PART';

SELECT partition_name
FROM user_tab_partitions
WHERE table_name = 'RANGE_PART';

CREATE INDEX gi_range_part_person_id
ON range_part (person_id);

SELECT index_name, partitioned
FROM user_indexes
WHERE table_name = 'RANGE_PART';

DROP INDEX gi_range_part_person_id;
Local Index Creation and Partition Pruning Demo CREATE INDEX <index_name>
ON <table_name> <column_name_list> LOCAL;
CREATE INDEX li_range_part_person_id
ON range_part (person_id)
LOCAL;

SELECT index_name, partitioned
FROM user_indexes
WHERE table_name = 'RANGE_PART';

SELECT ip.index_name, ip.composite, ip.partition_name, ip.high_value
FROM user_ind_partitions ip, user_indexes ui
WHERE ip.index_name = ui.index_name
AND ui.table_name = 'RANGE_PART';

DROP INDEX li_range_part_person_id;

CREATE INDEX li_range_part_person_id
ON range_part (person_id)
LOCAL (
PARTITION yr0  TABLESPACE part1,
PARTITION yr1  TABLESPACE part2,
PARTITION yr2a TABLESPACE part3,
PARTITION yr2b TABLESPACE part4,
PARTITION yr9  TABLESPACE uwdata);

col tablespace_name format a15

SELECT ip.index_name, ip.partition_name, ip.tablespace_name, ip.high_value
FROM user_ind_partitions ip, user_indexes ui
WHERE ip.index_name = ui.index_name
AND ui.table_name = 'RANGE_PART';

SELECT * FROM range_part;

SELECT * FROM range_part PARTITION(yr2a);

EXPLAIN PLAN FOR
SELECT *
FROM range_part
WHERE record_date BETWEEN TO_DATE('01-JAN-1998') AND TO_DATE('31-JAN-1998');

SELECT * FROM TABLE(dbms_xplan.display);

EXPLAIN PLAN FOR
SELECT *
FROM range_part
WHERE record_date BETWEEN TO_DATE('01-JAN-1998') AND TO_DATE('31-DEC-2000');

SELECT * FROM TABLE(dbms_xplan.display);

EXPLAIN PLAN FOR
SELECT *
FROM range_part
WHERE record_date BETWEEN TO_DATE('01-JAN-1999') AND TO_DATE('31-DEC-2002');

SELECT * FROM TABLE(dbms_xplan.display);
Global Partition Index Creation CREATE INDEX <index_name>
ON <table_name> <column_name_list>
GLOBAL PARTITION BY RANGE (partition_column_name_list) (
PARTITION <partition_name> VALUES <condition>);
DROP INDEX li_range_part_person_id;

UPDATE range_part
SET organization_id = ROWNUM;

col ph_comments format a15

SELECT * FROM range_part;

CREATE INDEX gi_range_part_person_id
ON range_part (organization_id)
GLOBAL PARTITION BY RANGE(organization_id) (
PARTITION p1 VALUES LESS THAN(4)
TABLESPACE part1,
PARTITION p2 VALUES LESS THAN(MAXVALUE)
TABLESPACE part2);

col high_value format a20

SELECT ip.index_name, ip.partition_name, ip.tablespace_name, ip.high_value
FROM user_ind_partitions ip, user_indexes ui
WHERE ip.index_name = ui.index_name
AND ui.table_name = 'RANGE_PART';
Query for Unusable Indexes SELECT index_name, partition_name, status
FROM user_ind_partitions;
 
Alter Table and Index For Partitions
Rebuild Local All Local Indexes On A Table ALTER TABLE <table_name>
MODIFY PARTITION <partition_name>
REBUILD UNUSABLE LOCAL INDEXES;
CREATE INDEX li_range_part_person_id
ON range_part (person_id)
LOCAL;

SELECT t.table_name, i.index_name, i.partition_name, i.status
FROM user_ind_partitions i, user_tab_partitions t
WHERE i.partition_name = t.partition_name;

ALTER TABLE range_part
MODIFY PARTITION yr0
REBUILD UNUSABLE LOCAL INDEXES;
Rebuild any unusable local index partitions associated with a hash partition at the specific composite partitioned table subpartition level ALTER TABLE <table_name>
MODIFY SUBPARTITION <subpartition_name>
REBUILD UNUSABLE LOCAL INDEXES;
SELECT i.table_name, s.index_name, s.partition_name, s.status
FROM user_ind_subpartitions s, user_indexes i
WHERE s.index_name = i.index_name;

ALTER TABLE composite_rng_hash
MODIFY SUBPARTITION sales_1999_sp4
REBUILD UNUSABLE LOCAL INDEXES;
Rebuild (and move) a local partition index ALTER INDEX <index_name>
REBUILD PARTITION <partition_name>
TABLESPACE <new_tablespace_name>;
col partition_name format a10
col tablespace_name format a20

SELECT i.table_name, s.index_name, s.tablespace_name, s.partition_name, s.status
FROM user_ind_partitions s, user_indexes i
WHERE s.index_name = i.index_name;

ALTER INDEX
li_range_part_person_id
REBUILD PARTITION yr2
TABLESPACE uwdata;

SELECT i.table_name, s.index_name, s.tablespace_name, s.partition_name, s.status
FROM user_ind_partitions s, user_indexes i
WHERE s.index_name = i.index_name;
Setting a new default tablespace ALTER INDEX <index_owner>.<index_name>
MODIFY DEFAULT ATTRIBUTES TABLESPACE <tablespace_name>;
SELECT DISTINCT 'ALTER INDEX ' || index_owner || '.' || index_name ||
       'MODIFY DEFAULT ATTRIBUTES TABLESPACE newtbs;'
FROM user_ind_partitions;
 
Drop Partition
Drop partition from a partitioned table ALTER TABLE DROP PARTITION <partition_name> [UPDATE GLOBAL INDEXES];
SELECT table_name, partition_name
FROM user_tab_partitions;

ALTER TABLE range_list DROP PARTITION s2k UPDATE GLOBAL INDEXES;
 
Demos
Partition Elimination conn scott/tiger@pdborcl

-- create a list partitioned table
CREATE TABLE partdemo (
empno    NUMBER(4) NOT NULL,
ename    VARCHAR2(10),
job      VARCHAR2(9),
mgr      NUMBER(4),
hiredate DATE,
sal      NUMBER(7, 2),
comm     NUMBER(7, 2),
deptno   NUMBER(2))
partition by list(deptno)(
partition p1 values (10,30) tablespace uwdata,
partition p2 values (20,40) tablespace example);

INSERT INTO partdemo SELECT * FROM scott.emp;

set linesize 121

SELECT * FROM partdemo;

SELECT * FROM partdemo PARTITION(p1);

SELECT * FROM partdemo PARTITION(p2);

-- take the example tablespace OFFLINE to examine partition elimination
conn sys@pdborcl as sysdba

ALTER TABLESPACE example OFFLINE;

conn scott/tiger@pdborcl

SELECT COUNT(*) FROM partdemo;
SELECT COUNT(*) FROM partdemo WHERE deptno = 10;
SELECT COUNT(*) FROM partdemo WHERE deptno BETWEEN 1 AND 19;
SELECT COUNT(*) FROM partdemo WHERE deptno BETWEEN 1 AND 20;
SELECT COUNT(*) FROM partdemo WHERE deptno IN(10,30);

conn sys@pdborcl as sysdba

ALTER TABLESPACE example ONLINE;
Partitioning and READ ONLY Tablespaces CREATE TABLE ropt (
rid     NUMBER(5),
datecol DATE,
msgcol  VARCHAR2(20))
PARTITION BY RANGE (datecol) (
PARTITION p2009 VALUES LESS THAN (TO_DATE('01-JAN-2010','DD-MON-YYYY'))
TABLESPACE part1,
PARTITION p2010 VALUES LESS THAN (TO_DATE('01-JAN-2011','DD-MON-YYYY'))
TABLESPACE part2,
PARTITION pmax VALUES LESS THAN (MAXVALUE) TABLESPACE uwdata);

INSERT INTO ropt VALUES (1, TO_DATE('01-JUL-2009', 'DD-MON-YYYY'), 'validate');
INSERT INTO ropt VALUES (2, TO_DATE('01-JUL-2010', 'DD-MON-YYYY'), 'assumptions');
INSERT INTO ropt VALUES (3, TO_DATE('01-JUL-2011', 'DD-MON-YYYY'), 'by testing');
COMMIT;

SELECT * FROM ropt;

CREATE OR REPLACE FUNCTION part_status(rid_in IN rowid) RETURN VARCHAR2 IS
 tspstat user_tablespaces.status%TYPE;
BEGIN
  SELECT uts.status
  INTO tspstat
  FROM dba_data_files ddf, user_tablespaces uts
  WHERE ddf.relative_fno = dbms_rowid.rowid_relative_fno(rid_in)
  AND ddf.file_id = dbms_rowid.rowid_to_absolute_fno(rid_in, USER, 'ROPT')
  AND ddf.tablespace_name = uts.tablespace_name;

  RETURN tspstat;
END part_status;
/

SELECT ilv.*, dts.status
FROM (
  SELECT ropt.*,
         dbms_rowid.rowid_relative_fno(rowid) rfno,
         dbms_rowid.rowid_to_absolute_fno(rowid,user,'ROPT') afno
  FROM ropt) ilv,
  dba_data_files ddf,
  dba_tablespaces dts
WHERE ddf.relative_fno = rfno
AND ddf.file_id = afno
AND ddf.tablespace_name = dts.tablespace_name;

col part_status format a20

SELECT ropt.*, part_status(ropt.rowid) PART_STATUS
FROM ropt;

ALTER TABLESPACE part2 READ ONLY;

SELECT ilv.*, dts.status
FROM (
  SELECT ropt.*,
         dbms_rowid.rowid_relative_fno(rowid) rfno,
         dbms_rowid.rowid_to_absolute_fno(rowid,user,'ROPT') afno
  FROM ropt) ilv,
  dba_data_files ddf,
  dba_tablespaces dts
WHERE ddf.relative_fno = rfno
AND ddf.file_id = afno
AND ddf.tablespace_name = dts.tablespace_name;

SELECT ropt.*, part_status(ropt.rowid) PART_STATUS
FROM ropt;

ALTER TABLESPACE users READ WRITE;
Maximize Storage by Utilizing Zero Size Unusable Indexes CREATE TABLE range_part (
prof_history_id NUMBER(10),
record_date DATE NOT NULL)
PARTITION BY RANGE (record_date) (
PARTITION yr0 VALUES LESS THAN (TO_DATE('01-JAN-2010','DD-MON-YYYY')),
PARTITION yr1 VALUES LESS THAN (TO_DATE('01-JAN-2011','DD-MON-YYYY')),
PARTITION yr2 VALUES LESS THAN (TO_DATE('01-JAN-2012','DD-MON-YYYY')),
PARTITION yr9 VALUES LESS THAN (MAXVALUE));

SELECT segment_name
FROM user_segments
WHERE segment_type LIKE '%PARTITION';

CREATE INDEX ix_range_part_phid
ON range_part(prof_history_id)
LOCAL UNUSABLE;

SELECT segment_name
FROM user_segments
WHERE segment_type LIKE '%PARTITION';

SELECT index_name
FROM user_part_indexes;

ALTER INDEX ix_range_part_phid REBUILD PARTITION yr1;

SELECT segment_name
FROM user_segments
WHERE segment_type LIKE '%PARTITION';

ALTER INDEX ix_range_part_phid REBUILD PARTITION yr2;

SELECT segment_name
FROM user_segments
WHERE segment_type LIKE '%PARTITION';

ALTER INDEX ix_range_part_phid MODIFY PARTITION yr1 UNUSABLE;

SELECT segment_name
FROM user_segments
WHERE segment_type LIKE '%PARTITION';

Related Topics
DATAOBJ_TO_PARTITION
DBMS_PCLXUTIL
DBMS_STATS
Hybrid Columnar Compression
Indexes
Secure Files
TBL$OR$IDX$PART$NUM
Tables
Truncate Partition
Update Statements

Morgan's Library Page Footer
This site is maintained by Dan Morgan. Last Updated: This site is protected by copyright and trademark laws under U.S. and International law. © 1998-2014 Daniel A. Morgan All Rights Reserved