Oracle Partitioning Introduction
1 1 MyOnlineITCourses.com
dbVidya.com
Partitioning
Partitioning is the ability of the database to take very
large tables or indexes and
physically break them into
smaller
manageable pieces.
2 2 MyOnlineITCourses.com
dbVidya.com
Partitioned VS Non Partitioned Table
3 3 MyOnlineITCourses.com
dbVidya.com
Partitions - Benefits
ORDERS ORDERS ORDERS
USA
EUROPE
JAN FEB JAN FEB
Large Table Partition Composite Partition
Difficult to Manage Divide and Conquer Better Performance
Easier to Manage More flexibility to match
business needs
Improve Performance
Transparent to applications
4 4 MyOnlineITCourses.com
dbVidya.com
Partitioning - Benefits
• Queries will access only those relevant
partitions
Faster
• An entire partition can be exported
• Exported partition can be deleted from
Cheaper database
• Partition Maintenance is simpler when
compared to a large un-partitioned table
Flexible • Partition backup/restoration is easier
5 5 MyOnlineITCourses.com
dbVidya.com
When To Partition - Table
Here are some suggestions for when to partition a table:
Tables greater than 2 GB should always be considered as
candidates for partitioning.
Tables containing historical data, in which new data is
added into the newest partition.
A typical example is a historical table where only the
current month's data is updatable and the other 11
months are read only.
When the contents of a table need to be distributed across
different types of storage devices.
6 6 MyOnlineITCourses.com
dbVidya.com
Partition Strategies
7 7 MyOnlineITCourses.com
dbVidya.com
Partition Type .. (Examples)
8 8 MyOnlineITCourses.com
dbVidya.com
Range Partition
9 9 MyOnlineITCourses.com
dbVidya.com
Range Partitioned Tables
• Data distribution is based on range of values
• Data distribution is continuous
Definition • Best performance when data distribution is even on
the partition key.
• Knowledge of data is critical before choosing this
• Date as partitioning key
Jan 2011, Feb 2011, Mar 2011 so on..
Example
• Department code as partitioning key
Values <= 30, Values > 30
10 10MyOnlineITCourses.com
dbVidya.com
Range Partition
CREATE TABLE DEPT (
DEPTNO NUMBER (2),
DEPT_NAME VARCHAR2 (30))
PARTITION BY RANGE (DEPTNO)
(
PARTITION D1 VALUES LESS THAN (10) TABLESPACE DEPT1,
PARTITION D2 VALUES LESS THAN (20) TABLESPACE DEPT2,
PARTITION D3 VALUES LESS THAN (MAXVALUE) TABLESPACE DEPT3
)
A MAXVALUE literal can be defined for the highest partition.
MAXVALUE represents a virtual infinite value that sorts
higher than any other possible value for the partitioning key,
including the NULL value
11 11MyOnlineITCourses.com
dbVidya.com
Accessing Partition Data
Insert some rows
insert into dept_part_range values(10,'HR');
insert into dept_part_range values(25,'SALES');
insert into dept_part_range values(5, 'PAYROLL');
select * from dept_part_range partition (D1)
Deptno Dname
----------------
5 PAYROLL
select * from dept_part_range partition (D3)
Deptno Dname
----------------
25 SALES
12 12MyOnlineITCourses.com
dbVidya.com
Multi Column Range
Partition
13 13 MyOnlineITCourses.com
dbVidya.com
Range Partition (Multi column)
Multi Column Range • Defined on two or more
Partition columns of the table
• Enhances Query performance
if searched on partition key
Advantages
• Improves the Manageability of
partitions
14 14MyOnlineITCourses.com
dbVidya.com
Creating a Multi Column
range-partitioned table
CREATE TABLE sales_demo (
year NUMBER,
month NUMBER,
day NUMBER,
amount_sold NUMBER)
PARTITION BY RANGE (year,month)
(PARTITION before2001 VALUES LESS THAN (2001,1),
PARTITION q1_2001 VALUES LESS THAN (2001,4),
PARTITION q2_2001 VALUES LESS THAN (2001,7),
PARTITION q3_2001 VALUES LESS THAN (2001,10),
PARTITION q4_2001 VALUES LESS THAN (2002,1),
PARTITION future VALUES LESS THAN (MAXVALUE,0));
15 15MyOnlineITCourses.com
dbVidya.com
Inserting into Multi column
Partition
REM 12-DEC-2000
INSERT INTO sales_demo VALUES(2000,12,12, 1000);
REM 17-MAR-2001
INSERT INTO sales_demo VALUES(2001,3,17, 2000);
REM 1-NOV-2001
INSERT INTO sales_demo VALUES(2001,11,1, 5000);
REM 1-JAN-2002
INSERT INTO sales_demo VALUES(2002,1,1, 4000);
16 16MyOnlineITCourses.com
dbVidya.com
When to use Range Partition
Very less time
Very large for
tables being administrative
scanned on a operations like
range predicate backup on large
like Order-Date tables
Need to maintain
rolling window of
data
17 17MyOnlineITCourses.com
dbVidya.com
Hash
Partitioning
18 18 MyOnlineITCourses.com
dbVidya.com
Hash Partitioning
Partition • Specify the number of
Key partitions
Always • Oracle database inserts
Define 2n rows based on hash value
Partitions of partition key
• Specify storage for the
Storage entire table and the
tablespace for partitions
19 19MyOnlineITCourses.com
dbVidya.com
Hash Partition
Used to spread data evenly over partitions.
Gives a highly tunable method of data placement,
Influences availability and performance by spreading
these evenly sized partitions across I/O devices
(striping).
Hash partitioning is used when the data to be
partitioned is not historical or has no obvious
partitioning key or data has no logical groupings.
Oracle Database uses a linear hashing algorithm and to
prevent data from clustering within specific partitions.
Define the number of partitions by a power of two
(for example, 2, 4, 8).
20 20MyOnlineITCourses.com
dbVidya.com
Hash Partition – Example
Hash-partitioned table that splits the table into four parts based on
the hash of the partition key,-> acct_no.
CREATE TABLE CUST_SALES_HASH (
ACCT_NO NUMBER (5),
CUST_NAME VARCHAR2 (30))
PARTITION BY HASH (ACCT_NO) PARTITIONS 4
STORE IN (USERS1, USERS2, USERS3, USERS4);
Note: User has no control of the row to partition mapping
21 21MyOnlineITCourses.com
dbVidya.com
Advantage Hash Partition
Maintainability
Instead of having a 100 gig tablespace to backup,
you have 100, 1 gig tablespaces.
Each tablespace spends less time in backup
mode, reduces the amount of potential
Extra redo
Reduces the amount of manual recovery
you need to do if the instance failes during
backup).
Same with restores.
22 22MyOnlineITCourses.com
dbVidya.com
Advantage Hash Partition
You can analyze each partition quickly, instead of
running an analyze on a 100 gig table.
You can reorg each partition independent of
any other partition
you can easily redistribute the load over many
disks, you now have evenly distributed the data
into 100 1 gig partitions, move them at will.
If you did this manually you would have a
heck of a time moving the stuff around.
23 23MyOnlineITCourses.com
dbVidya.com
Advantage Hash Partition
Partition elimination.
In OLTP system. Pick a PARTITION key that is used
in most all queries
(say a CUSTOMER_ID, an ORDER_ID, something).
It is very hard to range partition on that (almost
impossible on something like an ORDER_ID) but very
trivial to hash partition.
Now, all queries of the form:
select * from t where customer_id = :x;
Oracle will use partition elimitation in the above query
24 24MyOnlineITCourses.com
dbVidya.com
Advantage Hash Partition
In data warehouse if there is a requirement to
do a bulk update/delete.
This activity can be done in parallel now
since the data is partitioned, if it weren't,
you could not do PDML.
When a range partitioning is used but the
underlying partitions are still too large --
enter hash partitioning to re-introduce the
divide and conquer features of partitions.
25 25MyOnlineITCourses.com
dbVidya.com
Advantage Hash Partition
Use it on data you might have used in a hash
cluster.
A census table with data full of data keyed
by SSN.
We either FULL SCAN it or go in by SSN.
Perfect for a hash partitioned data.
26 26MyOnlineITCourses.com
dbVidya.com
When to use Hash Partition
Improve
Manageability/availability of
Large tables
Avoid data skew in
partitions
Maximize I/0 throughput
27 27MyOnlineITCourses.com
dbVidya.com
List
Partition
28 28 MyOnlineITCourses.com
dbVidya.com
List Partitioning
•Segmenting data with a list of values
•Flexible means of partitioning where data is better
Definition
understood
• Similar to Range Partitioning, but without any max value
• CREATE TABLE DEPT_PART (DEPTNO NUMBER (2),DNAME VARCHAR2 (14),LOC
VARCHAR2 (13))
PARTITION BY LIST (DNAME)
(PARTITION D1_EAST VALUES (‘NEW YORK’),
Example
PARTITION D2_WEST VALUES (‘SAN FRANCISCO’, ‘LOS ANGELES’),
PARTITION D3_SOUTH VALUES (‘ATLANTA’,’DALLAS’,’HOUSTON’),
PARTITION D4_NORTH VALUES (‘CHICAGO’,’DETROIT’));
29 29MyOnlineITCourses.com
dbVidya.com
List Partition- Data
30 30MyOnlineITCourses.com
dbVidya.com
Range ,List , Hash Partitions
31 31MyOnlineITCourses.com
dbVidya.com
Composite Partitioning
A distinct value
Composite
pair for the two
Data is Partitioning is
dimensions
Partitioned complementary
uniquely
along two to multi
determines the
dimensions column range
target partition
partition
32 32MyOnlineITCourses.com
dbVidya.com
Composite
Partitioning
33 33 MyOnlineITCourses.com
dbVidya.com
Composite Partitioning Strategies
New 11g Strategy Use Case
List – Range Geography –Time
Range - Range Ship Date – Order Date
List - Hash Geography – OrderID
List - List Geography – Product
34 34MyOnlineITCourses.com
dbVidya.com
Range – List Partitioning
35 35MyOnlineITCourses.com
dbVidya.com
Composite Partitioning
Table SALES Range - Range
RANGE(order_date)-RANGE(ship_date)
ship_date
• All records with
Jan order_date in
2006 ... ... March 2006
AND
Feb
ship_date in May
2006 ... ... 2006
... ...
May
May
2006 ... ...
... ...
Jan 2006 Feb 2006 Mar2006
Mar 2006 Jan 2007
order_date
36 36MyOnlineITCourses.com
dbVidya.com
Range-Hash Partitioned
CREATE TABLE credential_evaluations
( eval_id VARCHAR2(16) primary key
, grad_id VARCHAR2(12)
, grad_date DATE
, degree_granted VARCHAR2(12)
, degree_major VARCHAR2(64)
, school_id VARCHAR2(32)
, final_gpa NUMBER(4,2))
PARTITION BY RANGE (grad_date)
SUBPARTITION BY HASH (grad_id) SUBPARTITIONS 8 STORE IN (T1,T2,T3,T4)
( PARTITION grad_date_70s VALUES LESS THAN (TO_DATE('01-JAN-1980','DD-
MON-YYYY'))
, PARTITION grad_date_80s
VALUES LESS THAN (TO_DATE('01-JAN-1990','DD-MON-YYYY'))
, PARTITION grad_date_90s
VALUES LESS THAN (TO_DATE('01-JAN-2000','DD-MON-YYYY')),
PARTITION grad_date_00s
VALUES LESS THAN (TO_DATE('01-JAN-2010','DD-MON-YYYY'))
);
37 37MyOnlineITCourses.com
dbVidya.com
Range List
C REATE TABLE q_territory_sales
( divno VARC HAR2(12), depno NUMBER,
itemno VARC HAR2(16), accrual_date DATE,
sales_amount NUMBER, state VARC HAR2(2),
constraint pk_q_dvdno primary key(divno,depno)
) TABLESPAC E t8
PARTITION BY RANGE (accrual_date) SUBPARTITION BY LIST (state)
(PARTITION q1_2000 VALUES LESS THAN (TO_DATE('1-APR-2000','DD-MON-YYYY'))
( SUBPARTITION q1_2000_nw VALUES ('OR', 'WY'),
SUBPARTITION q1_2000_sw VALUES ('C A', 'NM'),
SUBPARTITION q1_2000_ne VALUES ('NY', 'C T'),
SUBPARTITION q1_2000_se VALUES ('FL', 'GA'),
SUBPARTITION q1_2000_nc VALUES ('SD', 'WI'),
SUBPARTITION q1_2000_sc VALUES ('TX', 'LA‘) ),
PARTITION q2_2000 VALUES LESS THAN (TO_DATE('1-JUL-2000','DD-MON-YYYY'))
( SUBPARTITION q2_2000_nw VALUES ('OR', 'WY'),
SUBPARTITION q2_2000_sw VALUES ('C A', 'NM'),
SUBPARTITION q2_2000_ne VALUES ('NY', 'C T'),
SUBPARTITION q2_2000_se VALUES ('FL', 'GA'),
SUBPARTITION q2_2000_nc VALUES ('SD', 'WI'),
SUBPARTITION q2_2000_sc VALUES ('TX', 'LA‘)
), PARTITION q3_2000 VALUES LESS THAN (TO_DATE('1-OC T-2000','DD-MON-YYYY'))
( SUBPARTITION q3_2000_nw VALUES ('OR', 'WY'),
SUBPARTITION q3_2000_sw VALUES ('C A', 'NM'),
SUBPARTITION q3_2000_ne VALUES ('NY', 'C T'),
SUBPARTITION q3_2000_se VALUES ('FL', 'GA'),
SUBPARTITION q3_2000_nc VALUES ('SD', 'WI'),
SUBPARTITION q3_2000_sc VALUES ('TX', 'LA')
), PARTITION q4_2000 VALUES LESS THAN ( TO_DATE('1-JAN-2001','DD-MON-YYYY'))
( SUBPARTITION q4_2000_nw VALUES ('OR', 'WY'),
SUBPARTITION q4_2000_sw VALUES ('C A', 'NM'),
SUBPARTITION q4_2000_ne VALUES ('NY', 'C T'),
SUBPARTITION q4_2000_se VALUES ('FL', 'GA'),
SUBPARTITION q4_2000_nc VALUES ('SD', 'WI'),
SUBPARTITION q4_2000_sc VALUES ('TX', 'LA')) );
38 38MyOnlineITCourses.com
dbVidya.com
Interval
Partitioning
39 39 MyOnlineITCourses.com
dbVidya.com
Interval Partitioning
Partitions are created automatically as data
arrives
40 40MyOnlineITCourses.com
dbVidya.com
Interval Partitioning
Interval Partitioning
Extension to Range Partitioning
Full automation for equi-sized range partitions
Partitionsare created as metadata information only
Start Partition is made persistent
Segments are allocated as soon as new data arrives
No need to create new partitions
Local indexes are created and maintained as well
41 41MyOnlineITCourses.com
dbVidya.com
Interval Partitioning
As easy as One, Two, Three ..
CREATE TABLE sales (order_date DATE, ...)
PARTITON BY RANGE (order_date)
INTERVAL(NUMTOYMINTERVAL(1,'month')
(PARTITION p_first VALUES LESS THAN ('01-JAN-2006');
Table SALES
... ... ...
Jan 2006 Feb 2006 Mar 2006 Jan 2007 Oct 2009 Nov 2009
• First segment is created
– Mandatory to have a defined lower bound
• Other segments only meta-data
– Will be allocated when data is inserted
42 42MyOnlineITCourses.com
dbVidya.com
Interval Partition
create table
pos_data_interval (
start_date DATE,
store_id NUMBER,
inventory_id NUMBER(6),
qty_sold NUMBER(3)
)
PARTITION BY RANGE (start_date)
INTERVAL(NUMTOYMINTERVAL(1, 'MONTH'))
(
PARTITION pos_data_p2 VALUES LESS THAN (TO_DATE('1-7-2007',
'DD-MM-YYYY')),
PARTITION pos_data_p3 VALUES LESS THAN (TO_DATE('1-8-2007',
'DD-MM-YYYY'))
);
43 43MyOnlineITCourses.com
dbVidya.com
Virtual Column
based
Partitioning
44 44 MyOnlineITCourses.com
dbVidya.com
Virtual Column-Based
Partitioning
ORDERS
ORDER_ID ORDER_DATE CUSTOMER_ID... REGION AS (SUBSTR(ORDER_ID,6,2))
------
---------- ----------- ----------- --
US
9834-US-14 12-JAN-2007 65920
EU
8300-EU-97 14-FEB-2007 39654
EU
3886-EU-02 16-JAN-2007 4529
US
2566-US-94 19-JAN-2007 15327
US
ORDERS
3699-US-63 02-FEB-2007 18733
USA
REGION requires no storage
Partition by ORDER_DATE, REGION EMEA
JAN FEB
45 45MyOnlineITCourses.com
dbVidya.com
Virtual Column-Based
Partitioning
ACCOUNT_NUMBER ACCOUNT_NAME CONTACT_PERSON
--------------------- -------------------- -----------------
3983-9339-1232-1292 N-JOHNS-INDUSTRIALS JOHN
8778-5435-5345-5223 E-MATTEL-AUTOMOTIVE MIKE
2432-6543-2244-0877 S-SOUTHERN-TRANSPORTS DOUG
4333-3424-6564-1322 W-GLOBAL-DISTRIBUTION GERRY
a virtual column can be used to create an expression
that represents
the region such as “substr(account_name,1,1)”.
46 46MyOnlineITCourses.com
dbVidya.com
Virtual Columns based
Partition - Example
create table
accounts_v (
account_number varchar2(30),
account_name varchar2(30),
contact_person varchar2(30),
region AS (case
when substr(account_name,1,1) = 'N' then 'NORTH'
when substr(account_name,1,1) = 'E' then 'EAST'
when substr(account_name,1,1) = 'S' then 'SOUTH'
when substr(account_name,1,1) = 'W' then 'WEST'
end)
)
partition by
list (region)
(
partition pN values ('NORTH'),
partition pE values ('EAST'),
partition pS values ('SOUTH'),
partition pW values ('WEST')
);
47 47MyOnlineITCourses.com
dbVidya.com
Verification
SELECT
TABLE_NAME, PARTITION_NAME, PARTITION_POSITION, HIGH_VALUE
FROM
DBA_TAB_PARTITIONS
WHERE
TABLE_NAME='ACCOUNTS_V'
ORDER BY
PARTITION_NAME;
TABLE_NAME PARTITION_ PARTITION_POSITION HIGH_VALUE
--------------- ---------- ------------------ ----------
ACCOUNTS_V PE 2 ‘EAST'
ACCOUNTS_V PN 1 'NORTH'
ACCOUNTS_V PS 3 ‘SOUTH'
ACCOUNTS_V PW 4 'WEST'
48 48MyOnlineITCourses.com
dbVidya.com
Verification
select * from accounts_v partition (PE);
ACCOUNT_NUMBER ACCOUNT_NAME CONTACT_PERSON REGION
--------------------- ------------------------- ---------------- -------
8778-5435-5345-5223 E-MATTEL-AUTOMOTIVE MIKE EAST
49 49MyOnlineITCourses.com
dbVidya.com
partitioning strategies -Oracle
11g
50 50MyOnlineITCourses.com
dbVidya.com
partitioning strategies -
Extensions
51 51MyOnlineITCourses.com
dbVidya.com
Partition Strategies -
Extentions
52 52MyOnlineITCourses.com
dbVidya.com
Quiz
53 53 MyOnlineITCourses.com
dbVidya.com
Quiz
Afirm always queries sales data every monthly
which partitioning will you suggest?
A) List Partitioning
B) Range Partitioning
C) Hash Partitioning
Answer : B
54 54MyOnlineITCourses.com
dbVidya.com
Quiz
A firm always queries data on the basis of
geography and order Ids
which partitioning will you suggest?
A) List – List Partitioning
B) Range -List Partitioning
C) List - Hash Partitioning
Answer : C
55 55MyOnlineITCourses.com
dbVidya.com
Quiz
A Banking firm queries account information of
customers based on account numbers
which partitioning will you suggest?
A) List Partitioning
B) Range Partitioning
C) Hash Partitioning
Answer : C
56 56MyOnlineITCourses.com
dbVidya.com
57 57MyOnlineITCourses.com
dbVidya.com