Partitioning in Databases

advertisement
Partitioned tables
•
•
•
•
•
•
•
Partitions / partitioning / partitioned tables
For very large tables
Improve querying
Easier admin
Backup and recovery easier
Optimiser knows when partitioning used
Can use in SQL also
Creating a PT
• Create table FRED (
ID
number
name
varchar2(25)
age
number
constraint fred_pk primary key (ID)
)
partition by range (age)
(partition PART1 values less than (21)
partition PART2 values less than (40)
partition PART3 values less than (maxvalue)
Warning
• Specification of partition is exclusive
• Maxvalue is a general term to pick up
anything that failed so far
• Works for text as well as number
Hash partition
• Only in Oracle 8i and above
• Uses a numerical algorithm based on
partition key to determine where to place
data
• Range partition = consecutive values
together
• Hash = consecutive values may be in
different partitions
What is Hash?
• Imagine 8GB table – split in 8 / 1 GB
• No intuitively clever way to split data
• Or obvious way is totally imbalanced
– 1 partition 7BG + 7 140MB
– Huge variations in performance
• Randomise breakdown of data so objects of
similar size
– Select one column
– Select number of partitions
– Oracle does the rest!
Mechanics of hashing
• Each record is allocated into a bucket based on
key value – e.g. Name = Joe
• Applying the hashing function to the value Joe
uniquely returns the bucket number where the
record is located:
• E.g. using prime number
–
–
–
–
divide KEY by a prime number
If text, translation into numeric value using ASCII code
use remainder of the division = address on the disk
if record already at same address - pointer to overflow
area.
Hash partition - SQL
Create table FRED (
Name
varchar2(25) primary key,
Age
number,
Years abroad
number
)
Partition by hash (age)
Partitions 2
Store in (Part1_fred, Part2_fred);
(Not compulsory)
Sub-partitions
Create table FRED (
Name
varchar2(25) primary key,
Age
number,
Years abroad
number
)
Partition by range (years abroad)
Subpartition by hash (name)
Subpartitions 5
(partition Part1 values less than (1)
partition Part2 values less than (3)
partition Part3 values less than (6)
partition Part4 values less than (MAXVALUE));
Indexing partitions
• Performance requirements may mean Partitioned
tables should be indexed (separate issue)
Create index FRED_NAME on FRED (name)
Local
Partitions (Part1, Part2, Part3, Part4)
• Local means create separate index for each
partition of the table
• Alternative is to create a global index with values
from different partitions
• Global indexes cannot be created for Hash
partitions
Download