千家信息网

oracle分区表可以分为几类

发表于:2024-11-26 作者:千家信息网编辑
千家信息网最后更新 2024年11月26日,这篇文章主要讲解了"oracle分区表可以分为几类",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"oracle分区表可以分为几类"吧!Oracle分区表
千家信息网最后更新 2024年11月26日oracle分区表可以分为几类

这篇文章主要讲解了"oracle分区表可以分为几类",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"oracle分区表可以分为几类"吧!

Oracle分区表分为四类:范围分区表;列表分区表;哈希分区表;组合分区表

1.列表分区表(list)

CREATE TABLE list_example(

dname VARCHAR2(10),

DATA VARCHAR2(20)

)

PARTITION BY LIST(dname)

(

PARTITION part01 VALUES('初始登记','转移登记'),

PARTITION part02 VALUES('更名登记','楼盘变更'),

PARTITION part03 VALUES('抵押登记'),

PARTITION part04 VALUES('限制登记')

);

2.范围分区

CREATE TABLE example

(

CUSTOMER_ID NUMBER NOT NULL PRIMARY KEY,

FIRST_NAME VARCHAR2(30) NOT NULL,

LAST_NAME VARCHAR2(30) NOT NULL,

PHONE VARCHAR2(15) NOT NULL,

EMAIL VARCHAR2(80),

STATUS CHAR(1)

)

PARTITION BY RANGE (CUSTOMER_ID)

(

PARTITION exam1 VALUES LESS THAN (100000) TABLESPACE arsystem,

PARTITION exam2 VALUES LESS THAN (200000) TABLESPACE arsystem,

PARTITION exam3 VALUES LESS THAN (300000) TABLESPACE arsystem

);

时间分区

CREATE TABLE part_date

(

ORDER_ID NUMBER(7) NOT NULL,

ORDER_DATE DATE,

TOTAL_AMOUNT NUMBER,

CUSTOTMER_ID NUMBER(7),

PAID CHAR(1)

)

PARTITION BY RANGE (ORDER_DATE)

(

PARTITION ORD_ACT_PART01 VALUES LESS THAN (TO_DATE('2015-07-01 00:00:00','yyyy-mm-dd hh34:mi:ss')) TABLESPACE arsystem,

PARTITION ORD_ACT_PART02 VALUES LESS THAN (TO_DATE('2015-08-01 00:00:00','yyyy-mm-dd hh34:mi:ss')) TABLESPACE arsystem,

PARTITION ORD_ACT_PART03 VALUES LESS THAN (TO_DATE('2015-09-01 00:00:00','yyyy-mm-dd hh34:mi:ss')) TABLESPACE arsystem,

PARTITION ORD_ACT_PART04 VALUES LESS THAN (MAXVALUE)TABLESPACE arsystem

);

哈希分区

create table HASH_PART

(

transaction_id number primary key,

item_id number(8) not null

)

partition by hash(transaction_id)

(

partition part_01 tablespace ARSYSTEM,

partition part_02 tablespace ARSYSTEM,

partition part_03 tablespace ARSYSTEM

);

简写

CREATE TABLE emp_hash

(

empno NUMBER (4),

ename VARCHAR2 (30),

sal NUMBER

)

PARTITION BY HASH (empno) PARTITIONS 8

STORE IN (arsystem1,arsystem2);

hash分区最主要的机制是根据hash算法来计算具体某条纪录应该插入到哪个分区中,hash算法中最重要的是hash函数,Oracle中如果你要使用hash分区,只需指定分区的数量即可。建议分区的数量采用2的n次方,这样可以使得各个分区间数据分布更加均匀。

组合分区

范围-散列分区

CREATE TABLE SALES

(

PRODUCT_ID VARCHAR2(5),

SALES_DATE DATE,

SALES_COST NUMBER(10),

STATUS VARCHAR2(20)

)

PARTITION BY RANGE(SALES_DATE) SUBPARTITION BY LIST (STATUS)

(

PARTITION P1 VALUES LESS THAN(TO_DATE('2003-01-01','YYYY-MM-DD'))TABLESPACE ARSYSTEM

(

SUBPARTITION P1SUB1 VALUES ('ACTIVE') TABLESPACE ARSYSTEM,

SUBPARTITION P1SUB2 VALUES ('INACTIVE') TABLESPACE ARSYSTEM

),

PARTITION P2 VALUES LESS THAN (TO_DATE('2003-03-01','YYYY-MM-DD')) TABLESPACE ARSYSTEM

(

SUBPARTITION P2SUB1 VALUES ('ACTIVE') TABLESPACE ARSYSTEM,

SUBPARTITION P2SUB2 VALUES ('INACTIVE') TABLESPACE ARSYSTEM

)

);

