全部博文(163)
分类: Oracle
2011-09-09 14:55:41
在数据仓库中经常会用到 Partition Tables & Index,这些天客户都放假,有点空闲时间,把关于 Partition 的概念重新过一遍,顺便简单整理如下:
What Partition:
Partition 就是分区,将一张表或者索引根据不同的条件划分成一个个分区,逻辑上来看跟普通表无异。
When Partition:
一般来说,当你某张表中的数据量非常庞大,达到百万级别,或数据大小达到以G为单位计时候,建议使用 Partition。
Why Partition:
对于应用来说,由于数据量巨大,当进行巨额数据之间的 join 操作时,使用 Partition 会极大的提高性能;对于管理来说,Partition 大大的加强了管理的简便性。
Which Partition:
oracle 10g 主要提供了4种分区的方式
1, Range Partitioning
2, Hash Partitioning
3, List Partitioning
4, Composite Partitioning
Range Partitioning
这种分区是最普遍的分区方法,表中的数据若能够按照逻辑范围来划分(比如月份),就可以采用此方式。
复制内容到剪贴板 程序代码
Create TABLE sonic_range
(sonic_id NUMBER(5),
sonic_name VARCHAR2(30),
sonic_date DATE)
PARTITION BY RANGE(sonic_date)
(PARTITION sonic_jan2009 VALUES LESS THAN(TO_DATE('02/01/2009','DD/MM/YYYY')),
PARTITION sonic_feb2009 VALUES LESS THAN(TO_DATE('03/01/2009','DD/MM/YYYY')),
PARTITION sonic_mar2009 VALUES LESS THAN(TO_DATE('04/01/2009','DD/MM/YYYY')));
Hash Partition
这种分区是基于 Hash 算法的,在一个或多个列上应用 Hash 函数,然后将表中的数据尽可能平均的打散,分布在不同的分区上。
复制内容到剪贴板 程序代码
Create TABLE sonic_hash
(sonic_id NUMBER(5),
sonic_name VARCHAR2(30),
sonic_week_no NUMBER(2))
PARTITION BY HASH(sonic_id)
PARTITIONS 4;
List Partition
通过明确的关键字,将数据进行分区,这些关键字都是离散的,一般是无规律可循的
复制内容到剪贴板 程序代码
Create TABLE sonic_list
(sonic_id NUMBER(5),
sonic_name VARCHAR2(30),
sonic_state VARCHAR2(20),
sonic_date DATE)
PARTITION BY LIST(sonic_state)
(PARTITION sonic_north VALUES('Ji lin', 'Bei Jing') COMPRESS,
PARTITION sonic_south VALUES('Guang dong', 'Hai nan', 'Yun nan'),
PARTITION sonic_central VALUES('Jiang su', 'Shang hai'));
Composite Partitioning
顾名思义,将前三者任意两种或者三种同时进行分区,就是组合分区,这里就不举例了。
How Partition:
复制内容到剪贴板 程序代码
Add Partition:
Alter TABLE table_name ADD PARTITION partition_name VALUES LESS THAN (TO_DATE('2009-12-01','YYYY-MM-DD') TABLESPACE tablespace_name;
Delete Partition:
Alter TABLE table_name Drop PARTION partition_name;
Truncate Partition:
alter table table_name truncate partiton partition_name;
Merge Partition:
alter table table_name merge partitons partition_name1, partition_name2 into partition_name3;
alter index index_name rebuild partition partition_name3 parallel 2;
Split Partition:
Alter TABLE table_name SPLIT PARTITON partition_name AT TO_DATE ('1999-11-01','YYYY-MM-DD') INTO (partition partition_name1, partition partition_name2);
alter table table_name split partition partition_name values (1,2) into (partition partition_name1,partition partition_name2);
Exchange Partition:
alter table table_name exchange partition partition_name with table commom_table_name;
Query Partition:
select * from table_name partition(partition_name)
Export Partition:
exp username/password tables=table_name:partition_name file=xxx.dmp
Import Partition:
imp username/password file=xxx.dmp tables=(table_name:partition_name)
Get Partition Information
dba_segments
dba_part_key_columns
dba_tables
dba_tab_partitions
dba_indexes
dba_ind_partitions
那么Oracle最大允许存在多少个分区呢?
我们可以从Oracle的Concepts手册上找到这个信息,对于Oracle9iR2:
Tables can be partitioned into up to 64,000 separate partitions.
对于Oracle10gR2,Oracle增强了分区特性:
Tables can be partitioned into up to 1024K-1 separate partitions.
真的会有人尝试来突破这个分区极限吗? 你的数据库最大的分区表有多少个分区呢?
关于何时应该进行分区,Oracle有如下建议:
■ Tables greater than 2GB should always be considered 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.