您好,欢迎访问一九零五行业门户网

Oracle分区表管理的一些笔记

oracle分区表的管理笔记(仅限于对普通表,即堆表的分区管理,iot跟cluster table不再讨论范围内)
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的记录此时可以交换到id 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 ();
其它类似信息

推荐信息