范围-哈希分区

create table dinya_test

(

transaction_id number primary key,

item_id number(8) not null,

item_description varchar2(300),

transaction_date date

)

partition by range(transaction_date)subpartition by hash(transaction_id) subpartitions 3 store in (arsystem,users)

(

partition part_01 values less than(TO_DATE('2015-07-01 00:00:00','yyyy-mm-dd hh34:mi:ss')),

partition part_02 values less than(TO_DATE('2015-08-01 00:00:00','yyyy-mm-dd hh34:mi:ss')),

partition part_03 values less than(maxvalue)

);

在分区表上可以建立三种类型的索引:1和普通表一样的全局索引;2.全局分区索引;3.本地分区索引

1.建立普通的索引

create index com_index_range_example_id on range_example(id);

2.建立本地分区索引

create index local_index_range_example_id on range_example(id) local;

3.建立全局分区索引

create index gidx_range_exampel_id on range_example(id)

GLOBAL partition by range(id)

(

part_01 values less than(1000),

part_02 values less than(MAXVALUE)

);

对于分区索引的删除,local index 不能指定分区名称,单独的删除分区索引。

local index 对应的分区会伴随着data分区的删除而一起被删除。global partition index 可以指定分区名称,删除某一分区。但是有一点要注意,如果该分区不为空,则会导致更高一级的索引分区被置为UNUSABLE 。

ALTER INDEX gidx_range_exampel_id drop partition part_01 ; 此句将导致part_02 状态为UNUSABLE

分区表的操作

1.查询

select * from part_date partition(ORD_ACT_PART01);

2.删除某个分区

alter table part_date drop partition ORD_ACT_PART04;

alter table part_date truncate partition ORD_ACT_PART04;

3.添加分区

ALTER TABLE part_date ADD PARTITION ORD_ACT_PART04 VALUES LESS THAN(TO_DATE('2015-10-01','YYYY-MM-DD'));

4.创建索引

create index idx_part_date on part_date(order_id) local;

-----------------------在线重定义过程-----------------------

包中的定义,可以用pl/sql工具看下包dbms_redefinition的用法

-- Constants for the options_flag parameter of start_redef_table

cons_use_pk CONSTANT PLS_INTEGER := 1;---主键重定义

cons_use_rowid CONSTANT PLS_INTEGER := 2;---rowid重定义

增快处理速度,可添加并行

alter session force parallel dml parallel 4;

alter session force parallel query parallel 4;

建立普通表

CREATE TABLE putong_table

(

CUSTOMER_ID NUMBER NOT NULL PRIMARY KEY,

FIRST_NAME VARCHAR2(30) NOT NULL,

LAST_NAME VARCHAR2(30) NOT NULL,

PHONE VARCHAR2(15) NOT NULL,

EMAIL VARCHAR2(80),

STATUS CHAR(1)

);

创建分区表

CREATE TABLE part_table

(

CUSTOMER_ID NUMBER NOT NULL PRIMARY KEY,

FIRST_NAME VARCHAR2(30) NOT NULL,

LAST_NAME VARCHAR2(30) NOT NULL,

PHONE VARCHAR2(15) NOT NULL,

EMAIL VARCHAR2(80),

STATUS CHAR(1)

)

PARTITION BY RANGE (CUSTOMER_ID)

(

PARTITION exam1 VALUES LESS THAN (100000) TABLESPACE arsystem,

PARTITION exam2 VALUES LESS THAN (200000) TABLESPACE arsystem,

PARTITION exam3 VALUES LESS THAN (300000) TABLESPACE arsystem

);

插入测试数据

insert into putong_table values(1,'43t','f4y','t54','th','3');

insert into putong_table values(2,'43t','f4y','t54','th','2');

insert into putong_table values(3,'43t','f4y','t54','th','1');

在线重定义过程

测试该表是否可分区

exec dbms_redefinition.can_redef_table('ARADMIN', 'putong_table');

开始

EXEC DBMS_REDEFINITION.START_REDEF_TABLE('ARADMIN', 'putong_table', 'part_table');

--插入测试数据(可选)

insert into putong_table values(4,'43t','f4y','t54','th','4');

同步数据

EXEC dbms_redefinition.sync_interim_table('ARADMIN', 'putong_table', 'part_table');

