The Database Design Resource Center



Oracle Partitions : Divide and increase performance!

Oracle partitions addresses the key problem of supporting very large tables and indexes by allowing you to decompose them into smaller and more manageable pieces called partitions.

Once partitions are defined, SQL statements can access and manipulate the partitions rather than entire tables or indexes.

Partitions are especially useful in data warehouse applications, which commonly store and analyze large amounts of historical data.

How do we create and manage partitions?


Oracle partitions using DML:

The following DML statements contain an optional partition specification for non-remote partitioned tables:

INSERT
UPDATE
DELETE
LOCK TABLE
SELECT

For example:

SELECT * FROM schema.table PARTITION(part_name);

This syntax provides a simple way of viewing individual partitions as tables: A view can be created which selects from just one partition using the partition-extended table name, and this view can be used in lieu of a table.

With such views you can also build partition-level access control mechanisms by granting (revoking) privileges on these views to (from) other users or roles.

The use of partition-extended table names has the following restrictions:

  • A partition-extended table name cannot refer to a remote schema object.
  • The partition-extended table name syntax is not supported by PL/SQL.
  • A partition extension must be specified with a base table. No synonyms, views, or any other schema objects are allowed.

In order to provide partition independence for DDL and utility operations, Oracle supports DML partition locks.

Partition independence allows you to perform DDL and utility operations on selected partitions without disturbing activities on other partitions.

The purpose of a partition lock is to protect the data in an individual partition while multiple users are accessing that partition or other partitions in the table concurrently.

Managing Oracle partitions:

Create a partitioned table:

Creating Oracle partitions is very similar to creating a table or index. You must use the CREATE TABLE statement with the PARTITION CLAUSE.

The first step to create a partitioned table would be to identify the column(s) to partition on and the range of values which go to each partition. Then you determine the tablespaces where each partition should go.

Here is a script to create a simple partitioned table:

CREATE TABLE AA_GENERAL_ATTENDANCE
(GL_MARKS_MONTH NUMBER (4),
GL_BATCH VARCHAR2(4),
GL_JIB VARCHAR2(1),
... ... ... ... GLR_OVER_UNDER_IND VARCHAR2(1))
PCTFREE 0 PCTUSED 40 INITRANS 1
STORAGE(INITIAL 250M NEXT 10M MINEXTENTS 1
MAXEXTENTS 1000 PCTINCREASE 0 )
PARTITION BY RANGE (GL_MARKS_MONTH)
(PARTITION SSTN7912 VALUES LESS THAN (8000)
TABLESPACE SSTN 7912
STORAGE (INITIAL 100M NEXT 10M PCTINCREASE 0)
, PARTITION SSTN 8012 VALUES LESS THAN (8100)
TABLESPACE SSTN 8012,
PARTITION SSTN 8112 VALUES LESS THAN (8200)
TABLESPACE SSTN 8112,
PARTITION SSTN 8212 VALUES LESS THAN (8300)
TABLESPACE SSTN 8212,
... ... ... ...
PARTITION SSTN 9712 VALUES LESS THAN (9800)
TABLESPACE SSTN 9712,
PARTITION SSTN 9801 VALUES LESS THAN (MAXVALUE)
TABLESPACE SSTN 9801
STORAGE (INITIAL 50M NEXT 5M PCTINCREASE 0)
);

Moving Oracle partitions:

You can use the MOVE PARTITION clause to move a partition. For example, a DBA wishes to move the most active partition to a tablespace that resides on its own disk (in order to balance I/O).

The DBA can issue the following statement:

ALTER TABLE aaa MOVE PARTITION bbb
TABLESPACE rrr NOLOGGING;

This statement always drops the partition's old segment and creates a new segment, even if you don't specify a new tablespace.

When the partition you are moving contains data, MOVE PARTITION marks the matching partition in each local index, and all global index partitions as unusable. You must rebuild these index partitions after issuing MOVE PARTITION.

Adding Oracle partitions:

You can use the ALTER TABLE ADD PARTITION statement to add a new partition to the "high" end.

If you wish to add a partition at the beginning or in the middle of a table, or if the partition bound on the highest partition is MAXVALUE, you should instead use the SPLIT PARTITION statement.

When the partition bound on the highest partition is anything other than MAXVALUE, you can add a partition using the ALTER TABLE ADD PARTITION statement.

ALTER TABLE edu
ADD PARTITION jan99 VALUES LESS THAN ( '990201' )
TABLESPACE tsjan99;

When there are local indexes defined on the table and you issue the ALTER TABLE ... ADD PARTITION statement, a matching partition is also added to each local index.

Since Oracle assigns names and default physical storage attributes to the new index partitions, you may wish to rename or alter them after the ADD operation is complete.

Dropping Oracle partitions:

You can use the ALTER TABLE DROP PARTITION statement to drop Oracle partitions.

If there are local indexes defined for the table, ALTER TABLE DROP PARTITION also drops the matching partition from each local index.

