44
Partitioning: Tips and Tricks Arup Nanda Longtime Oracle DBA

Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Embed Size (px)

Citation preview

Page 1: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Partitioning: Tips and Tricks

Arup NandaLongtime Oracle DBA

Page 2: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Agenda• Partitioning primer• Choosing a partition strategy• Choosing a partition key• Solutions to common problems using partitioning• Potential issues to watch out for• Creative solutions in partitioning

Partitioning Tips and Tricks2

Page 3: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Partitioning Tips and Tricks3

Page 4: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Local Indexes• Index is partitioned exactly as

the table• Index entries of each part are

found in the corresponding partition in index only

• When table partition is dropped, so is the index partition

• Examplecreate index in_mytabon mytab (col1)local

Partitioning Tips and Tricks4

part4

part3

part2

part1

part4

part3

part2

part1

Table Index

Page 5: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Global Indexes• Entries for all parts of the table

are found all over the index.• Usually used for unique indexes• Index may be optionally

partitioned• When table part is dropped, the

index needs to be rebuilt.• Example

CREATE INDEX PK_MYTABON MYTAB (COL2)GLOBAL;

Partitioning Tips and Tricks5

part4

part3

part2

part1

Index

Table Index

Page 6: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Global-vs-Local Index• Whenever possible, use local index• In Primary Key (or Unique) Indexes:

– If part column is a part of the PK – local is possible and should be used

– e.g. TXN table. PK – (TXN_DT, TXN_ID) and part key is (TXN_DT)

• If not, try to include the column in PKs– E.g. if TXN_ID was the PK of TXN, can you make it (TXN_DT,

TXN_ID)?• Ask some hard design questions

– Do you really need a PK constraint in the DW?

Partitioning Tips and Tricks6

Page 7: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Global indexes can be partitioned• The global indexes can themselves be partitioned in any manner,

different from the table partitioning scheme

Partitioning Tips and Tricks7

create table mytab(

col1 number,col2 date,col3 varachar2,… and so on for other columns …

)partition by range (col1)(partition p1 values less than (101),partition p2 values less than (201),partition p2 values less than (301)

)

create index pk_mytabon mytab (col2)globalpartition by hashpartitions 4;

Global index is hash partitioned while table is range partitioned, on different columns.

Page 8: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Different Range Partitioning

Partitioning Tips and Tricks8

create table mytab(…)partition by range (col1)(partition p1 values less than (101),partition p2 values less than (201),partition p2 values less than (301))

create index IN1 on MYTAB (col4)globalpartition by range (col4)(partition p1 values less than (100),partition p2 values less than (maxvalue))

create index IN1 on MYTAB (col2)globalpartition by range (col4)

Will fail with ORA-14038: GLOBAL partitioned index must be prefixed

create index IN1 on MYTAB (col4,col2) globalpartition by range (col4)

Page 9: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Sub-PartitioningRange-Hash

Sales Date and Sales Trans IDRange-List

Sales Date and Product CodeRange-range

2 date columnsList-range

Product code and then sales dateList-list

Product code and geographic territoryList-Hash

Product code and transaction id

9Partitioning Tips and Tricks

Page 10: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Global Index Maintenance• Global Indexes maintained with the partition

operationalter table mypart drop partition p1 update indexes;

• Or, only global indexes:alter table mypart drop partition p1 update global indexes;

Partitioning Tips and Tricks10

Page 11: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Partitioning Tips and Tricks11

Page 12: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Referential Partitioning• You want to partition

CUSTOMERS on ACC_REP column

• The column is not present on child tables

• Earlier option: add the column to all tables and update it– Difficult and error-prone

• 11g has referential partitioning

Partitioning Tips and Tricks12

CUSTOMERSCUST_IDACC_REP

SALESSALES_IDCUST_ID FKTOT_AMT

LINE_ITEMSSALES_ID FKLINE_IDPRODUCT_ID

part

Page 13: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Referential PartitioningPartition CUSTOMERS as usualcreate table SALES (SALES_ID number not null,CUST_ID number not null,TOT_AMT numberconstraint fk_sales_01foreign key (cust_id)references customers)

partition by reference(fk_sales_01);

Partitions of SALES are created with data from CUSTOMERS.

Partitioning Tips and Tricks13

CUSTOMERSCUST_IDACC_REP

SALESSALES_IDCUST_ID FKTOT_AMT

LINE_ITEMSSALES_ID FKLINE_IDPRODUCT_ID

part

Page 14: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Addressing Ref Partitions• USER_PART_TABLES view has info

– partitioning_type – "REFERENCE"– ref_ptn_constraint_name – the FK name

• The partitions will also bear the same name as the parent

Partitioning Tips and Tricks14

Page 15: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

INTERVAL Partitioning• SALES table partitioned on SALES_DT

– Partitions defined until SEP 2008. Before Oct starts, you have to create the partition

– If you don't create the part, the INSERT will fail on Oct 1st.

• To mitigate the risk, you created the PMAX partition. Undesirable

