ORACLE按时间创建分区表有些项⽬中可能会涉及到表的分区(有的表⼤⼩在70G左右) 下⾯简单写⼀下创建分区表过程
1、创建测试表
⾸先创建测试表weihai_test语句如下
create table weihai_test (
id          int          notnull,
join_date            DATE);
以上表中join_date字段为分区表字段
2、插⼊数据
2.1、模拟插⼊30万条数据
plsql/developer ⼯具执⾏
declare
i  int := 1;
year VARCHAR2(20);
begin
loop
year :=CASE mod(i, 3)
WHEN 0 THEN
'2015-12-01 00:00:00'
WHEN 1 THEN
'2016-12-01 00:00:00'
ELSE
'2017-12-01 00:00:00'
END;
insert into weihai_test values(i, to_date(year, 'yyyy-mm-dd hh24:mi:ss'));
exit when i= 300000;
i := i+ 1;
end loop;
end;
commit;
2.2、查看是否插⼊成功
select count(1) from weihai_test;
3、重命名原表,⽣成临时表
数据插⼊完成后,重命名原表,这⾥演⽰的是停机之后的操作,如果是在线操作,建议使⽤oracle 在线重定义功能来保障数据不丢失rename weihai_test to weihai_test_his;  (这个过程只建议应⽤停机的时候做)
4、创建分区表
create table weihai_test (
id          int          notnull,
join_date            DATE )
partition by range(join_date)
(
partition weihai_test_2016_less values less than (to_date('2016-01-01 00:00:00','yyyy-mm-dd hh24:mi:ss')) tablespace fmt, partition weihai_test_2016 values less than (to_date('2017-01-01 00:00:00','yyyy-mm-dd hh24:mi:ss')) tablespace fmt, partition weihai_test_2017 values less than (to_date('2018-01-01 00:00:00','yyyy-mm-dd hh24:mi:ss')) tablespace fmt, partition weihai_test_max values less than (to_date('2999-12-31 23:59:59','yyyy-mm-ddhh24:mi:ss')) tablespace fmt
);
5、数据转移
表创建完成之后,开始导数,
insert /*+append*/ into weihai_test (
id,
join_date) select
id,
join_date from weihai_test_his;
commit;
6、数据对⽐
plsql developer导入数据
导⼊完成之后,对⽐两张表的数据,⼀样就表⽰导⼊成功
select count(1) from weihai_test_his;
select count(1) from weihai_test;
7、查看执⾏计划
查询数据,查看执⾏计划,与临时表weihai_test_his相⽐较,是否扫描的更少
explain plan for select * from weihai_test_his where join_date <= date'2016-01-01'; select plan_table_output from table(dbms_xplan.display());
同样的查询在分区表执⾏⼀遍
explain plan for select * from weihai_test where join_date <= date'2016-01-01'; select plan_table_output from table(dbms_xplan.display());
相⽐之下,分区表耗费的资源更少
8、删除临时表
数据导⼊完成之后,drop临时表
drop table weihai_test_his;