Sunteți pe pagina 1din 63

Partitioning

Demystified
Arup Nanda
Proligence, Inc.
Norwalk, CT

pr ligence Empowering Intelligence


Whats It
Divide and Conquer
The Best Thing Since Sliced Bread

pr ligence Empowering Intelligence


Partition Views
View Created as
SELECT some fields Table A
FROM Table1
UNION ALL
SELECT same fields Table B
FROM Table2

pr ligence Empowering Intelligence


Problem
While INSERTING
Table A

Row1

Table B

pr ligence Empowering Intelligence


Real Partitions
Came Out with Oracle 8.0
Range Partitioned Only Partition A
Partition Key Column
VALUES LESS THAN (CONSTANT)
Partition B

Main Table

pr ligence Empowering Intelligence


Hash Partitions
Where Range is Not Practical
Performance

Partition1
Hash
Row1 Func
Partition2

pr ligence Empowering Intelligence


Composite Partitioning
Partitions are subdivided
Range-Hash Subpart1
Partition1
Range-List Subpart2

Subpart1
Partition2
Subpart2

pr ligence Empowering Intelligence


List Partitioning
Similar to Range but Values are Discrete
PARTITION BY LIST (STATE_CODE)
(
PARTITION P1 VALUES (CT,NY),
PARTITION P2 VALUES (NJ),
PARTITION PM VALUES (DEFAULT)
)
Multi-column not supported

pr ligence Empowering Intelligence


Local Index
Partitioning Schemes
of Index and Table are
Same
Easy to Administer Table

REBUILD part by part

Index

pr ligence Empowering Intelligence


Global Indexes
Indexes That Span
Across All Rows of All
Partitions
Typically Used to
Enforce Primary Keys
Index

pr ligence Empowering Intelligence


Equi-Partitioning
If a Table and another Table
are partitioned in exactly
same way with the same
Partitioning Key
TableA
E.g.
SALES on ORDER_DATE
ORDERS on ORDER_DATE

TableB

pr ligence Empowering Intelligence


SALES

pr ligence Empowering Intelligence


QTR1 QTR2 QTR3 DEFAULT

SALES
PARTITIONS

pr ligence Empowering Intelligence


CUST1 CUST1 CUST1 CUST1
SUBPARTITIONS

CUST2 CUST2 CUST2 CUST2


QTR1 QTR2 QTR3 DEFAULT
CUST3 CUST3 CUST3 CUST3

DEFAULT DEFAULT DEFAULT DEFAULT


SALES
PARTITIONS

pr ligence Empowering Intelligence


ANALYZE

TAB

CUST1 CUST1 CUST1 CUST1


CUST2 CUST2 CUST2 CUST2
QTR1 QTR2 QTR3 DEFAULT
CUST3 CUST3 CUST3 CUST3
DEFAULT DEFAULT DEFAULT DEFAULT

pr ligence Empowering Intelligence


TAB

CUST1 CUST1 CUST1 CUST1


CUST2 CUST2 CUST2 CUST2
QTR1 QTR2 QTR3 DEFAULT
CUST3 CUST3 CUST3 CUST3
DEFAULT DEFAULT DEFAULT DEFAULT

pr ligence Empowering Intelligence


IND CUST1 CUST1 CUST1 CUST1
CUST2 CUST2 CUST2 CUST2
QTR1 QTR2 QTR3 DEFAULT INDEX
CUST3 CUST3 CUST3 CUST3
DEFAULT DEFAULT DEFAULT DEFAULT

TABLE
TAB CUST1 CUST1 CUST1 CUST1
CUST2 CUST2 CUST2 CUST2
QTR1 QTR2 QTR3 DEFAULT
CUST3 CUST3 CUST3 CUST3
DEFAULT DEFAULT DEFAULT DEFAULT

pr ligence Empowering Intelligence


IND CUST1 CUST1 CUST1 CUST1
CUST2 CUST2 CUST2 CUST2
QTR1 QTR2 QTR3 DEFAULT INDEX
CUST3 CUST3 CUST3 CUST3
DEFAULT DEFAULT DEFAULT DEFAULT

TABLE
TAB CUST1 CUST1 CUST1 CUST1
CUST2 CUST2 CUST2 CUST2
QTR1 QTR2 QTR3 DEFAULT
CUST3 CUST3 CUST3 CUST3
DEFAULT DEFAULT DEFAULT DEFAULT

pr ligence Empowering Intelligence


Subpartitioning
Select from a partition by
SELECT FROM TAB1 PARTITION (P1);
Select from a subpartition by
SELECT FROM TAB1 SUBPARTITION (SP1);

Works for Insert/Update/Delete, SQL*Loader


INTO TABLE TAB1 SUBPARTITION (SP1)
And Export, too TABLE=MYTAB1:SP1

pr ligence Empowering Intelligence


Subpartitioning
DBA_SEGMENTS
SELECT * FROM DBA_SEGMENTS
WHERE TABLE_NAME = TAB1
AND PARTITION_NAME = SP1
/
SEGMENT_TYPE

pr ligence Empowering Intelligence


