分区表常用操作

1.有关表分区的一些维护性操作:
  5.1 添加分区
 以下代码给sales表添加了一个p3分区
 alter table sales add partition p3 values less than(to_date('2003-06-01','yyyy-mm-dd'));
  注意:以上添加的分区界限应该高于最后一个分区界限。
 以下代码给sales表的p3分区添加了一个p3sub1子分区
 alter table sales modify partition p3 add subpartition p3sub1 values('complete');
  5.2 删除分区
 以下代码删除了p3表分区:
 alter table sales drop partition p3;
 在以下代码删除了p4sub1子分区:
 alter table sales drop subpartition p4sub1;
  注意:如果删除的分区是表中唯一的分区,那么此分区将不能被删除,要想删除此分区,必须删除表。
  5.3 截断分区
 截断某个分区是指删除某个分区中的数据,并不会删除分区,也不会删除其它分区中的数据。当表中即使只有一个分区时,也可以截断该分区。通过以下代码截断分区:
 alter table sales truncate partition p2;
 通过以下代码截断子分区:
 alter table sales truncate subpartition p2sub2;
  5.4 合并分区
 合并分区是将相邻的分区合并成一个分区,结果分区将采用较高分区的界限,值得注意的是,不能将分区合并到界限较低的分区。以下代码实现了p1 p2分区的合并:
 alter table sales merge partitions p1,p2 into partition p2;
  5.5 拆分分区
 拆分分区将一个分区拆分两个新分区,拆分后原来分区不再存在。注意不能对hash类型的分区进行拆分。
 alter table sales sblit partition p2 at(to_date('2003-02-01','yyyy-mm-dd')) into (partition p21,partition p22);
  5.6 接合分区(coalesca)
 结合分区是将散列分区中的数据接合到其它分区中,当散列分区中的数据比较大时,可以增加散列分区,然后进行接合。
  值得注意的是,接合分区只能用于散列分区中。通过以下代码进行接合分区:
 alter table sales coalesca partition;
  5.7 重命名表分区
 以下代码将p21更改为p2
 alter table sales rename partition p21 to p2;
  5.8 相关查询
 --跨分区查询
 select sum( *) from
  (select count(*) cn from t_table_ss partition (p200709_1)
  union all
  select count(*) cn from t_table_ss partition (p200709_2)
  );
 --查询表上有多少分区
 select * from user_tab_partitions where table_name='tablename';
 --查询索引信息
 select object_name,object_type,tablespace_name,sum(value)
   from v$segment_statistics
   where statistic_name IN ('physical reads','physical write','logical reads') and object_type='INDEX'
  group by object_name,object_type,tablespace_name
  order by 4 desc  
  /* 
  create or replace view sys.v_$segment_statistics as
    select "owner","object_name","subobject_name","tablespace_name","ts#","obj#","dataobj#","object_type","statistic_name","statistic#","value"
      from v$segment_statistics;
  */
 --显示分区表信息:
 select * from dba_part_tables;
 select * from all_part_tables;
 select * from user_part_tables;
 --显示分区信息:
 select * from dba_tab_partitions
 select * from all_tab_partitions
 select * from user_tab_partitions
 --显示子分区信息
 select * from dba_tab_subpartitions
 select * from all_tab_subpartitions
 select * from user_tab_subpartitions
 --显示分区列信息:
 select * from dba_part_key_columns
 select * from all_part_key_columns
 select * from user_part_key_columns
 --显示子分区列信息:
 select * from dba_subpart_key_columns
 select * from all_subpart_key_columns
 select * from user_subpart_key_columns
 --怎样查询出oracle数据库中所有的的分区表
 select * from user_tables a where a.partitioned='YES'
 --删除一个表的数据是
 truncate table table_name;
 --删除分区表一个分区的数据是
 alter table table_name truncate partition p5;

posted @ 2014-11-26 21:05  智能先行者  阅读(407)  评论(0)    收藏  举报