数据分区是针对表的。
为什么要将表分区?
- 分区表通常存储在较小的文件(<2GB)中,易于备份。
- 硬件故障时,只有数据库的一小部分会受到影响。
- 易于数据分析。
- 可以在其他分区继续提供服务的同时维护需要进行维护的表的分区。alter table sale drop partition q1_1999;
基于量程的分区
关键是选择分区键,它应该考虑到3个因素。
- 当数据从分区表中返回时,该列必须是SQL语句谓词的一部分。
- 列中必须有足够的不同值,可以用来在分区之间拆分行。
- 如果确定已经有足够的值,那么这些值中的行分布必须有助于平均行分布。(使分区的数据量要趋同)
create table miling (
originator varchar2(40),
recipient varchar2(40),
message varchar2(4000),
created date,
delivered varchar2(1),
read varchar2(1))
storage (initial 120m next 120m pctincrease 0)
partition by range(created)
(partition mailing_p01 values less than
(to_date('01-JAN-2001', 'DD-MON-YYYY'))
tablespace mailing_ts1,
partition mailing_p02 values less than
(to_date('01-JUL-2001', 'DD-MON-YYYY'))
tablespace mailing_ts2,
partition mailing_p03 values less than
(to_date('01-JAN-2002', 'DD-MON-YYYY'))
tablespace mailing_ts3,
partition mailing_p04 values less than
(to_date('01-JUL-2002', 'DD-MON-YYYY'))
tablespace mailing_ts4,
partition mailing_p05 values less than
(to_date('01-JAN-2003', 'DD-MON-YYYY'))
storage (initial 300m next 100m pctincrease 0)
tablespace mailing_ts5,
partition mailing_p06 values less than
(to_date('01-JUL-2004', 'DD-MON-YYYY'))
tablespace mailing_ts6,
partition mailing_p01 values less than (maxvalue)
tablespace mailing_tsmax);
select table_name, partition_name, tablespace_name
from user_tab_partitions
order by 1,2,3;
创建本地索引(索引分区)
create index mailing_n1 on mailing (created)
local
(parition mailing_n1_p01 tablespace mailing_ts1,
parition mailing_n1_p02 tablespace mailing_ts2,
parition mailing_n1_p03 tablespace mailing_ts3,
parition mailing_n1_p04 tablespace mailing_ts4,
parition mailing_n1_p05 tablespace mailing_ts5,
parition mailing_n1_p06 tablespace mailing_ts6,
parition mailing_n1_pmax tablespace mailing_tsmax)
select table_name, uip.index_name, uip.partition_name, uip.tablespace_name
from user_part_indexes upi, user_ind_partitions uip
where upi.index_name = uip.index_name;
索引分区的边界值是从表分区中继承而来的。
select partition_name, high_value
from user_ind_partitions
select partition_name, high_value
from user_tab_partitions;
在同样的表上创建全局索引
create index mailing_n2 on mailing(recipient)
global partition by range(recipient)
(partition mailing_n2_p01
values less than('G')
tablespace mailingx_ts1,
partition mailing_n2_p02
values less than('P')
tablespace mailingx_ts2,
partition mailing_n2_p03
values less than('T')
tablespace mailingx_ts3,
partition mailing_n3_pmax
values less than(maxvalue)
tablespace malingx_tsmax);