• When you finally add the OCT08 partition, you will need to split the PMAX – highly undesirable

Partitioning Tips and Tricks15

Page 16: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Interval Partitionscreate table SALES ( sales_id number,sales_dt date )

partition by range (sales_dt) interval (numtoyminterval(1,'MONTH'))

store in (TS1,TS2,TS3) ( partition SEP08 values less than (to_date('2008-

10-01','yyyy-mm-dd')) );

Creates a partition automatically when a new row comes in

Partitioning Tips and Tricks16

This is the first partition. The subsequent

partition names are system generated

Specifies one partition per

month

Page 17: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Addressing Interval Partitions• USER_PART_TABLES view:

– partitioning_type – "INTERVAL"• USER_TAB_PARTITIONS view:

– high_value shows the upper bound of partition• To address a specific partition:

select * from SALES partition for (to_date('22-sep-2008','dd-mon-yyyy'));

Partitioning Tips and Tricks17

Page 18: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Non-Interval Process• To add partitions automatically:

http://arup.blogspot.com/2010/11/tool-to-add-range-partitions.html

• To drop partitions automatically:http://arup.blogspot.com/2010/11/automatic-range-partition-dropping-tool.html

Partitioning Tips and Tricks18

Page 19: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Table

Asynch Global Index

Partitioning Tips and Tricks19

part1

part2

part3

Global Index

part1

part2

part3

alter table drop t partition part3 update global indexes;

A scheduler jobpmo_deferred_gidx_maint_job cleans up

Column ORPHANED_ENTRIES in USER_INDEXES view

giptab_test1.sql

Page 20: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Table

Partial Index

Partitioning Tips and Tricks20

part1

part2

part3

Local Index

part1

part2

part3

SQL> alter table ptab1 modify partition p1 indexing on;

SQL> alter table ptab1 modify partition p2 indexing off;

SQL> create index in_g2_ptab1 on ptab1 (c1) global indexing partial;

partindex_test1.sql

Page 21: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Partitioning Tips and Tricks21

Watchout!

Page 22: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Date Partition-keys• Clear definition helps• This will not choose the partition at compile time

where sales_date between '1-jan-09' and '31-jan-09';

• This will:where sales_date betweenTO_DATE(' 2009-01-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN')

andTO_DATE(' 2009-01-31 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN')

/

Partitioning Tips and Tricks22

date_explain1.sqldate_explain2.sqlexp.sql

Page 23: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Partition-wise Joins• Works for range partitioned tables• Not for hash partitioned• Works only for equality operators; not ranges

Partitioning Tips and Tricks23

pwj_test1.sqlpwj_test2.sql

Page 24: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Multicolumncreate table mcptab1

(

col1 number(10),

col2 number(10)

)

partition by range (col1, col2)

(

partition p1 values less than (101, 101),

partition p2 values less than (201, 201)

)

Partitioning Tips and Tricks24

COL1 COL2---------- ----------

100 100100 101100 102101 100100 200100 201100 202101 101101 102102 100102 101102 102101 200101 201101 202102 200102 201102 202mcpart_test1.sql

Page 25: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Multi-part key Determination

Partitioning Tips and Tricks25

< boundary value?

= boundary value?

Place in Partition 1

Consider 2nd column

Consider 1st column

< boundary value?

Place in Partition 1

Place in Partition 2

No

Yes Yes

Yes

No2nd column is considered only when 1st column is equal to the boundary, not less or not more

A

A

No

Page 26: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Subpart Stats Collection• The normal method to collect statsbegin

dbms_stats.gather_table_stats (ownname=> user, tabname=>'PTEST2');

end;

• Problem:– This populates the partition stats but not subpartition stats– To collect the subpartition stats, you must use granularity

parameter. It has to be either ALL or SUBPARTITIONbegin

dbms_stats.gather_table_stats (ownname=> user, tabname=>'PTEST2‘, granularity=>’SUBPARTITION’);

end;

Partitioning Tips and Tricks26

subpart_stats1.sqlsubpart_stats2.sql

Page 27: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Partition stats collection• The granularity parameter controls the scope for the stats• Possible Values

1. AUTO – determined by Oracle2. GLOBAL AND PARTITION – global stats and partition-level stats (subpartition

level stats are not collected)3. SUBPARTITION – down to subpartition level4. GLOBAL – only global stats5. ALL – global, part and subpart level6. APPROX_GLOBAL AND PARTITION – new in 11g. Global stats are not

calculated; but derived from partition stats

GRANULARITY Table |Partition|Partition |SubpartitionGlobal|Global |Statistics |Statistics

GLOBAL YES NO NO NOPARTITION NO YES YES NODEFAULT YES YES YES NOSUBPARTITION NO NO YES YESALL YES YES YES YES

Partitioning Tips and Tricks27

Page 28: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Stats for a specific partition only• To collect stats for a specific partition (or

subpartition)• Use the partname parameterbegin

dbms_stats.gather_table_stats (

ownname=> user, tabname=>'PTEST2‘, partname=>’SALES_Q1’,

);

end;

• In 11g, the global stats are automatically updated

Partitioning Tips and Tricks28

Page 29: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Partitioning Tips and Tricks29

Creative Solutions

Page 30: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Partition on Virtual Columns• VC: not stored with the table• Computed at runtime• Can be indexed and partitioned

Partitioning Tips and Tricks30

vcpart_test1.sql

Page 31: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Partition on Invisible Columns• Invisible columns are not visible • Need not be entered• Can be indexed and partitioned

Partitioning Tips and Tricks31

Invcolpart_test1.sql

Page 32: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Index Blocks Too Hot to Handle• Consider an index on TRANS_ID – a

monotonically increasing number• It may make a handful of leaf blocks

experience severe contention• This hot area shifts as the access

patterns change• Solution: Reverse Key Index?

Partitioning Tips and Tricks32

1-10 11-20

1-20

12-20

13-20

14-20

Page 33: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Solution: Hash Partitioned Index• Index Can be hash-partitioned,

regardless of the partitioning status of the table

• Table SALES is un-partitioned; while index is partitioned

• This creates multiple segments for the same index, forcing index blocks to be spread on many branches

• Can be rebuilt:alter index IN_SALES_01 rebuild partition

<PartName>;

• Can be moved, renamed, etc.

Partitioning Tips and Tricks33

create index IN_SALES_01on SALES (SALES_TRANS_ID)globalpartition by hash (SALES_TRANS_ID)partitions 8

Page 34: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Reason

Partitioning Tips and Tricks34

1-10 11-20

1-20

12-20

13-20

14-20

1-10 11-20

1-20

12-20

13-20

14-20

Hash partition 1 Hash partition 2 Hash partition 8…

Page 35: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

When• Overlap between Logical Modeling and Physical Design

• Last part of logical design and first part of physical design• When should partitioning be used

– In almost all the time for large tables• There is no advantage in partitioning small tables, right?

– Wrong. In some cases small tables benefit too

Logical PhysicalPartitionDesign

Partitioning Tips and Tricks35

Page 36: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Partitioning Tips and Tricks36

How to Choose

Partitioning

Page 37: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Why? Common Reasons• Easier Administration:

– Smaller chunks are more manageable – Rebuilding indexes partition-by-partition– Data updates, does not need counters

• Performance:– full table scans are actually partition scans– Partitions can be joined to other partitions– Latching

Partitioning Tips and Tricks37

Page 38: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

More Important Reasons• Data Purging

– DELETEs are expensive – REDO and UNDO– Partition drops are practically free– Local indexes need not be rebuilt

• Archival– Usual approach: insert into archival table select * from

main table– Partition exchange– Local indexes need not be rebuilt

Partitioning Tips and Tricks38

Page 39: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Materialized Views Refreshes• Partition Exchange

– Create a temp table– Create Indexes, etc.– When done, issue:

alter table T1 exchange partition sp11 with table tmp1;

– Data in TMP1 is availablesp41

sp31

sp21

sp11

sp32

sp22

sp12

sp33

sp13partition p1

partition p2

partition p3

partition p4Table

TempTable

Partitioning Tips and Tricks39

Page 40: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Backup Efficiency• When a tablespace is read-only, it does not change

and needs only one backup– RMAN can skip it in backup– Very useful in DW databases– Reduces CPU cycles and disk space

• A tablespace can be read only when all partitions in them can be so

SQL> alter tablespace Y08M09 read only;

Partitioning Tips and Tricks40

Page 41: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Data Transfer• Traditional Approach

insert into target select * from source@dblink

• Transportable Tablespace– Make it read only– Copy the file– "Plug in" the file as a new tablespace into

the target database– Can also be cross-platform

Source Target

TS1TST

TS1TST

Partitioning Tips and Tricks41

Page 42: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Information Lifecycle Management• When data is accessed less frequently, that can

be moved to a slower and cheaper storage, e.g. from Fiber to SATA

• Two options:1. Create a tablespace ARC_TS on cheaper

disksALTER TABLE TableName MOVE

PARTITION Y07M08 TABLESPACE ARC_TS;Reads will be allowed; but not writes

2. ASM ApproachALTER DISKGROUP DROP DISK … ADD DISK

…Fully available

FastDisk

SlowDisk

TS1 ARC_TS

Partitioning Tips and Tricks42

Page 43: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

How to Decide• First, decide on the objectives of partitioning. Multiple

objectives possible• Objectives

– Data Purging– Data Archival– Performance– Improving Backups– Data Movement– Ease of Administration– Different Type of Storage

Assign priorities to each of these objectives

Partitioning Tips and Tricks43

Page 44: Partitioning: Tips and Tricks - Proligence Home · Different Range Partitioning Partitioning Tips and Tricks 8 create table mytab (…) partition by range (col1) (partition p1 values

Thank You!

My Blog: arup.blogspot.comMy Tweeter: arupnanda

44