Oracle Partitioned Tables
Oracle Partitioned Tables
Oracle Partitioned Tables
Maintenance of large tables and indexes can become very time and resource
consuming. At the same time, data access performance can reduce drastically for these
objects. Partitioning of tables and indexes can benefit the performance and
maintenance in several ways.
All the examples shown here use the users tablespace for all partitions. In a real
situation it is likely that these partitions would be assigned to different tablespaces to
reduce device contention.
Related articles.
Or specified individually.
Partitioning Indexes
There are two basic types of partitioned index.
Local - All index entries in a single partition will correspond to a single table
partition (equipartitioned). They are created with the LOCAL keyword and
support partition independance. Equipartioning allows oracle to be more efficient
whilst devising query plans.
Global - Index in a single partition may correspond to multiple table partitions.
They are created with the GLOBAL keyword and do not support partition
independance. Global indexes can only be range partitioned and may be
partitioned in such a fashion that they look equipartitioned, but Oracle will not
take advantage of this structure.
Prefixed - The partition key is the leftmost column(s) of the index. Probing this
type of index is less costly. If a query specifies the partition key in the where
clause partition pruning is possible, that is, not all partitions will be searched.
Non-Prefixed - Does not support partition pruning, but is effective in accessing
data that spans multiple partitions. Often used for indexing a column that is not
the tables partition key, when you would like the index to be partitioned on the
same key as the underlying table.
Oracle will generate the partition names and build the partitions in the default
tablespace using the default size unless told otherwise.
Note that the partition range values must be specified. The GLOBAL keyword means
that Oracle can not assume the partition key is the same as the underlying table.
Next we create a new partitioned table with a single partition to act as our destination
table.
Next we switch the original table segment with the partition segment.
We can now drop the original table and rename the partitioned table.
Finally we can split the partitioned table into multiple partitions as required and gather
new statistics.
2 rows selected.
----
Definitions
Decompose a table or index into smaller, more manageable pieces, called partitions. Each
Parti partition of a table or index must have the same logical attributes, such as column names,
tion datatypes, and constraints, but each partition can have separate physical attributes such as
pctfree, pctused, and tablespaces.
Parti
Each row in a partitioned table is unambiguously assigned to a single partition. The partition key is
tion
a set of from 1 to 16 columns that determines the partition for each row.
Key
Sub
Partitions created within partitions. They are just partitions themselves and there is nothing special
parti
about them.
tion
Co
mpo
site Composite partitioning is a combination of other partitioning methods. Oracle currently supports
Parti range-hash and range-list composite partitioning.
tioni
ng
Inter
val Interval partitioning is an extension to range partitioning in which, beyond a point in time, partitions
Parti are defined by an interval. Interval partitions are automatically created by the database when data
tioni is inserted into the partition.
ng
Expl KEY(I) IN subquery
ain
Plan KEY(SQ) Recursive subquery
PST
ART
/PS
TOP
Valu
es
By default, the following operations on partitions on a heap-organized table mark all global
Inva indexes as unusable:
lidati
ng ADD (HASH)
Inde COALESCE (HASH)
xes DROP
EXCHANGE
MERGE
MOVE
SPLIT
TRUNCATE
Glo
bal
A single index covering all partitions.
Inde
x
Enables partitioning of data that does not lend itself to range or list partitioning.
Has
h -- To view the numbers Oracle uses for hashing:
Parti
tioni SELECT program, sql_hash_value, prev_hash_value
ng
FROM gv$session;
List
Parti Explicitly controls how rows map to partitions. You do this by specifying a list of discrete values for
tioni the partitioning key in the description for each partition.
ng
Separate indexes for each partition. A local index can be unique. However, in order for a local
Loc
index to be unique, the partitioning key of the table must be part of the index's key columns.
al
Unique local indexes are useful for OLTP environments. You cannot explicitly add a partition to a
Inde
local index. Instead, new partitions are added to local indexes only when you add a partition to the
x
underlying table.
Ran
ge
Maps data to partitions based on ranges of partition key values that you establish for each
Parti
partition.
tioni
ng
Ref
eren
tial
Data is mapped to partitions based on values defined in a referential constraint (foreign key)
Parti
tioni
ng
Parti Oracle optimizes SQL statements to mark the partitions or subpartitions that need to be accessed
tioni and eliminates (prunes) unnecessary partitions or subpartitions from access. Partition pruning is
ng the skipping of unnecessary index and data partitions or subpartitions by a query.
Pru
ning
Demo Tablespaces
CREATE TABLESPACE part1
Cre DATAFILE 'c: emp\part01.dbf' SIZE 10M
ate
BLOCKSIZE 8192
dem
o EXTENT MANAGEMENT LOCAL UNIFORM SIZE 256K
tabl SEGMENT SPACE MANAGEMENT AUTO
esp ONLINE;
ace
s CREATE TABLESPACE part2
DATAFILE 'c: emp\part02.dbf' SIZE 10M
BLOCKSIZE 8192
EXTENT MANAGEMENT LOCAL UNIFORM SIZE 256K
SEGMENT SPACE MANAGEMENT AUTO
ONLINE;
Table Partitions
CREATE TABLE hash_part (
Has prof_history_id NUMBER(10),
h
person_id NUMBER(10) NOT NULL,
Parti
tion organization_id NUMBER(10) NOT NULL,
ed record_date DATE NOT NULL,
Tabl prof_hist_comments VARCHAR2(2000))
e PARTITION BY HASH (prof_history_id)
PARTITIONS 3
STORE IN (part1, part2, part3);
desc hash_part
desc user_tab_partitions
conn / as sysdba
x
CREATE TABLE students (
Ran student_id NUMBER(6),
ge
student_fn VARCHAR2(25),
Parti
tion student_ln VARCHAR2(25),
ed PRIMARY KEY (student_id))
Tabl PARTITION BY RANGE (student_ln) (
e- PARTITION student_ae VALUES LESS
By THAN ('F%') TABLESPACE part1,
Alph
a
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) TABLESPACEpart4);
desc ref_parent
desc vcol_part
desc user_tab_cols
COMMIT;
Composite Partitions
Co
CREATE TABLE composite_rng_hash (
mpo cust_id NUMBER(10),
site cust_name VARCHAR2(25),
Parti amount_sold NUMBER(10,2),
tion time_id DATE)
ed
PARTITION BY RANGE(time_id)
Tabl
e- SUBPARTITION BY HASH(cust_id)
By SUBPARTITION TEMPLATE(
Ran
SUBPARTITION sp1 TABLESPACE part1,
ge
And SUBPARTITION sp2 TABLESPACE part2,
Has SUBPARTITION sp3 TABLESPACE part3,
h SUBPARTITION sp4 TABLESPACE part4) (
PARTITION sales_pre05
VALUES LESS THAN (TO_DATE('01/01/2005','DD/MM/YYYY')),
PARTITION sales_2005
VALUES LESS THAN(TO_DATE('01/01/2006','DD/MM/YYYY')),
PARTITION sales_2006
VALUES LESS THAN(TO_DATE('01/01/2007','DD/MM/YYYY')),
PARTITION sales_2007
VALUES LESS THAN(TO_DATE('01/01/2008','DD/MM/YYYY')),
PARTITION sales_2008
VALUES LESS THAN(TO_DATE('01/01/2009','DD/MM/YYYY')),
PARTITION sales_future
VALUES LESS THAN(MAXVALUE));
desc user_tab_partitions
desc user_tab_subpartitions
desc user_subpartition_templates
conn sh/sh
conn uwclass/uwclass
exec dbms_stats.gather_table_stats(USER,
'COMPOSITE_RNG_HASH', 'SALES_PRE05');
exec dbms_stats.gather_table_stats(USER,
'COMPOSITE_RNG_HASH', 'SALES_2005');
exec dbms_stats.gather_table_stats(USER,
'COMPOSITE_RNG_HASH', 'SALES_2006');
exec dbms_stats.gather_table_stats(USER,
'COMPOSITE_RNG_HASH', 'SALES_2007');
exec dbms_stats.gather_table_stats(USER,
'COMPOSITE_RNG_HASH', 'SALES_2008');
exec dbms_stats.gather_table_stats(USER,
'COMPOSITE_RNG_HASH', 'SALES_FUTURE');
exec dbms_stats.gather_table_stats(USER,
'COMPOSITE_RNG_HASH', GRANULARITY=>'ALL');
desc composite_rng_list
desc composite_rng_rng
Compressed Partitions
Need syntax diagram
Parti
tion CREATE TABLE sales (
Lev saleskey NUMBER,
el quarter NUMBER,
Co product NUMBER,
mpr
salesperson NUMBER,
essi
on 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'));
COMMIT;
set timing on
ALTER TABLE range_part
EXCHANGE PARTITION pm WITH TABLE new_part;
set timing on
set timing on
set timing on
SELECT partition_name
FROM user_tab_partitions
WHERE table_name = 'RANGE_PART';
Drop Partition
ALTER TABLE DROP PARTITION <partition_name> [ UPDATE GLOBAL
INDEXES];
Dro
p
SELECT table_name, partition_name
Parti FROM user_tab_partitions;
tion
ALTER TABLE range_list DROP PARTITION s2k UPDATE GLOBAL
INDEXES;
Demos
conn scott/tiger
Parti
tion
-- Create a list partitioned table
Elim
inati CREATE TABLE partdemo (
on 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);
conn scott/tiger
conn / as sysdba
Undocumented Partitioning
Fou
nd SELECT DISTINCT TBL$OR$IDX$PART$NUM(BBUKDW.VISIT_TX, 0,
by CALENDAR_DT)
Jon FROM (
atha SELECT D.CALENDAR_DT CALENDAR_DT
n
Lewi FROM BBUKDW.JPL_DAY D
s WHERE D.FINANCIAL_WEEK_ID>=200218
and AND D.FINANCIAL_WEEK_ID <=200222)
note ORDER BY 1;
d he
re