oracle分区怎么做的 oracle分区表的分区键

oracle数据库怎么建分区一般语法:
create table t_test (
pk_id number(30) not null,
add_date_timeDATE,
【oracle分区怎么做的 oracle分区表的分区键】constraintPK_T_TEST primary key (pk_id)
)
PARTITION BY RANGE (add_date_time)
(
PARTITIONt_test_2013_less VALUES LESS THAN (TO_DATE('2013-01-01 00:00:00','yyyy-mm-ddhh24:mi:ss')) TABLESPACE TS_MISPS,
PARTITIONt_test_2013 VALUES LESS THAN (TO_DATE('2014-01-01 00:00:00','yyyy-mm-ddhh24:mi:ss')) TABLESPACE TS_MISPS,
PARTITION t_test_2014VALUES LESS THAN (TO_DATE('2015-01-01 00:00:00','yyyy-mm-dd hh24:mi:ss'))TABLESPACE TS_MISPS
);
Oracle分区之四:分区维护和管理 一 分区表的相关实验 创建一个列表分区表 create table t (id number city varchar ( )) partition by list(city) ( partition p values ( SH JS ZJ ) partition p values ( BJ TJ HB ) partition p values ( GZ SZ ) partition p_others values (default) ); create or replace procedure proc as begin for i in loop execute immediate INSERT INTO T values(:x :y) USING i SH ; end loop; end; / exec proc create or replace procedure proc as begin for i in loop execute immediate INSERT INTO T values(:x :y) USING i JS ; end loop; end; / exec proc create or replace procedure proc as begin for i in loop execute immediate INSERT INTO T values(:x :y) USING i ZJ ; end loop; end; / exec proc create or replace procedure proc as begin for i in loop execute immediate INSERT INTO T values(:x :y) USING i BJ ; end loop; end; / exec proc create or replace procedure proc as begin for i in loop execute immediate INSERT INTO T values(:x :y) USING i TJ ; end loop; end; / exec proc create or replace procedure proc as begin for i in loop execute immediate INSERT INTO T values(:x :y) USING i GZ ; end loop; end; / exec proc create or replace procedure proc as begin for i in loop execute immediate INSERT INTO T values(:x :y) USING i HB ; end loop; end; / exec proc create or replace procedure proc as begin for i in loop execute immediate INSERT INTO T values(:x :y) USING i SZ ; end loop; end; / exec proc create or replace procedure proc as begin for i in loop execute immediate INSERT INTO T values(:x :y) USING i AH ; end loop; end; / exec proc SQL SET linesize SQL select TABLE_NAME TABLE_OWNER PARTITION_NAME SUBPARTITION_COUNT from dba_tab_partitions where table_name= T ; TABLE_NAME TABLE_OWNER PARTITION_NAME SUBPARTITION_COUNT T HR P T HR P T HR P T HR P_OTHERS
实验一(SPLIT 分区) alter table t split partition p values ( JS ) into (partition p _ partition p _ ); SQL select TABLE_NAME TABLE_OWNER PARTITION_NAME SUBPARTITION_COUNT from dba_tab_partitions where table_name= T ; TABLE_NAME TABLE_OWNER PARTITION_NAME SUBPARTITION_COUNT T HR P _ T HR P _ T HR P T HR P T HR P_OTHERS
实验二(merge 分区) alter table t merge partitions p _ p _ into partition p ; SQL select TABLE_NAME TABLE_OWNER PARTITION_NAME SUBPARTITION_COUNT from dba_tab_partitions where table_name= T ; TABLE_NAME TABLE_OWNER PARTITION_NAME SUBPARTITION_COUNT T HR P T HR P T HR P T HR P_OTHERS 实验三 alter table t split partition p values ( BJ TJ ) into (partition p _ partition p _ ); SQL select TABLE_NAME TABLE_OWNER PARTITION_NAME SUBPARTITION_COUNT from dba_tab_partitions where table_name= T ; TABLE_NAME TABLE_OWNER PARTITION_NAME SUBPARTITION_COUNT T HR P T HR P _ T HR P _ T HR P T HR P_OTHERS
实验四 alter table t merge partitions p _ p _ into partition p ; SQL select TABLE_NAME TABLE_OWNER PARTITION_NAME SUBPARTITION_COUNT from dba_tab_partitions where table_name= T ; TABLE_NAME TABLE_OWNER PARTITION_NAME SUBPARTITION_COUNT T HR P T HR P T HR P T HR P_OTHERS 实验五( 向分区某个分区里增加个分区列值) SQL alter table t modify partition p add values( ZQ ); Table altered 二 分区索引的相关实验 实验六(创建索引分区) create index idx_t on t (id) global partition by range(id) ( partition p values less than ( ) partition p values less than (maxvalue) ); drop index idx_ create index idx_t on t (id) global partition by hash(id) partitions ; create table tt (id number createdate date) partition by range(createdate) subpartition by hash(id) subpartitions ( partition p values less than (to_date( yyyy mm dd )) partition p values less than (to_date( yyyy mm dd )) ); create table tt (id number name varchar ( )) partition by range(name) ( partition p values less than ( h ) partition p values less than ( o ) ); create index idx_tt on tt (id) local; drop indexe idx_tt ; create index idx_tt on tt (id) global partition by range(id) ( partition p values less than ( ) partition p values less than (maxvalue) ); SQL SET LINESIZE SQL select INDEX_OWNER INDEX_NAME PARTITION_NAME FROM dba_Ind_Partitions where index_name= IDX_TT ; INDEX_OWNER INDEX_NAME PARTITION_NAME HR IDX_TT P HR IDX_TT P
alter index idx_tt split partition p at ( ) into (partition p partition p_max); SQL select INDEX_OWNER INDEX_NAME PARTITION_NAME FROM dba_Ind_Partitions where index_name= IDX_TT ; INDEX_OWNER INDEX_NAME PARTITION_NAME HR IDX_TT P HR IDX_TT P HR IDX_TT P_MAX
三 分区表交换的相关实验 Exchange partition提供了一种方式 让你在表与表或分区与分区之间迁移数据 注意不是将表转换成分区或非分区的形式 而仅只是迁移表中数据(互相迁移) 由于其号称是采用了更改数据字典的方式 因此效率最高(几乎不涉及io操作) Exchange partition适用于所有分区格式 你可以将数据从分区表迁移到非分区表 也可以从非分区表迁移至分区表 或者从hash partition到range partition诸如此类 其语法 alter table tbname exchange partition/subpartition ptname with table tbname ; 注意 在将未分区表的数据迁移到分区表中时 可能出现ora 的错误 虽然可以用without validation去解决 但是此时进入分区表的数据可能不符合分区规 则 所以without validation一定要慎用 a 涉及交换的两表之间表结构必须一致 除非附加with validation子句; b 如果是从非分区表向分区表做交换 非分区表中的数据必须符合分区表中指定分区的规则 除非附加without validation子句; c 如果从分区表向分区表做交换 被交换的分区的数据必须符合分区规则 除非附加without validation子句; d Global索引或涉及到数据改动了的global索引分区会被置为unusable 除非附加update indexes子句 注意 一旦附加了without validation子句 则表示不再验证数据有效性 因此指定该子句时务必慎重
创建一个交换分区的普通heap表 SQL create table exchange_t (id number city varchar ( )); Table created SQL select distinct city from t partition (p ); CITY TJ BJ HB 查看下P 分区有records SQL select count(*) from t partition (p ); COUNT(*) 下面是分区表和普通HEAP表交换 alter table t exchange partition p with table exchange_t including indexes without validation; 验证下数据 和上面的P 分区数据一致 SQL select count(*) from exchange_t ; COUNT(*) SQL select distinct city from exchange_t ; CITY TJ BJ HB 四 一个实际应用的例子的相关实验 创建一个分区表 只保留最近 年的财务数据 create table ware(wareyear varchar ( ) id number) partition by range (wareyear) ( partition p_ values less than( ) partition p_ values less than( ) partition p_max values less than(maxvalue) ); 创建索引 create index idx_ware_id on ware(id) global partition by range(id) ( partition p_id_ values less than( ) partition p_id_max values less than(maxvalue) ); create index idx_ware_wareyear on ware(wareyear) local; 插入测试数据 insert into ware select object_id from dba_objects; insert into ware select object_id from dba_objects; mit; 年终 归档最早的数据 并加入新财年的数据 create table ware_ (wareyear varchar ( ) id number); create index idx_ware_ on ware_ (wareyear); insert into ware_ select object_id from dba_objects; mit; alter table ware split partition p_max at ( ) into (partition p_ partition p_max); 将p_ 分区放入ware_ 表里 alter table ware exchange partition p_ with table ware_ including indexes without validation; create table ware_ (wareyear varchar ( ) id number); create index idx_ware_ on ware_ (wareyear); alter table ware exchange partition p_ with table ware_ including indexes without validation; 删除p_ 分区 alter table ware drop partition p_ ; 导出做归档 [oracle@even admin]$ exp hr/hr@test file=/home/oracle/ware_ dmp tables=ware_ press=n Export: Release Production on Fri Jan : : Copyright (c) Oracle All rights reserved
Connected to: Oracle Database g Enterprise Edition Release Production With the Partitioning Oracle Label Security OLAP and Data Mining options Export done in US ASCII character set and AL UTF NCHAR character set server uses AL UTF character set (possible charset conversion) About to export specified tables via Conventional Path exporting table WARE_ rows exported Export terminated successfully without warnings 然后删除表 drop table ware_ ;
五 表和索引的维护的常见SQL语句及注意事项 对于分区索引 不能整体进行重建 只能对单个分区进行重建(也就是物理存在的分区) 语法如下 Alter index idx_name rebuild partition index_partition_name [online nologging] Alter Index IndexName Rebuild Partition P_Name; 有子分区的本地索引 不能重建某分区 只能对每个子分区进行重建 Alter Index Index_Name Rebuild subPartition P_Sub_Name; 脚本 重建所有unUsable的索引 Select alter index || Index_Name || rebuild; From User_Indexes Where Status = UNUSABLE union Select alter index || Index_Name || rebuild Partition ||Partition_Name || ; From User_Ind_Partitions Where Status = UNUSABLE union Select alter index || Index_Name || rebuild subPartition ||subPartition_Name || ; From User_Ind_subPartitions Where Status = UNUSABLE ;
add parttion Alter Table TestTab Add Partition P Values Less Than ( ); 如果有子分区 且定义了子分区模板 所有的子分区会自动添加 新加分区后 该区没有统计信息 全是空 如果表级不是global_satus 则表级的统计信息也会空 新加分区后 如果表级统计是global_satus 还会出现out of range的问题(CBO估算的选择率很低) 解决 问题的方法是 copy_table_stats exec dbms_stats copy_table_stats(user tabname = TEST_TAB srcpartname = P_ dstpartname = P_ );
tuncate and drop partition truncate和drop可对有子分区的分区进行 ALTER TABLE TEST truncate Partition P_ ; ALTER TABLE TEST Drop Partition P_ ; 它们会导致globl index的某些分区不可用 必须这样做 ALTER TABLE TEST truncate Partition P_ update indexes; ALTER TABLE TEST truncate Partition P_ update global indexes; ALTER TABLE TEST Drop Partition P_ update indexes; ALTER TABLE TEST Drop Partition P_ update global indexes;
move partition 有子分区的分区不能move 只能move每个子分区(也就是物理分区) Alter Table TEST Move Partition P_ ; 由于rowid变了 会导致所有相关索引unusable 必须这样做 Alter Table TEST Move subPartition P_ _P update indexes; Alter Table TEST Move subPartition P_ _P update global indexes; Local Index没有更新 split partion 语法 alter table table_name split partition partition_name at (value) into (partition partition_name partition partition_name) [update [global] indexes]; 可以对有子分区的分区进行 自动split子分区 由于rowid变了 新分区和global index都变为unusable alter table t merge partitions p _ p _ into partition p ; 合并range分区 ALTER TABLE Test_Tab Merge Partitions P_ P_ Into Partition P_ [Update [global] Indexes]; 该分区有子分区 有子分区 也可以单独合并子分区merge subpartition
lishixinzhi/Article/program/Oracle/201311/19037
Oracle分区表怎么建唯1、一般分区表都会很大,所以可以先创建表空间,为了让分区表存放到单独的表空间 , 否则默认会存放到USERS表空间
2、创建TABLESPACE TS1:
CREATE TABLESPACE TS1 DATAFILE '/data1/oracle/test.dbf' SIZE 512M AUTOEXTEND ON NEXT 512M MAXSIZE UNLIMITED;
3、创建分区表 , 需要确定按什么分区,比如按id或按时间段:
CREATE TABLE test_201602
(
ID NUMBER(10) NOT NULL,
CREATE_TIME DATE,
)
TABLESPACE TS1
PARTITION BY LIST (ID)
(PARTITION PT_1001 VALUES (1001) TABLESPACE TS1);
4、继续增加分区
ALTER TABLE test_201602 ADD PARTITION "PT_1003" VALUES (1003) LOGGING NOCOMPRESS;
ALTER TABLE test_201602 ADD PARTITION "PT_1004" VALUES (1004) LOGGING NOCOMPRESS;
5、查询表及分区数量
select TABLE_NAME,PARTITION_COUNT,DEF_TABLESPACE_NAME from USER_PART_TABLES order by DEF_TABLESPACE_NAME;
Oracle创建分区表操作1、创建语句
create table p(id number)
partition by range(id)
(partition p1 values less than(100) tablespace t1,
partition p2 values less than(200) tablespace t2,
partition p3 values less than(300) tablespace t3);
2、添加分区
alter table p add partition p4 values less than (400) tablespace t4;
3、清除分区数据
alter table p trunc partition p1;
4、删除分区
alter table p drop partition p1;
oracle分区怎么做的的介绍就聊到这里吧,感谢你花时间阅读本站内容 , 更多关于oracle分区表的分区键、oracle分区怎么做的的信息别忘了在本站进行查找喔 。

    推荐阅读