ITPub博客

首页 > 数据库 > Oracle > 非分区表转换为分区表和partition index

非分区表转换为分区表和partition index

原创 Oracle 作者:tolilong 时间:2012-07-04 14:32:37 0 删除 编辑

非分区表转换为分区表

1).检测是否可以转换

SQL> exec dbms_redefinition.can_redef_table('scott','test')

已順利完成 PL/SQL 程序.

2).创建临时表并化分区

create table test_temp

(owner varchar2(30) not null,

object_name varchar2(30) not null,

object_id number not null,

object_type varchar2(30)

)

partition by range(object_id)

(partition p1 values less than (400) tablespace users,

partition p2 values less than (950) tablespace test,

partition p3 values less than (maxvalue) tablespace users

)

3).开始重定义表

SQL> exec dbms_redefinition.start_redef_table('scott','test','test_temp')

已順利完成 PL/SQL 程序.

4).创建索引限制及触发器

SQL> var v_num number

SQL> exec dbms_redefinition.copy_table_dependents('scott','test','test_temp',dbms_redefinition.cons_orig_params,true,true,true,true,:v_num,true);

已順利完成 PL/SQL 程序.

http://yangtingkun.itpub.net/post/468/494878 (其中遇见问题参考)

5).同步表内容

如果同步的数据很大,则可以考虑通过prebuilt table的方法建立物化视图

Exec dbms_redefinition.sync_interim_table(‘scott’,’test’,’test_temp’)

6).结束转换过程

SQL> exec dbms_redefinition.finish_redef_table('scott','test','test_temp')

已順利完成 PL/SQL 程序.

7).删除临时表test_temp

Drop table test_temp

其中可以使用dbms_redefinition.abort_redef_table(‘scott’,’test’,’test_temp’)中途退出该过程。

Index

Local index:

l 本地索引一定是分区索引,分区键等同于表的分区键,分区数等同于表的分区数目,本地索引的分区机制和表的分区机制一样。

l 如果本地索引的的索引列伊分区键开头,则称之为前缀索引

l 如果本地索引的索引列不是以分区键开头,不包含分区键之,称之为非前缀索引

l 位图索引只能为本地分区索引

l 本地所有多应用于数据仓库环境中olap。

l 本地所有只支持分区内的唯一性,无法支持表上的唯一性,如果要使用本地索引给表做唯一性约束,则约束中必须要包括分区键列。

create index test_index on test(object_id)

local

(

partition p1 tablespace users,

partition p2 tablespace test,

partition p3 tablespace tbs

)

alter table test add primary key(object_Id) using index test_index

或者:

create index test_index1 on test(object_id) local

前缀索引,索引中包含分区键

select dbms_metadata.get_ddl('INDEX','TEST_INDEX1','SCOTT') from dual

create index test_index2 on test(object_type) local

非前缀索引,索引中不包含分区键

select dbms_metadata.get_ddl('INDEX','TEST_INDEX2','SCOTT') from dual

SQL> select index_name,table_name,partitioning_type,locality,alignment from user_part_indexes where table_name='TEST';

INDEX_NAME TABLE_NAME PARTITIONING_T LOCALITY ALIGNMENT

--------------- -------------------- -------------- ------------ ------------------------

TEST_INDEX1 TEST RANGE LOCAL PREFIXED

TEST_INDEX2 TEST RANGE LOCAL NON_PREFIXED

Global index:

l 全局索引的分区键和分区数和表的分区键和分区数可能都不相同,表和全局索引的分区机制不一样。

l 全局索引可以分区,也可以不分区索引。全局索引必须是前缀索引,即全局索引的索引列必须是一索引分区键作为前几列。

l 全局分区索引的索引条目可能指向若干个分区,因此,对于全局分区索引,即使只截断一个分区中的数据,都需要rebuild若干个分区甚至是整个索引。

l 全局索引多用于oltp系统中。

l 全局分区索引只按范围或者散列hash分区,hash分区是10g以后才支持。

l 9i后对分区表组move或者truncate时可以用update global indexes语句来同步更新全局分区索引,用消耗一定资源来换可用性。

l 全局分区索引只能是B树索引,而且只能是前缀索引。

Eg:

create table test1

(id int,

name varchar2(20),

desc1 varchar2(20)

)

partition by range(id)

(

partition p1 values less than (1000) tablespace users,

partition p2 values less than (5000) tablespace test,

partition p3 values less than (maxvalue) tablespace tbs

)

创建索引:

create index test1_global_index on test1(id) global

partition by range(id)

(partition p1 values less than (4000) tablespace users,

partition p2 values less than (maxvalue) tablespace test

)

SQL> alter table test1 drop partition p3;

已更改表格.

索引的状态发生变化

SQL> select index_name,partition_name,status from user_ind_partitions where index_name='TEST1_GLOBAL_INDEX';

INDEX_NAME PARTITION_ STATUS

-------------------- ---------- ----------------

TEST1_GLOBAL_INDEX P1 UNUSABLE

TEST1_GLOBAL_INDEX P2 UNUSABLE

必须加上前置码

SQL> l

1 create index test1_global_index1 on test1(object_type) global

2 partition by range(id)

3 (partition p1 values less than (4000),

4 paritition p2 less than (maxvalues)

5* )

SQL> /

partition by range(id)

*

ERROR 在行 2:

ORA-14038: GLOBAL 分割索引必須加上前置碼

不能和位图索引使用

SQL> l

1 create bitmap index test1_global_index1 on test1(object_type) global

2 partition by range(id)

3 (partition p1 values less than (4000),

4 paritition p2 less than (maxvalues)

5* )

SQL> /

create bitmap index test1_global_index1 on test1(object_type) global

*

ERROR 在行 1:

ORA-25113: GLOBAL 可能無法與點陣圖索引一起使用

操作索引部分

 

索引类型

range

hash/list

组合

删除索引分区

global

drop partition

local

 

 

 

重建索引分区

global

rebuild partition

local

rebuild partition

rebuild partition

rebuild subpartition

重命名索引分区

global

rename partition

local

rename partition

ename partition

rename subpartition

分割索引分区

global

split partition

local

 

 

 

SQL> alter index test1_global_index drop partition p1;

已更改索引.

SQL> alter index test1_global_index rebuild partition p2;

已更改索引.

SQL> alter index test1_global_index rename partition p2 to p1;

已更改索引.

user_part_tables 分区表信息

user_tab_partitions 分区分层信息

user_tab_subpartitions 子分区分层信息

user_part_key_columns 分区表的分区键列

user_subpart_key_columns 组合分区表的子分区键列

user_part_col_statistics 表的分区的列统计信息和直方图

user_part_col_statistics

user_part_histograms 表的分区上的直方图的直方图的直方图数据

user_subpart_histograms

user_part_indexes 分区索引的分区信息

user_ind_partitions 分区层次的分区信息,分区的存储参数

user_ind_subpartitions

[@more@]

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/24237320/viewspace-1058717/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: partition table(2)
请登录后发表评论 登录
全部评论

注册时间:2010-07-13

  • 博文量
    406
  • 访问量
    1208666