You cannot explicitly drop a partition for a local index. Instead, local index partitions are dropped only when you drop a partition from the underlying table.

If, however, the partition contains data and global indexes, and you leave the global indexes in place during the ALTER TABLE DROP PARTITION statement which marks all global index partitions unusable, you must rebuild them afterwards.

Truncating Partitioned Tables:

You can use the ALTER TABLE TRUNCATE PARTITION statement to remove all rows from a table partition with or without reclaiming space.

If there are local indexes defined for this table, ALTER TABLE TRUNCATE PARTITION also truncates the matching partition from each local index.

Splitting Oracle partitions:

You can split a table partition by issuing the ALTER TABLE SPLIT PARTITION statement.

If there are local indexes defined on the table, this statement also splits the matching partition in each local index.

Because Oracle assigns system-generated names and default storage attributes to the new index partitions, you may wish to rename or alter these index partitions after splitting them.

If the partition you are splitting contains data, the ALTER TABLE SPLIT PARTITION statement marks the matching partitions (there are two) in each local index, as well as all global index partitions, as unusable.

You must rebuild these index partitions after issuing the ALTER TABLE SPLIT PARTITION statement.

Exchanging Table Partitions:

You can convert a partition into a non-partitioned table, and a table into a partition of a partitioned table by exchanging their data and index segments.

Exchanging table partitions is most useful when you have an application using non-partitioned tables which you want to convert to partitions of a partitioned table.

Converting a Partition View into a Partitioned Table:

This part describes how to convert a partition view into a partitioned table. The partition view is defined as follows:

CREATE VIEW students
SELECT * FROM students_jan95
UNION ALL
SELECT * FROM students_feb95
UNION ALL
...
SELECT * FROM students_dec95;

Initially, only the two most recent partitions, students_NOV95 and students_DEC95, will be migrated from the view to the table by creating the partition table.

Each partition gets a temporary segment of 2 blocks (as a placeholder).

CREATE TABLE accounts_new (...)
TABLESPACE ts_temp STORAGE (INITIAL 2)
PARTITION BY RANGE (opening_date)
(PARTITION jan95 VALUES LESS THAN ('950201'),
...
PARTITION dec95 VALUES LESS THAN ('960101'));

Use the EXCHANGE command to migrate the tables to the corresponding partitions.

ALTER TABLE students_new
EXCHANGE PARTITION nov95
WITH TABLE students_95
WITH VALIDATION;

ALTER TABLE students_new
EXCHANGE PARTITION dec95 WITH TABLE students_dec95
WITH VALIDATION;

So now the placeholder data segments associated with the NOV95 and DEC95 partitions have been exchanged with the data segments associated with the students_NOV95 and students_DEC95 tables.

Redefine the students view:

CREATE OR REPLACE VIEW accounts
SELECT * FROM students_jan95
UNION ALL
SELECT * FROM students_feb_95
UNION ALL
...
UNION ALL
SELECT * FROM students_new PARTITION (nov95)
UNION ALL
SELECT * FROM students_new PARTITION (dec95);

Drop the students_NOV95 and students_DEC95 tables, which own the placeholder segments that were originally attached to the NOV95 and DEC95 partitions.

After all the tables in the UNIONALL view are converted into partitions, drop the view and rename the partitioned table as the view.

DROP VIEW students;
RENAME students_new TO accounts;br>

Rebuilding Index Partitions:

Some operations, such as ALTER TABLE DROP PARTITION, mark all Oracle partitions of a global index unusable. You can rebuild global index partitions in two ways:

  1. Rebuild each partition by issuing the ALTER INDEX REBUILD PARTITION statement (you can run the rebuilds concurrently).
  2. Drop the index and re-create it (probably the easiest method).

Merging Oracle partitions:

Partition-level Export and Import provide a way to merge Oracle partitions in the same table, even though SQL does not explicitly support merging partitions.

A DBA can use partition-level Import to merge a table partition into the next highest partition on the same table. To merge partitions, do an export of the partition you would like to merge, delete the partition and do an import.

Return to Oracle DBA


Exclusive interviews with:
Steven Feuerstein, PLSQL expert
Donald Burleson, Top IT consultant


Free eBook

Subscribe to my newsletter and get my ebook on Entity Relationship Modeling Principles as a free gift:


What visitors say...

"I just stumbled accross your site looking for some normalization theory and I have to say it is fantastic.

I have been in the database field for 10+ years and I have never before come across such a useful site. Thank you for taking the time to put this site together."

Mike, USA

Read more Testimonials



Database Normalization eBook:


Database Normalization eBook




Copyright © www.databasedesign-resource.com / Alf A. Pedersen
All rights reserved.
All information contained on this website is for informational purposes only.
Disclaimer: www.databasedesign-resource.com does not warrant any company, product, service or any content contained herein.

Return to top

Copyright acknowledgement note:

The name Oracle is a trademark of Oracle Corporation.
The names MS Access/MS SQL Server are trademarks of Microsoft Corporation.
Any other names used on this website may be trademarks of their respective owners, which I fully respect.