当前位置:文档之家› ORACLE分区表管理的一些笔记

ORACLE分区表管理的一些笔记

分区表的管理笔记(仅限于对普通表,即堆表的分区管理,IOT跟CLUSTER TABLE不再讨论范围内)


1. 增加分区(add partition)
语法是:alter table xxx add partition…
需要注意的是如果分区中存在maxvalue或default分区add partition会报错,应使用split

如:

Alter table t_range add partition p5 values less than (50) [tablespace users];
--50 要大于之前分区的所有值
Alter table t_list add partition p5 values (7,8,9) [tablespace users];
--7,8,9均不能在之前分区中出现
Alter table t_hash add partition [p5] [tablespace users];

增加子分区:
Alter table xxx modify partition p1 add subpartition …
如:增加RANGE-HASH子分区
ALTER TABLE diving MODIFY PARTITION locations_us
ADD SUBPARTITION us_locs5 TABLESPACE us1;

Range,list增加分区不会影响索引(包括global 跟local),HASH增加分区会让数据重新分配,产生IO,如果不指定update indexes 选项则会导致有数据移动的索引unusable,需要重新编译。
当然,我们说的对索引的影响都是在表中有数据的情况下,没数据当然影响不到索引了。


2. 合并分区(coalesce partition)
Alter table xxx coalesce partion [update indexes];
Alter table xxx modify partition p1 coalesce subpartition;
仅适用于HASH分区或子分区,合并一次会减少一个分区(最少能减少到1个),数据重新分配,产生IO,有数据移动的索引失效(如果不指定update indexes的话).

3. 删除分区(drop partition)
Alter table xxx drop partition ppp;
删除子分区:
Alter table xxx drop subpartition ppp;
此功能hash不支持。同时要注意,删除分区会同时删除该分区内数据。
同样,如果不指定update indexes的话该操作会导致GLOBAL索引失效,而LOCAL不会,因为对应的LOCAL索引分区也被删除了嘛,其他分区的LOCAL不会受到影响。

4. 交换分区(exchange partition)
Alter table tb1 exchange partition/subpartition p1 with table tb2;
据说是采用了更改数据字典的方式,所以速度比较快。
可以是分区跟非分区表交换,子分区跟非分区表交换,组合分区跟分区表交换。
例如:
组合分区跟分区表交换:
CREATE TABLE t1 (i NUMBER, j NUMBER)
PARTITION BY HASH(i)
(PARTITION p1, PARTITION p2);


CREATE TABLE t2 (i NUMBER, j NUMBER)
PARTITION BY RANGE(j)
SUBPARTITION BY HASH(i)
(PARTITION p1 VALUES LESS THAN (10)
SUBPARTITION t2_pls1
SUBPARTITION t2_pls2,
PARTITION p2 VALUES LESS THAN (20)
SUBPARTITION t2_p2s1
SUBPARTITION t2_p2s2));

ALTER TABLE t2 EXCHANGE PARTITION p1 WITH TABLE t1
WITH VALIDATION;

如果指定WITH VALIDATION(默认) 会对交换进来的数据进行合法检查,看是否符合该分区的规则,WITHOUT VALIDATION 会忽略合法检查(比如ID=12的记录此时可以交换到I

D VALUES LESS THAN (10)的分区里),但如果表上有primary key 或unique 约束的话,指定without validation会被忽略。
同样,如果不指定UPDATE INDEXES ,GLOBAL 索引会失效,需要重新编译。


5. 合并分区(merge partitions)
Alter table xxx merge partitions/subpartitions p1,p2 into partiton/subpartition p3 [TABLESPACE tablespace_name];
HASH不适用,因为它有COALESCE了嘛。
表分区必须是相邻的。
跟COALESCE一样,会产生IO,数据量大的话,IO也是相当大的。
同样可以用UPDATE INDEXES 避免索引失效


6. 修改LIST分区—ADD VALUES
Alter table xxx modify partition/subpartition p1 add values(7,9);
要注意的是,增加的VALUES不能在其他分区列的VALUES值中存在,也不能在DEFAULT分区(如果有的话)中有相应VALUES.
不会影响索引


7. 修改LIST 分区—DROP VALUES
Alter table xxx modify partition/subpartition p1 drop values(8,9);
同样,删除的values 不能存在记录.
不会影响索引


8. 拆分分区(split partitions)
功能与MERGE PARTITIONS相反。通常我们会用来拆分MAXVALUE/DEFAULT分区。
Range partition:
Alter table xxx split partition/subpartition p1 at (15) into (partition/subpartition p1_new1,partition/subpartition p1_new2);
List partition:
Alter table xxx split partition/subpartition p1 values(15,16) into (partition/subpartition p1_new1,partition/subpartition p1_new2);
原分区中符合新值定义的记录会存入第一个分区,其他存入第二个分区,当然,在新分区后面可以指定属性,比如TABLESPACE。
HASH分区不适用。
会产生IO
同样,可用update indexes 来避免索引失效


9. 截断分区(truncate partition)
跟TRUNCATE TABLE一样,截断该分区内的数据。
Alter table xxx truncate partition/subpartition p1;
同样,可用update indexes 来避免索引失效


10. 移动分区(move partition)
Alter table xxx move partition/subpartition p1 …;
有些功能比如改变分区表空间,modify partition就做不到,此时就可以用move partition来做。
Use the MOVE PARTITION clause of the ALTER TABLE statement to:
? Re-cluster data and reduce fragmentation
? Move a partition to another tablespace
? Modify create-time attributes
? Store the data in compressed format using table compression

如:
ALTER TABLE parts MOVE PARTITION depot2
TABLESPACE ts094 NOLOGGING COMPRESS;
(如果指定compress,affects only future storage, but not existing data.)

同样,可用update indexes 来避免索引失效


11. 重命名分区(rename partition)
Alter table xxx rename partition/subpartition p1 to p1_new;
跟重命名表差不多。


12. 修改分区默认属性(modify default attributes)
修改表属性:alter table xxx modify default attributes …
修改分区属性(适用于组合分区):alter table xxx modify default attributes for partition p1 …
只对以后添加的分区产生影响,适用于所

有分区,其中hash分区只能修改表空间属性。
如:
Alter table xxx modify default attributes tablespace users;


13. 修改子分区模板属性(set subpartition template)
Alter table xxx set subpartition template (…);
仅影响以后的子分区,当前的子分区属性不会改变
如:
Alter table xxx set subpartition template
(partition p1 tablespace tbs_1,
Partition p2 tablespace tbs_2);
如果要取消掉子分区模板:
Alter table xxx set subpartition template ();



相关主题
文本预览
相关文档 最新文档