当前位置:文档之家› oracle分区操作文档

oracle分区操作文档

1. 分区建立1.1. 建立分区表create table bigtable(sale_date date,product_id number,sale_count number,charge number,sales_id number)tablespace ts_partitionpctfree 5pctused 80initrans 1maxtrans 255parallel(degree 2)storage(initial 2Mnext 2Mminextents 1maxextents unlimitedpctincrease 0)partition by range(sale_date)(partition sale_date_20020101 values less than (to_date('20020102','yyyymmdd')),partition sale_date_20020102 values less than (to_date('20020103','yyyymmdd')));1.2. 建立分区索引create index create index idx_bigtable_product_id on bigtable(product_id) parallel 2 localtablespace users on bigtable(product_id) parallel 2 local tablespace users;分区索引和全局索引:分区索引就是在所有每个区上单独创建索引,它能自动维护,在drop或truncate某个分区时不影响该索引的其他分区索引的使用,也就是索引不会失效,维护起来比较方便,但是在查询性能稍微有点影响。

create index idx_ta_c2 on ta(c2) local (partition p1,partition p2,partition p3,partition p4);或者 create index idx_ta_c2 on ta(c2) local ;另外在create unique index idx_ta_c2 on ta(c2) local ;系统会报ORA-14039错误,这是因为ta表的分区列是c1,不支持在分区表上创建PK主键或时主键列不包含分区列,创建唯一约束也不可以这样。

oracle全局索引就是在全表上创建索引,它可以创建自己的分区,可以和分区表的分区不一样,也就是它是独立的索引。

在drop或truncate某个分区时需要创建索引alter index idx_xx rebuild,也可以通过alter table table_name drop partition partition_name update global indexes;实现,但是如果数据量很大则要花很长时间在重建索引上。

可以通过查询user_indexes、user_part_indexes和user_ind_partitions视图来查看索引是否有效。

create index idx_ta_c3 on ta(c3);或者把全局索引分成多个区(注意和分区表的分区不一样):create index idx_ta_c4 on ta(c4) global partition by range(c4)(partition ip1 values less than(10000),partition ip2 values less than(20000),partition ip3 values less than(maxvalue));注意全局索引上的引导列要和range后列一致,否则会有ORA-14038错误。

如不能这样写:create index idx_ta_c4 on ta(c1) global partition by range(c4)(partition ip1 values less than(10000),partition ip2 values less than(20000),partition ip3 values less than(maxvalue));oracle会对主键自动创建全局索引如果想使某个分区索引置为不可用则可以用如下脚本:alter index idx_tab1 modify partition "ind partition name" unusable如果想在主键的列上创建分区索引,除非主键包括分区键,还有就是主键建在两个或以上列上,否则不能创建。

在频繁删除表的分区且数据更新比较频繁时为了维护方便要避免使用全局索引。

1.3. 增加分区alter table bigtable add partition sale_date_20020103values less than (to_date('20020104','yyyymmdd'));1.4. 截断分区alter table bigtable truncate partition sale_date_20020103;1.5. 丢弃分区alter table bigtable drop partition sale_date_20020103;1.6. 交换分区如果分区表里含有LOCAL 的索引,此分区的索引将处于不可用状态,需要重建:alter index idx_bigtable_product_id rebuild partition sale_date_20020102 tablespace users;如果被交换表和分区表的索引结构相同,可以用including indexes 连同索引一起交换,不1.7. 移动分区alter table bigtable move partition sale_date_20020102 tablespace user2;1.8. 修改分区alter table bigtable modify partitionsale_date_20020102 storage(pctincrease 10);1.9. 重命名分区alter table bigtable rename partitionsale_date_20020102 to sale_date_20020103;1.10. 分割分区alter table bigtable split partition sale_date_20020104at (to_date('20020104','yyyymmdd'))into (partition sale_date_20020103,partition sale_date_20040104);1.11. 合并分区合并分区的具体语法格式为:alter table 表名 merge partitions 分区名1,分区名2 into partition 合并后分区的名字。