完成

EXEC DBMS_REDEFINITION.FINISH_REDEF_TABLE('ARADMIN', 'putong_table', 'part_table');

将分区表重命名

alter table part_table rename to part_table_new;

1.无主键表

普通表

CREATE TABLE date_putong

(

ORDER_ID NUMBER(7) NOT NULL,

ORDER_DATE DATE,

TOTAL_AMOUNT NUMBER,

CUSTOTMER_ID NUMBER(7),

PAID CHAR(1)

);

分区表

CREATE TABLE date_partition

(

ORDER_ID NUMBER(7) NOT NULL,

ORDER_DATE DATE,

TOTAL_AMOUNT NUMBER,

CUSTOTMER_ID NUMBER(7),

PAID CHAR(1)

)

PARTITION BY RANGE (ORDER_DATE)

(

PARTITION ORD_ACT_PART01 VALUES LESS THAN (TO_DATE('2015-07-01 00:00:00','yyyy-mm-dd hh34:mi:ss')) TABLESPACE arsystem,

PARTITION ORD_ACT_PART02 VALUES LESS THAN (TO_DATE('2015-08-01 00:00:00','yyyy-mm-dd hh34:mi:ss')) TABLESPACE arsystem,

PARTITION ORD_ACT_PART03 VALUES LESS THAN (TO_DATE('2015-09-01 00:00:00','yyyy-mm-dd hh34:mi:ss')) TABLESPACE arsystem,

PARTITION ORD_ACT_PART04 VALUES LESS THAN (MAXVALUE)TABLESPACE arsystem

);

插入数据

insert into date_putong values(1,TO_DATE('2015-08-24','yyyy-mm-dd'),1,2,3);

insert into date_putong values(1,TO_DATE('2015-08-24','yyyy-mm-dd'),1,2,3);

insert into date_putong values(1,TO_DATE('2015-08-24','yyyy-mm-dd'),1,2,3);

重定义过程

exec dbms_redefinition.can_redef_table('ARADMIN', 'date_putong',2);

EXEC DBMS_REDEFINITION.START_REDEF_TABLE('ARADMIN', 'date_putong', 'date_partition',null,2);

EXEC dbms_redefinition.sync_interim_table('ARADMIN', 'date_putong', 'date_partition');

EXEC DBMS_REDEFINITION.FINISH_REDEF_TABLE('ARADMIN', 'date_putong', 'date_partition');

--------------异常处理--------------

异常情况下终止操作

BEGIN

DBMS_REDEFINITION.ABORT_REDEF_TABLE(uname => 'ARADMIN',

orig_table => 'date_putong',

int_table => 'date_partition'

);

END;

DBMS_REDEFINITION.ABORT_REDEF_TABLE('ARADMIN', 'date_putong', 'date_partition');

ORA-12089: 不能联机重新定义无主键的表 "ARADMIN"."DATE_PUTONG"

ORA-06512: 在 "SYS.DBMS_REDEFINITION", line 56

ORA-06512: 在 "SYS.DBMS_REDEFINITION", line 1498

ORA-06512: 在 line 2

alter table PUTONG_TABLE add primary key (CUSTOMER_ID)

ORA-12091

删除物化视图日志

drop materialized view log on BS_T_WF_DEALPROCESS_BAK;

查看分区时的错误

select * from DBA_REDEFINITION_ERRORS;

虚拟列分区举例

要求按月进行分区,并且这12个分区可以循环使用,只保留2个月的数据,所以使用11g的虚拟列进行分区

create table MACHINE_TEMP_part

(

id NUMBER,

username VARCHAR2(30),

tcp VARCHAR2(30),

clientip VARCHAR2(30),

logintime VARCHAR2(30),

logouttime VARCHAR2(30),

serverip VARCHAR2(30),

insertdate DATE,

part_numas (to_nubmer(to_char(insertdate,'mm')))

)

tablespace UNIONMON

artition by range(partition_num)

(partition p1 values less than(2),

partition p2 values less than(3),

partition p3 values less than(4),

partition p4 values less than(5),

partition p5 values less than(6),

partition p6 values less than(7),

partition p7 values less than(8),

partition p8 values less than(9),

partition p9 values less than(10),

partition p10 values less than(11),

partition p11 values less than(12),

partition p12 values less than(13)

);

感谢各位的阅读,以上就是"oracle分区表可以分为几类"的内容了,经过本文的学习后,相信大家对oracle分区表可以分为几类这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!

0