Subpartitions
Subpartition Templates
PARTITION BY RANGE (COL1)
SUBPARTITION BY HASH (COL2)
SUBAPARTITION TEMPLATE
(
SUBPARTITION SP1 TABLESPACE T1,
SUBPARTITION SP2 TABLESPACE T2
)
(
PARTITION P1 VALUES LESS THAN (101),
PARTITION P2 VALUES LESS THAN (201),

P1_SP1, P1_SP2, P2_SP1, P2_SP2
Cant Specify Storage (8i)
Can specify Storage (9i) DBA_SUBPARTITION_TEMPLATES

pr ligence Empowering Intelligence


Partition Pruning
Partition Based on Date 1 partition per quarter
SELECT FROM SALES
WHERE ORDER_DATE = 1/1/2003
Will search only the 2003 Q1 partion

ptest1 ins_ptest1; ptest2

pr ligence Empowering Intelligence


Plan_Table Revisited
Relvant Columns
PARTITION_START
PARTITION_STOP
PARTITION_ID
The step id that decided the partition start and stop
FILTER_PREDICATES
The exact condition used to evaluate partitions

pr ligence Empowering Intelligence


Dbms_xplan
select * from
table (
dbms_xplan.display (
/* plan table name */ plan_table,
/* statement id */ NULL,
/* format */ BASIC, )
SERIAL
ALL,
TYPICAL,
)

pr ligence Empowering Intelligence


Partition Wise Joins
Equi-partioned tables
SALES partitioned on SALES_DATE
REVENUE partitioned on BOOKED_DATE
SELECT FROM SALES S, REVENUE R
WHERE S.SALES_DATE = R.BOOKED_DATE
SALES REVENUE
Partition1 Partition1
Partition2 Partition2
Partition3 Partition3
Ptest3a ptest3b

pr ligence Empowering Intelligence


Partitionwise Join
Hash Partitioned Tables
Elimination will occur in Equality only, not in
range.

Ptest3ha ptest3hb

pr ligence Empowering Intelligence


Character Values in Range
CREATE TABLE EMPLOYEE ()
PARTITION BY RANGE (LAST_NAME)
(
PARTITION P1 VALUES LESS THAN (D%),
PARTITION P2 VALUES LESS THAN (M%),
PARTITION P3 VALUES LESS THAN (T%),
PARTITION PM VALUES LESS THAN (MAXVALUE)
)

ptest4a, ptest4b, ins_ptest4

pr ligence Empowering Intelligence


Multi-column Keys
partition by range Col1 col2
(col1, col2) --- ---- -----
(partition p1 values 50 50 rec1 p1
less than (101, 101), 150 150 rec2 p2
50 150 rec3 p2
partition p2 values less 150 50 rec4 p2
than (201, 201), 101 50 rec5 p2
partition pm values less 101 101 rec6 p2
than 101 150 rec7 p2
(maxvalue, maxvalue)) 201 50 rec8 pm
201 101 rec9 pm
201 ptab1 pm
201 rec10
pr ligence Empowering Intelligence
Multi-Column Decision
Consider < =
Boundary N Boundary
1st Column
Value Value

1st Partition Consider


2nd Column

<
2nd Partition Boundary N 3rd Partition
Value

pr ligence Empowering Intelligence


Converting to Partitioning
Oracle Recommended (MetaLink Note 1070693.6)
Create the partitoned table and Insert
Create Table As Select (CTAS)
Create Small Tables and Exchange Partitions
PROBLEM: Space Requirement and Time

pr ligence Empowering Intelligence


Alternatives
Oracle 9i Online Redefinition
Small Downtime
Space Needed
Oracle 9i

pr ligence Empowering Intelligence


Split-Split Method
Create partitioned table exactly same as source
table with one partition with MAXVALUE
Exchange source table with this partition
Split this partition at the lowest boundary
Split the maximum partition at the next boundary
Repeat till all partitions are created

pr ligence Empowering Intelligence


Example
Table NOPART Partitioned BY RANGE (COL1)
COL1 NUMBER P1 LESS THAN (101)
COL2 VARCHAR2(10) P2 LESS THAN (201)
COL3 CHAR(2) P3 LESS THAN (301)
P4 LESS THAN (401)
Index IN_NOPART PM (MAXVALUE)
(COL2)
Table PART
Constraint CK_NOPART Table, Index, Constraint
(COL3 IS NOT NULL) Name Change

pr ligence Empowering Intelligence


Summary
Create table part
Exchange partition pMAX with table NOPART
Split partition pMAX repeatedly
Drop original table nopart
Rename part to no part
Rename index to in_nopart
Rename constraint to ck_nopart

pr ligence Empowering Intelligence


Pros & Cons
Space Needs Less
Redo Log Generation Minimized
Time Less
Concurrency Higher
Time Still High
Concurency Still Low

pr ligence Empowering Intelligence


Exchanging Partitions
Main Table Source Table
Partition p1 partition p1
Subpartition sp1 partition p2
Subpartition sp2 partition p3
Subpartition sp3
),

pr ligence Empowering Intelligence


