2012年10月14日日曜日

[分区表] 分区表基本操作


# 分区表与分区索引相关视图
dba_part_tables
dba_tab_partitions
dba_tab_subpartitions
dba_part_key_columns
dba_subpart_key_columns
dba_part_indexes
dba_ind_partitions


1. 查询所有有分区的表
select * from dba_tables where partitioned = 'YES'


2. 查询表分区
select * from dba_tab_partitions where table_name = 'T'


3. 指定分区查询
select * from t partition (p1)


4. 增加一个分区

4-1. 增加列表分区
alter table t add partition p3 values (3);

4-2. 增加散列分区
alter table t add partition p3;

4-3. 增加范围分区
alter table t add partition p3
values less than (to_date('2012-05-01','yyyy-MM-dd'))

4-4. 增加组合分区
alter table t add partition p4
values less than(to_date('2012-05-01','yyyy-MM-dd'))
(
    subpartition p4_subp1 values('value1'),
    subpartition p4_subp2 values('value2')
)

4-5. 存在 default 分区存在时, 增加新分区的方法

4-5-1. 备份 default 分区数据
create table t_temp as select * from t where 1 = 0
alter table t exchange partition p0 with table t_temp

4-5-2. 删除 default 分区
alter table t drop partition p0

4-5-3. 增加新分区和 default 分区
alter table t add partition p4 values (4)
alter table t add partition pn values (default)

4-5-4. 恢复 default 分区数据
-- alter table t exchange partition p0 with table t_temp
insert into t select * from t_temp

4-5-5. 删除备份表
drop table t_temp purge


5. 增加子分区, 只能是散列或列表子分区
alter table t modify partition p1 add subpartition p11 values (x)


6. 增加一个 default 分区
alter table t add partition pn values (default)
alter table t add partition pn values less than (maxvalue)


7. 分区重命名
# 对全局, 局部索引无影响
alter table t rename partition p3 to p4
alter index t_idx rename partition p3 to p4


8. 删除一个表分区
alter table t drop partition p1


9. 删除子分区
alter table t drop subpartition p11


10. 截断分区

10-1. 对索引的影响
alter table t truncate partition p1 [update indexes];
# 截断分区会自动维护局部索引
# 如果不加 update indexes, 可能会使全局索引失效, 需要全局重建索引
select partition_name,status from dba_ind_partitions where index_owner='T_IDX';
alter index t_idx rebuild partition p1 [online];

10-2. 截断带有约束的分区表
# 禁用约束
alter table t disable constraint const1

# 截断分区
alter table t truncate partitoin p1 [update indexes]

# 启用约束
alter table t enable constraint const1


11. 截断子分区
alter table t truncate subpartition p11


12. 合并分区
# 合并后的分区名, 不能是边界值较低的那个
# 合并前边界值较小分区在前, 大的在后
# 只能合并相邻分区
# 全局索引失效, 所涉及的局部索引失效
# 若加 update indexes 子句, 全局局部索引均不失效
alter table t merge partitions p1, p2 into partition p2


13. 拆分分区
# 散列分区不可拆分
# 全局索引失效, 所涉及的局部索引失效
# 若加 update indexes 子句, 全局局部索引均不失效

13-1. 拆分范围分区
# 2003-01-01 为分界点, p21 < p22
alter table t split partition p2 at (to_date('2003-01-01','YYYY-MM-DD')) into (partition p21,partition p22)

13-2. 拆分列表分区
# 拆分后, 4 的记录分入 p4 分区, 其余分入 p5 分区
alter table t split partition pn values(4) into (partition p4,partition p5);

14. 分区移动表空间

14-1. 查看分区在哪个表空间
select table_name, partition_name, tablespace_name, subpartition_count
from dba_tab_partitions where table_owner='GO';

14-2. 移动分区
# 若不加 update indexes 子句, 全局和所在区的局部索引均失效
alter table t move partiton p1 tablespace tbs1 [update indexes];

14-3. 检查是否移动成功
select table_name, partition_name, tablespace_name, subpartition_count
from dba_tab_partitions where table_owner='GO';

14-4. 移动表空间后要重建索引, 否则索引会变得无效
alter index t_idx rebuild


15. 接合分区, 只适用于散列分区
alter table t coalesca partition