Oracle 分区详解

Oracle表分区的步骤:创建表并声明分区、查看创建分区结果、恢复分区索引、

创建分区

这是创建一个简单的根据日期(天)自增的一个范围分区

CREATE TABLE my_table (
  id NUMBER,
  name VARCHAR2(50),
  created_date DATE
)
PARTITION BY RANGE (created_date) INTERVAL (NUMTODSINTERVAL(1, 'DAY'))
(
  PARTITION p1 VALUES LESS THAN (TO_DATE('2022-01-01', 'YYYY-MM-DD'))
);

-- 查看分区结果
select * from user_tab_partitions where table_name = '[表明]'

-- 恢复数据(恢复数据之前不要建立索引否则会很慢)
insert into partitions_test_temp select * from position_test

-- 创建索引
create index······

常用分区

按照时间范围分区

-- 按照年份分区(每一年自动新增分区)
create table partitions_test
(
id NUMBER(20) not null primary key,
name VARCHAR2(20) not null,
joindate DATE not null
) tablespace test
partition by range(joindate) interval (numtoyminterval(1,'year'))
(partition p_before_2021 values less than (to_date('2022-01-01','YYYY-MM-DD'))
);
-- 按照月份分区(每个月自动新增分区)
create table partitions_test
(
id NUMBER(20) not null primary key,
name VARCHAR2(20) not null,
joindate DATE not null
) tablespace test
partition by range(joindate) interval (numtoyminterval(1,'month'))
(partition p_before_2021 values less than (to_date('2022-01-01','YYYY-MM-DD'))
);
-- 按照天分区(每天都会自动创建新的分区)
create table partitions_test
(
id NUMBER(20) not null primary key,
name VARCHAR2(20) not null,
joindate DATE not null
) tablespace test
partition by range(joindate) interval (numtodsinterval(1,'day'))
(partition p_before_2021 values less than (to_date('2022-01-01','YYYY-MM-DD'))
);

按照数字范围分区

create table partitions_test
(
  id NUMBER(20) not null primary key,
  name VARCHAR2(20) not null,
  joindate DATE not null
) partition by range(id)(
  partition p1 values less than(10000) tablespace space1,
  partition p2 values less than(20000) tablespace space2,
  partition p3 values less than(maxvalue) tablespace space3
) enable row movement;

列表分区 list

-- 创建根据字段值进行的符合分区(列表分区+列表子分区)
-- tablespace指定该分区数据存放的表空间
create table LAW_T_FULLTEXTSEARCH(
     id         varchar2(64),
     tablecode    varchar2(64),
     columncode varchar2(200),
     columndata varchar2(4000),

) partition by list(tablecode)
(
    partition P_NAME1 values ('tablecode_value1') tablespace testTableSpace1,
    partition P_NAME2 values ('tablecode_value2') tablespace testTableSpace1,
    partition p3 values (default) tablespace testTableSpace1
) enable row movement;

复合分区

-- 创建根据字段值进行的符合分区(列表分区+列表子分区)
create table LAW_T_FULLTEXTSEARCH(
     id         varchar2(64),
     tablecode    varchar2(64),
     columncode varchar2(200),
     columndata varchar2(4000),

) partition by list(tablecode) subpartition by list (columncode)
(
    partition P_NAME1 values ('tablecode_value1')
        (
        subpartition SUBP_NAME1_1 values ('columncode_value1'),
        subpartition SUBP_NAME1_2 values ('columncode_value2')
        ),
    partition P_NAME2 values ('tablecode_value2')
        (
        subpartition SUBP_NAME2_1 values ('columncode_value3')
        )
) enable row movement;

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值