WPD Partitioning in Oracle 11g
WPD Partitioning in Oracle 11g
Written by
Jacques Kilchoer, Software Engineer,
Thomas Szekely, Software Engineer,
Quest Software, Inc.
White Paper
© 2009 Quest Software, Inc.
ALL RIGHTS RESERVED.
WARRANTY
TRADEMARKS
All trademarks and registered trademarks used in this guide are property of their
respective owners.
World Headquarters
5 Polaris Way
Aliso Viejo, CA 92656
www.quest.com
e-mail: [email protected]
Please refer to our Web site (www.quest.com) for regional and international office
information.
Updated—May 2009
WPD-PartitioningOracle11-AG
CONTENTS
INTRODUCTION ..........................................................................................1
RECAP: ADVANTAGES OF PARTITIONING ............................................................... 1
REVIEW OF PARTITION TYPES (PRE-ORACLE 11) .......................................2
RANGE PARTITION ......................................................................................... 2
Example: Using Range Partitioning ............................................................ 2
Example: Archiving and Purging Unneeded Data .......................................... 3
HASH PARTITION .......................................................................................... 3
Example: Using Hash Partitioning .............................................................. 4
Example: Performing an Operation on a Subset of a Table ............................ 4
LIST PARTITION ............................................................................................ 4
Example: Using List Partitioning ................................................................ 5
COMPOSITE PARTITIONS .................................................................................. 5
GENERAL RESTRICTIONS ON PARTITIONING ........................................................... 6
FINDING PARTITIONED TABLES IN YOUR DATABASE .................................................. 6
NEW FEATURES OF PARTITIONING IN ORACLE 11 ......................................7
VIRTUAL COLUMNS ........................................................................................ 7
Example ................................................................................................ 7
Finding Tables with Virtual Columns in the Partitioning Key ........................... 8
Practical Issues for Virtual Columns ........................................................... 9
INTERVAL PARTITION ...................................................................................... 9
Example .............................................................................................. 10
Finding Interval-Partitioned Tables........................................................... 10
Changing a Range-partitioned Table to an Interval-partitioned Table,
or Vice-Versa........................................................................................ 10
Restrictions .......................................................................................... 11
REFERENCE PARTITION .................................................................................. 14
Example .............................................................................................. 14
Finding Reference-partitioned Tables........................................................ 16
Practical Issues for Reference-Partitioned Tables........................................ 17
Restrictions .......................................................................................... 17
SYSTEM PARTITION ...................................................................................... 19
Example .............................................................................................. 19
Finding System-partitioned Tables ........................................................... 20
Practical Issues for System-partitioned Tables ........................................... 20
Restrictions .......................................................................................... 20
ENHANCEMENTS TO COMPOSITE PARTITIONS ........................................................ 21
Pre-Oracle 11 Composite Partitions .......................................................... 21
New Types of Composite Partitions in Oracle 11......................................... 21
INDEX-ORGANIZED TABLE PARTITIONING ............................................................ 23
INDEX PARTITIONS ...................................................................................... 23
Local vs. Global Indexes......................................................................... 23
Allowed Types for Partitioned Indexes ...................................................... 24
PARTITION ADVISOR .................................................................................... 24
i
SUMMARY ................................................................................................. 25
REFERENCES ............................................................................................. 26
ABOUT THE AUTHORS ............................................................................... 27
ABOUT QUEST SOFTWARE, INC. ................................................................ 28
CONTACTING QUEST SOFTWARE ....................................................................... 28
CONTACTING QUEST SUPPORT ......................................................................... 28
ii
White Paper
INTRODUCTION
Partitioning was introduced in Oracle 8.0, and many improvements and
enhancements have been added in each subsequent version of Oracle. Partitioning
allows you to physically break up the segments that make up a table, index, or large
object, while keeping them logically in the same Oracle object. Partitioning is
available only in Oracle Enterprise Edition, and usually requires a separate license.
Partitioning increases the complexity of managing the segments that make up your
tables and indexes, but offers several significant advantages, which are listed below.
1
Partitioning in Oracle 11
• Range partition
• Hash partition
• List partition
• Composite partition
Range Partition
A range partition was the first type of partitioning introduced, and is available in
Oracle 8.0 and later.
Each partition in a range-partitioned table contains rows with partition key values
less than the specified limit for that partition. Each partition must have an upper
limit value (specify MAXVALUE as the upper limit to indicate that all values are
acceptable for that column in that partition).
Oracle will not allow inserting a row that exceeds the range limit of the last partition.
If a partition has MAXVALUE as the upper limit for a partitioning key column (or any
of the preceding columns in the partitioning key) then Oracle will not reject any
values for that column for that partition.
You cannot add partitions to a table once you have a range partition with a
MAXVALUE upper limit for the leading column in the partitioning key. Instead, you
would need to split one or more of the existing partitions.
Range partitions are useful for archiving historical data using drop partitions.
-- move the data to the archive table, replacing the partition in the
-- production table with an empty partition
alter table orders exchange partition orders_2006 with table orders_2006 ;
Hash Partition
Hash partitions were introduced in Oracle 8.1. In a hash-partitioned table, rows are
allocated evenly to equal-sized hash partitions using a hashing algorithm. You will
not know in which partition any particular row will reside.
A hash-partitioned table would be used to minimize the size of your segments and
increase manageability of maintenance operations. It can also be used to spread
DML activity across several segments using Oracle’s PARALLEL operations.
3
Partitioning in Oracle 11
To move partition 1:
To move partition 2:
List Partition
A list partition is available in Oracle 9.0 and later. This method allows partitioning
by a discrete list of values present in the partitioning key column. A list-partitioned
table can have only one column in the partitioning key.
4
White Paper
The partition with the DEFAULT value will include all the values not specified in any
other partition. If a partition includes the DEFAULT value, then you cannot add
more partitions to the table; you will need to split an existing partition. There can
be only one partition with the DEFAULT value, and you cannot specify any other
values for that partition.
The string making up the list of all values for a single partition is limited in size to
4K, and the total size of all values of the partitioning key (for all partitions) is
limited to 64K–1.
A list partition is useful when the partitioning key column that you choose to
physically separate your data contains a small number of discrete values.
Composite Partitions
5
Partitioning in Oracle 11
For tables with partitioning key columns, the limit is 16 columns in the partitioning
key (with exceptions as listed for individual partition types).
The columns in the partitioning key can be of any built-in datatype except ROWID,
LONG, LOB, or XMLType. TIMESTAMP WITH TIME ZONE columns cannot be used in
a partitioning key, but TIMESTAMP or TIMESTAMP WITH LOCAL TIME ZONE columns
can be used in the partitioning key.
6
White Paper
Virtual Columns
Example
Suppose in your phonebook table you want a virtual column that will store the
username in uppercase. Use the following code:
7
Partitioning in Oracle 11
You can use that virtual column in the partitioning key of your table, as follows:
select
a.owner, a.table_name, a.partitioning_type,
b.column_name, c.virtual_column, c.data_default
from dba_part_tables a, dba_part_key_columns b, dba_tab_cols c
where
a.owner = b.owner
and a.table_name = b.name
and b.object_type = 'TABLE'
and a.owner = c.owner
and a.table_name = c.table_name
and b.column_name = c.column_name
and exists (select null from dba_tab_cols d
where a.owner = d.owner
and a.table_name = d.table_name
and d.hidden_column = 'NO' and d.virtual_column = 'YES')
order by a.owner, a.table_name, b.column_position ;
8
White Paper
As with all new features, it is important to test virtual columns carefully before
implementing them in production. For example, in our version of Oracle 11, we
found that we were able to create a check constraint on a virtual column with no
error, but then subsequent inserts into the table caused an OR-00600, indicating an
Oracle bug.
Restrictions
Interval Partition
Interval partitioning does not share this drawback. You can create a range-
partitioned table with an added parameter (the INTERVAL parameter), which will
specify to Oracle the upper limit it should generate when it automatically creates a
new partition if you insert a row with a partitioning key value greater than the
upper limit of the last partition in the table.
9
Partitioning in Oracle 11
Example
Suppose you want to create a table of customers partitioned by customer signup
date, with Oracle automatically creating a partition every 18 months. Use the
following code:
Note that the ranges specified for existing partitions at table creation time do not
have to be separated by the INTERVAL value. The INTERVAL value will apply when
inserting a row with a value that exceeds the highest partition key value in the table.
Restrictions
You cannot use interval partitioning on an index-organized table.
There can be only one column in the partitioning key. The column must be of a
NUMBER or DATE datatype.
When you create the table, you must specify at least one range partition. The high
value for the last range partition is called the transition point; if you insert a value
past the transition point, Oracle will automatically create a new partition.
If you insert a row that requires the creation of a new partition, the new partition
will be created regardless of whether the transaction is committed.
Oracle will insert “sparse” partitions. Recall the example CUSTOMERS table above.
If you add a row with signup_date 31 January 3001, Oracle will not add all the
partitions in between; it will add just one new partition. The upper limit for the new
partition will be the upper limit that would be determined from by adding N *
INTERVAL to the upper limit for the highest existing partition, with a value N that
will yield a upper limit higher than the value in the inserted row.
X XP 1 3000
X XP 1 3000
X SYS_Pnnnnnn 10500
11
Partitioning in Oracle 11
Explanation:
If we had inserted 10000 rows in table X with values for Y from 1 to 10000
If we create the empty table and only add one row with value 10000, Oracle will
create only one new partition with upper limit 10500.
At table creation time, partitions with no specified tablespace will be stored in the
default tablespace for the table, which is either specified on the table create
statement or taken from the user’s default. When new partitions are added after
table creation, they will be placed either in the default tablespace for the table or in
one of the tablespaces specified in the STORE IN clause (which are used in a round-
robin fashion).
The storage parameters will be taken from either the tablespace default storage
parameters or the default storage parameters specified on the create table statement.
12
White Paper
For example:
In this example, the two partitions created at table creation time will go into the
default tablespace for the table, which is DATA (since no tablespace was specified
for the individual partitions in the CREATE TABLE statement). Any subsequent
partitions added will be created in tablespaces DATA1, DATA2, DATA3, DATA4, used
in a round-robin fashion. Of course you can manually add a partition and explicitly
specify a tablespace in the ADD PARTITION statement.
• MOVE PARTITION
• EXCHANGE PARTITION
• TRUNCATE PARTITION
• ADD PARTITION
• DROP PARTITION
• RENAME PARTITION
• SPLIT PARTITION
• MERGE PARTITION.
Splitting and merging partitions may modify the upper limit generated for new
partitions that will be added, if one of the split or merged partitions is the last
partition in the table. The upper limit that Oracle will generate for any new partition
depends on the transition point (the upper limit for the last partition in the table.)
13
Partitioning in Oracle 11
Reference Partition
Reference partitioning enables you to partition a child table using the same
partitioning as its parent table. The parent table is the table referenced by a foreign
key constraint. The child table will have exactly the same number of partitions as
the parent table, and partition operations made on the parent table will propagate
to the child table.
Any changes in the number of partitions to the parent table are propagated to the
child table.
Example
Suppose you want to create a table of orders partitioned by order date, with a child
table named order_detail having the same partitioning scheme. You can create
order_detail as a reference-partitioned table, and the rows in order_detail will be
partitioned by order_date even though the order_detail table does not contain the
order_date column.
TABLE ORDERS
TABLE ORDER_DETAIL
PARTITION P1 PARTITION P2
14
White Paper
For order_detail, you could have specified a list of partitions and tablespaces in the
create table clause, but it is not necessary.
15
Partitioning in Oracle 11
To see the list of columns in the referencing constraint on the child table, the
corresponding list of columns for the primary key constraint in the parent table, and
the partitioning type for the parent table, run this query:
select
a.owner, a.table_name, a.ref_ptn_constraint_name, c.column_name,
e.owner, e.table_name, d.partitioning_type, e.constraint_name, f.column_name
from dba_part_tables a, dba_constraints b, dba_cons_columns c,
dba_part_tables d, dba_constraints e, dba_cons_columns f
where
a.partitioning_type = 'REFERENCE'
and a.owner = b.owner
and a.ref_ptn_constraint_name = b.constraint_name
and b.constraint_type = 'R'
and b.owner = c.owner
and b.constraint_name = c.constraint_name
and e.constraint_type in ('P', 'U')
and e.owner = b.r_owner
and e.constraint_name = b.r_constraint_name
and d.owner = e.owner
and d.table_name = e.table_name
and e.owner = f.owner
and e.constraint_name = f.constraint_name
and c.position = f.position
order by
a.owner, a.table_name, d.owner, d.table_name, c.position ;
16
White Paper
In our version of Oracle 11, we were able to drop the primary key constraint on the
parent table using DROP PRIMARY KEY CASCADE (something that Oracle surely
intended to prevent.) But once the primary key constraint was dropped, any DML
on the child table returned an ORA-00600 error indicating an Oracle bug. Be
careful not to drop the primary key constraint on the parent table!
If you attempt to insert a row into the child table where the foreign key column
values are not found in the parent table, you will receive the following error:
Restrictions
The following restrictions apply to reference-partitioned tables:
• The parent table must be partitioned (as might seem obvious). It can have
any partitioning type except interval partitioning.
• The foreign key constraint on the child table must be ENABLE VALIDATE
NOT DEFERRABLE and cannot have the ON DELETE SET NULL clause.
• The columns in the foreign key constraint used for partitioning must be
NOT NULL.
• The foreign key and the primary key constraint it references cannot contain
virtual columns.
• The foreign key constraint used for partitioning may not be self-referencing
(i.e. point to a primary key constraint on the same table). (This also seems
obvious.)
• The ROW MOVEMENT clause must be the same on both tables (either
ENABLE or DISABLE).
• You cannot create a reference-partitioned table using a CREATE TABLE …
AS SELECT … statement. You should use CREATE TABLE … followed by
INSERT … SELECT …
• The child table cannot be an index-organized table.
• You will not be able to perform an EXCHANGE PARTITION on the parent
table (see partition operations below for an explanation.)
17
Partitioning in Oracle 11
When the child table is created, it will be created with the same number of
partitions as the parent table. The table partitions in the child table will have the
same names as the table partitions in the parent table. If a partition is added to the
parent table (via ADD PARTITION or SPLIT PARTITION), then a new partition will be
added to the child table, and it will have the same name as the partition in the
parent table.
The tablespace and storage parameters for the new partition in the child table are
taken from the table defaults. The default tablespace for the table will be either the
tablespace specified at table creation time, or, if no default tablespace is specified,
the user’s default tablespace. The storage parameters will be taken from the
tablespace default storage parameters.
• MOVE PARTITION
• TRUNCATE PARTITION
• RENAME PARTITION
• EXCHANGE PARTITION, with the following caveat:
Oracle does not allow you to perform an EXCHANGE PARTITION on a table
that has a primary key or unique constraint referenced by an enabled
foreign key constraint. The usual method is to disable the foreign key
constraints referencing the parent table, exchange the partition in the
parent table, and re-enable the foreign key constraints referencing the
parent table. Since you cannot disable or drop the foreign key constraint
used for partitioning, you will not be able to perform the EXCHANGE
PARTITION on the parent table; the parent table will always have an
enabled foreign key constraint referencing it unless you drop the reference-
partitioned child table.
The following operations are allowed only on the parent table, and are disallowed
on the reference-partitioned table (the child table). When one of these operations is
performed on the parent table, it will be replicated on the child table.
• ADD PARTITION
• DROP PARTITION
• SPLIT PARTITION
• MERGE PARTITION
18
White Paper
System Partition
The documentation suggests that this method of partitioning can be used to equi-
partition dependent tables, such as nested table or domain index storage tables
with partitioned base tables. These cases are less well known or less common, so in
the interest of simplicity we will present a simple system-partitioned table.
Example
Suppose you want to create a table of orders partitioned by order date, with a child
table named order_detail having the same partitionining scheme.
The following statement will insert rows into the table. Notice that every insert
portion of the insert statement must specify a partition name in addition to the
table name.
insert first
when object_type = 'TABLE'
then
into all_obj partition (my_tables)
(owner, object_name, object_type, created, last_ddl_time)
values (owner, object_name, object_type, created, last_ddl_time)
when object_type = 'INDEX'
19
Partitioning in Oracle 11
then
into all_obj partition (my_indexes)
(owner, object_name, object_type, created, last_ddl_time)
values (owner, object_name, object_type, created, last_ddl_time)
select owner, object_name, object_type, created, last_ddl_time
from all_objects ;
Restrictions
The following restrictions apply to system partitioning:
Partitions will be added only when the user issues a SQL statement including a
partition operation that will add a partition.
20
White Paper
The tablespace and storage parameters for any new partition, if not specified on the
SQL statement creating the partition, are taken from the table defaults. The default
tablespace for the table will be either the tablespace specified at table creation
time, or, if no default tablespace is specified, the user’s default tablespace. The
storage parameters will be taken from the tablespace default storage parameters.
• MOVE PARTITION
• EXCHANGE PARTITION
• TRUNCATE PARTITION
• ADD PARTITION
• DROP PARTITION
• RENAME PARTITION
• MERGE PARTITION
21
Partitioning in Oracle 11
Practical Considerations
For the primary practical considerations for each partition type, refer to the detailed
discussions above.
For range, list and hash subpartitions, you can create a subpartition template to
establish default subpartition characteristics. The subpartition template will be used
when a new partition is added via ADD PARTITION or SPLIT PARTITION, or when
the table is created without explicitly specifying the subpartitions for an individual
partition.
Example
Subpartition templates are not new to Oracle 11, so we will simply provide an
example of a subpartition template:
22
White Paper
Restrictions
The only physical attributes that can be specified for a subpartition are TABLESPACE
and COMPRESSION.
The new partitioning types (interval, reference, system) cannot be used to partition
an index-organized table.
An index-organized table cannot contain a virtual column; therefore you will not
be able to partition an index-organized table with a virtual column in the
partitioning key.
Index Partitions
There are no new features for index partitioning in Oracle 11, except that all new
partition types and composite partition types can have locally partitioned indexes.
However, we will include a brief recap of index partitioning considerations.
Local partitioned indexes are vastly easier to maintain than global indexes, because
they are automatically maintained during partition maintenance operations such as
EXCHANGE PARTITION (if you include indexes), TRUNCATE PARTITION, and DROP
PARTITION. There are exceptions:
23
Partitioning in Oracle 11
For any of these operations, Oracle has an UPDATE GLOBAL INDEXES clause to
rebuild global indexes as part of the operation, and an UPDATE ALL INDEXES clause
to update global and local indexes as part of the operation. Note that rebuilding
indexes will mean that the operation is no longer a simple data dictionary update
and the ALTER TABLE statement will not be instantaneous.
There are situations where a global index will be preferable to a local partitioned
index, depending on how the data is accessed. For example, if you query with a
column in the where clause that is in a local partitioned index but not in the
partitioning key, Oracle will need to read each index partition to find the row.
(Oracle will not always perform index partition elimination when the query is a local
nonprefixed index.) However, if a global partitioned index includes that column in
the partitioning key, Oracle will need to read only one index partition. Therefore, for
performance reasons you may want to create a global index or a global partitioned
index. You should, however, be aware of the potential of increased maintenance
tasks when using global indexes or global partitioned indexes on a partitioned table.
In some cases, you cannot use a local partitioned index on a partitioned table. For
example, for a unique locally partitioned index, or a local index used to enforce a
primary key or unique constraint, the index columns must be a superset of the
partitioning key. If your index columns are not a superset of the partitioning key,
then you will be unable to create a unique locally partitioned index on your
partitioned table, and a non-unique locally partitioned index cannot be used by
Oracle to enforce a primary key or unique constraint.
Partition Advisor
24
White Paper
SUMMARY
Oracle 11 introduced the following enhancements to partitioning:
25
Partitioning in Oracle 11
REFERENCES
Oracle 11 documentation
26
White Paper
Both authors work on Space Manager with LiveReorg, a tool to allow you to manage
partitioned tables and reorganize segments in the database.
27
Partitioning in Oracle 11
Please refer to our Web site for regional and international office information.
View the Global Support Guide for a detailed explanation of support programs,
online services, contact information, and policy and procedures. The guide is
available at: http://support.quest.com/pdfs/Global Support Guide.pdf
28