Subpartition Statistics
DBMS_STATS.GATHER_TABLE_STATS
tabname => MYTABLE
partname => P1
granularity =>
DEFAULT
GLOBAL
PARTITION
SUBPARTITION
ALL
Spart1.sql

pr ligence Empowering Intelligence


Statistics Collection

Granularity Table Partition Partition Subpart


Global Global Stats Stats
GLOBAL YES NO NO NO
PARTITION NO YES YES NO
DEFAULT YES YES YES NO
SUBPARTITION NO NO YES YES
ALL YES YES YES YES

pr ligence Empowering Intelligence


Parallel Index Rebuilding

q1 q2 q3 q4

QC

One Query Server per Partition


Slows down processing
Cant exploit the Parallel Processing Capabilities

pr ligence Empowering Intelligence


Parallel Index Rebuilding
DBMS_PCLXUTIL.BUILD_PART_INDEX
jobs_per_batch NUMBER DEFAULT 1
procs_per_job NUMBER DEFAULT 1
tab_name VARCHAR2
idx_name VARCHAR2
force_opt BOOLEAN DEFAULT FALSE

pr ligence Empowering Intelligence


The Rule Based Optimizer
Invokes CBO
Makes Up Statistics
Dont Use Partitioning if RBO is Used

pr ligence Empowering Intelligence


Coalesce vs- Merge
Coalesce Hash Partitions
Merge Range and List

coalesce

merge

pr ligence Empowering Intelligence


A Real Life Case
Sales Datawarehouse
Several Customers
Several Quarters
Data Coming from Customers
Irregular Frequency
Data Comes Often Late

pr ligence Empowering Intelligence


DB1 DB2
Cust1 Cust11 ? Cust10

Cust2 Cust9

Cust3 Cust8

Datawarehouse
Cust4 Cust7

Cust5 Cust6

DB3 DB4 DB5 DB6


pr ligence Empowering Intelligence
Problem of Irregular Data

Detail
Table

Summary
Table

Detail
Table

CUST2
pr ligence Empowering Intelligence
DETAIL TABLES
CUST1 aggregation

CUST1 Summary
CUST2 tables

CUST2

pr ligence Empowering Intelligence


Problems
Data Coming from Customers Irregularly
Need to Refresh the Summary Tables when New
Data Arrives
Quarters are Added Continuosly
Archival Requirements Vary Across Customers
Quick Retrieval of Archival Needed

pr ligence Empowering Intelligence


Problems contd.
Summary on Summary Tables as Materialized
Views
Needed to be Refreshed whenever New Data
Arrived
Or When Data is Purged/Reinstated
Customers Added and Deleted Frequently

pr ligence Empowering Intelligence


Objective
To Minimize Downtime for Refreshes
Incrementally Refresh
Partitioning Techniques
To Add Customers Easily
To Add Quarters Easily
To Archive Off and Purge Easily and Atomically
To Restore Archives Quickly

pr ligence Empowering Intelligence


Design
Varying Dimensions
Customer
Quarter
Composite Partitioning
Range (for Quarters)
List (for Customers)
Local Indexes

pr ligence Empowering Intelligence


Design contd
Mat Views Created As Tables First
Query Rewrite and Stale Tolerated
Same Partitioning as Parent Tables
Partition-Wise Joins
Partition Pruning
Partition Independence

pr ligence Empowering Intelligence


cust1 Index of
Owned by Temporary
Cust Schema Table

View INDEX

Filter: TABLE
Where SALES_DATE is Temporary
in that quarter
Table Summary
Table
Massaging
For Customer
Analyzing cust1 and DW
Quarter Q1
pr ligence Empowering Intelligence
cust
Old Sub
Partition

View INDEX

TABLE
Old Sub
Partition

DW

pr ligence Empowering Intelligence


Design
Each Subpartition of Index or Table is kept in
seprate tablespaces named in the format
S<n>_<Cust>_Y<Year>Q<Qtr>
E.g. S1_CUST1_Y02Q2

pr ligence Empowering Intelligence


partitions

MV2

MV1

PARENT

TableSpace1 TableSpace2

pr ligence Empowering Intelligence


Design
MV_* Subpartitions are on the same tablespace as
the parents.

Subparts of MV_S1_0? are in the same TS as S1


Subparts of MV_S2_0? in S2

pr ligence Empowering Intelligence


Cust1

Cust2

Cust3

DEF

pr ligence Empowering Intelligence


Cust1

Cust2

Cust3

DEF

pr ligence Empowering Intelligence


Cust1

Cust2

Cust3

Cust4

DEF

pr ligence Empowering Intelligence


Cust1

Cust2

Cust3

DEF

pr ligence Empowering Intelligence


Cust1

Cust2

Cust3

DEF

pr ligence Empowering Intelligence


Objectives Revisited
To Minimize Downtime for Refreshes
Incrementally Refresh
Partitioning Techniques
To Add Customers Easily
To Add Quarters Easily
To Archive Off and Purge Easily and Atomically
To Restore Archives Quickly

pr ligence Empowering Intelligence


Thank you!

www.proligence.com

pr ligence Empowering Intelligence