A non-partitioned table can be converted to a partitioned table with a MODIFY
clause added to the ALTER TABLE SQL statement.
In addition, the keyword ONLINE
can be specified, enabling concurrent DML operations while the conversion is ongoing.</br>
Also note for the UPDATE INDEXES
clause
Here is an example of online conversion:
ALTER TABLE students MODIFY
PARTITION BY RANGE (student_id) INTERVAL (100)
( PARTITION P1 VALUES LESS THAN (100),
PARTITION P2 VALUES LESS THAN (500)
) ONLINE
UPDATE INDEXES
( IDX1_SALARY LOCAL,
IDX2_EMP_ID GLOBAL PARTITION BY RANGE (employee_id)
( PARTITION IP1 VALUES LESS THAN (MAXVALUE))
);
UPDATE INDEXES
clause (taken from Oracle documentation)UPDATE INDEXES
clause is optional.
Indexes are maintained both for the online and offline conversion to a partitioned table.Prefixed means that the partition key columns are included in the index definition, but the index definition is not limited to including the partitioning keys only.
The conversion operation cannot be performed if there are domain indexes.
-- 1) Create the new partitioned table
CREATE TABLE partitioned_table ( id NUMBER, name VARCHAR2(50), date_col DATE)
PARTITION BY RANGE (date_col)
(
PARTITION p1 VALUES LESS THAN (TO_DATE('2022-01-01', 'YYYY-MM-DD')),
PARTITION p2 VALUES LESS THAN (TO_DATE('2022-06-01', 'YYYY-MM-DD')),
PARTITION p3 VALUES LESS THAN (TO_DATE('2022-12-01', 'YYYY-MM-DD')),
PARTITION p4 VALUES LESS THAN (TO_DATE('2023-06-01', 'YYYY-MM-DD'))
);
--2. Insert the data from the non-partitioned table into the partitioned table
INSERT INTO partitioned_table SELECT * FROM non_partitioned_table;
--3. Rename the original non-partitioned table to a temporary name
ALTER TABLE non_partitioned_table RENAME TO temp_non_partitioned_table;
--4. Rename the new partitioned table to the original name
ALTER TABLE partitioned_table RENAME TO non_partitioned_table;
--5. Drop the temporary non-partitioned table
DROP TABLE temp_non_partitioned_table;