alter table PM_V01R00_UTRANCELL_HSDPA merge partitionsPART_2010_06_01,PART_2010_06_05_BAK into partitionPART_2010_06_05;1.12. 分区表数据的逻辑备份exp username/password file=bigtable20020103_4.dmp tables= (bigtable:sale_date_20020103,bigtable:sale_date_20020104)1.13. 分区表数据的逻辑恢复imp username/password file=bigtable20020103_4.dmp tables= (bigtable:sale_date_20020103,bigtable:sale_date_20020104) ignore=y2. 分区维护2.1. 分区管理视图分区用到的视图:管理user_tab_partitions,user_part_indexes,user_part_tables,user_partial_drop_tabs,user_part_indexesuser_part_key_columns========================2.2. --查询分区所属的硬盘信息select file_name,file_id,bytes/1024/1024from dba_data_fileswhere tablespace_name='NMCPMTS';2.3. --查询表所有的分区信息select * from dba_tab_partitions where table_name='PM_V01R00_UTRANCELL_HSDPA';2.4. --查询表分区占用的空间大小select d.segment_name,d.partition_name,d.bytes/1024/1024 cnt_mb from dba_segments d whered.segment_name='PM_V01R00_UC_RRC_SUBCOUNTER';2.5. 为分区表建立一个单独的表空间create tablespace ts_partition datafile '/home/oracle/oradata/esales/partition.dbf' size 10Mextent management local uniform size 2M;查询分区表:select * from hcl partition(p_200809)2.6. 查看索引类型是否为分区索引select t1.index_name,t1.partitioned from Dba_Indexes t1wheret1.Table_Name=upper('CCB_COGNOS_PROD_BALANCE_AA')select t1.index_name,t1.partitioned from Dba_Indexes t1wheret1.Table_Name=upper('CCB_COGNOS_PROD_BALANCE_AA')将索引重建为索引:alter indexB_COGNOS_PROD_BALANCE_AA_N1 rebuild Nologging 找出失效的分区索引:select t.Index_Name, t.Partition_Name, t.Tablespace_Name, t.Statusfrom Dba_Ind_Partitions twhere t.Index_Name = 'CMZ_LOCAL_IDX_2'重建所有状态为unusable的索引重建脚本: Java代码ALTER INDEX 索引名REBUILD PARTITION 分区名TABLESPACE 表空间名NOLOGGING2.7. 查看介于某个时间段的分区先建立表create table pnmc_partitions(table_owner varchar2(50),table_name varchar2(40),partition_name varchar2(40),high_value varchar2(4000),TABLESPACE_NAME varchar2(50))tablespace NMCCMTSpctfree 10initrans 1maxtrans 255storage(initial 64minextents 1maxextents unlimited);然后建立测试窗口declarecursor my_cursor isselect table_owner ,table_name ,partition_name ,high_value ,tablespace_namefrom dba_tab_partitions;v_pnmc_partion pnmc_partitions%rowtype;begindelete from pnmc_partitions;commit;open my_cursor;loopfetch my_cursor into v_pnmc_partion;exit when my_cursor%notfound;v_pnmc_partion.high_value:=substr(v_pnmc_partion.high_value,11,19); insert into pnmc_partitions(table_owner, table_name, partition_name, high_value,tablespace_name) values(v_pnmc_partion.table_owner,v_pnmc_partion.table_name,v_pnmc_partion.partition_name,v_pnmc_partion.high_value,v_pnmc_partion.tablespace_name);end loop;commit;close my_cursor;end;然后再进行查询selecttable_owner ,table_name ,partition_name ,high_value ,tablespace_namefrom pnmc_partitions t where table_owner='NMC'and to_date(high_value,'YYYY-MM-DD HH24:MI:SS')<to_date('2009-03-01 00:00:00','YYYY-MM-DD HH24:MI:SS');--delete 操作后表空间的释放;select'alter table '||table_name||' drop partition '||partition_name||';',partition_name, high_valuefrom pnmc_partitions twhere table_owner ='NMC3G'and to_date(high_value,'YYYY-MM-DD HH24:MI:SS')<to_date('2011-06-15 00:00:00','YYYY-MM-DD HH24:MI:SS')and table_name in('PM_V01R00_UC_RLC_SUBCOUNTER',--'PM_V01R00_UC_FP_SUBCOUNTER',--'PM_V01R00_UC_CR_SUBCOUNTER',--'PM_V01R00_UTRANCELL_SUBCOUNTER',--'PM_V01R00_UTRANCELL_HSDPA',--'PM_V01R00_UTRANCELL_HSUPA')—--查询分区保存时间select table_name,(max(to_date(high_value, 'YYYY-MM-DD HH24:MI:SS')) -min(to_date(high_value, 'YYYY-MM-DD HH24:MI:SS')) )/365 ||’年’partition_save_timefrom pnmc_partitions twhere table_owner = 'CDMA2000'group by table_name--移动分区select'alter table '||table_name||' move partition '||partition_name||' tablespace NMCPMTS;'from pnmc_partitionswhere table_namein('PM_AN_CELL_INFO','PM_1601_TBL_RST_1275071817_3','PM_AN_CELL_SUM_INFO')--删除分区drop index UIDX_PM_AN_CELL_INFO;create unique index UIDX_PM_AN_CELL_INFO on PM_AN_CELL_INFO (START_TIME, SYS_INT_ID) localtablespace INDXpctfree 10initrans 2maxtrans 255storage(initial 2088Kminextents 1maxextents unlimited);。

相关主题