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;