关于oracle 11g自动分区+分区改名+定时任务综合使用实现自动分区后可以进行分区查询
一直想用oracle 11g 的interval分区(间断分区),这需要配合分区改名才能让我们可以使用分区查询,因为自动分区出来的分区名会是SYSPxxx这样。。
下面直接代码
drop tablespace bank_data including contents and datafiles;create tablespace bank_data
logging
datafile 'D:\bank.dbf'
size 512m
autoextend on
next 512m maxsize 2048m
extent management local; drop sequence seq_topic;alter table topic drop constraint ck_topic_classify;
alter table bank drop constraint ck_bank_subject;
alter table bank drop constraint ck_bank_status;drop table topic purge;
drop table bank purge;create table bank(id varchar2(17) primary key,district varchar2(30) not null,subject number(1) default 1 not null constraint ck_bank_subject check(subject in(1,4)),status number(1) default 0 not null constraint ck_bank_status check(status in(0,1)),remark varchar2(300)
);--1道路交通安全法律、法规和规章 2交通信号 3安全行车、文明驾驶基础知识 4机动车驾驶操作相关基础知识
--1违法行为综合判断与案例分析 2安全行车常识 3常见交通标志、标线和交通手势辨识 4驾驶职业道德和文明驾驶常识
--5恶劣气候和复杂道路条件下驾驶常识 6紧急情况下避险常识 7交通事故救护及常见危化品处理常识
create table topic(tid number(6) primary key,version varchar2(17) not null,pubdate date not null,classify number(2) default 11 not null constraint ck_topic_classify check(classify in(11,12,13,14,41,42,43,44,45,46,47)),title varchar2(1500) not null,answer varchar2(20) not null,explain varchar2(1200),image varchar2(30)
)
partition by range(pubdate)
interval (NUMTOYMINTERVAL (3, 'MONTH')) store in (bank_data)
subpartition by list(classify)
subpartition template(subpartition s1 values (11,12,13,14),subpartition s4 values (default)
)
(partition p_201604 values less than (TO_DATE ('04/01/2016', 'MM/DD/YYYY')) tablespace bank_data);alter table topic add constraint fk_topic_bank_version foreign key(version) references bank(id);create sequence seq_topic start with 100000 minvalue 100000;--修改分区名存储过程
create or replace procedure pro_partition
asv_sql varchar(400);v_table_name user_tab_partitions.table_name%type;v_partition_name user_tab_partitions.partition_name%type;v_high_value varchar(200);v_tmp_partition_name user_tab_partitions.partition_name%type;cursor cur1 isselecttable_name ,partition_name ,high_value from user_tab_partitions where partition_name like 'SYS%' and table_name='TOPIC';
beginopen cur1;loopfetch cur1 into v_table_name,v_partition_name,v_high_value;exit when cur1%notfound;v_tmp_partition_name := substr(v_high_value,11,10);v_tmp_partition_name := to_char( to_date(v_tmp_partition_name,'yyyy-mm-dd') , 'yyyymm');v_sql := 'alter table '||v_table_name||' rename partition '||v_partition_name||' to P_'||v_tmp_partition_name;execute immediate v_sql;end loop;close cur1;
end;
/
--修改子分区名存储过程
--14数字是从select high_value_length from user_tab_subpartitions where subpartition_name='P_201604_S1' and table_name='TOPIC'得来;
create or replace procedure pro_subpartition
asv_sql varchar(400);v_table_name user_tab_subpartitions.table_name%type;v_partition_name user_tab_subpartitions.partition_name%type;v_subpartition_name user_tab_subpartitions.subpartition_name%type;v_tmp_subpartition_name user_tab_subpartitions.subpartition_name%type;v_temp number;cursor cur2 isselecttable_name ,partition_name ,subpartition_name from user_tab_subpartitions where subpartition_name like 'SYS%' and table_name='TOPIC';
beginopen cur2;loopfetch cur2 into v_table_name,v_partition_name,v_subpartition_name;exit when cur2%notfound;select high_value_length into v_temp from user_tab_subpartitions where subpartition_name=v_subpartition_name and table_name='TOPIC';if (v_temp=14) then v_tmp_subpartition_name := v_partition_name||'_S1';elsev_tmp_subpartition_name := v_partition_name||'_S4';end if;v_sql := 'alter table '||v_table_name||' rename subpartition '||v_subpartition_name||' to '||v_tmp_subpartition_name;execute immediate v_sql;end loop;close cur2;
end;
/
--自动分区+改名 分区名格式为P_201604_S1
create or replace procedure pro_job
as
begin
insert into bank values('1','a',1,1,'a');
insert into topic values(1,'1',add_months(sysdate,3),11,'a','a','a','a');
insert into topic values(2,'1',add_months(sysdate,3),41,'a','a','a','a');
pro_partition;
pro_subpartition;
delete from topic where tid in(1,2);
delete from bank where id='1';
end;
/
--删除数据任务
create or replace procedure pro_del
as
v_sql varchar(300);
v_temp varchar(30);
v_num number;
v_date date;
v_count number;
v_partition_name user_tab_partitions.partition_name%type;
begin
select add_months(sysdate,-12) into v_date from dual;
v_num:=to_number(to_char(sysdate,'mm'));
if (v_num<4) then
v_temp:='04';
elsif (v_num<7) then
v_temp:='07';
elsif (v_num<10) then
v_temp:='10';
else
v_temp:='01';
end if;
v_partition_name:='P_'||to_char(v_date,'yyyy')||v_temp;
dbms_output.put_line('partition_name====>'||v_partition_name );
select count(1) into v_count from user_tab_partitions where partition_name=v_partition_name and table_name='TOPIC';
if (v_count=1) then
v_sql:='alter table topic drop partition '||v_partition_name;
execute immediate v_sql;
end if;
end;
/--定义一个job每个季度第一天凌晨执行自动建表分区
create or replace procedure pro_auto_job
as
v_job number;
begin
execute immediate 'alter table topic deallocate UNUSED KEEP 0';
dbms_job.submit(v_job,'pro_job;',sysdate,'TRUNC(ADD_MONTHS(SYSDATE,3),''q'')');
dbms_job.run(v_job);
end;
/exec pro_auto_job;create or replace procedure pro_auto_del
as
job number;
begin
execute immediate 'alter table topic deallocate UNUSED KEEP 0';
dbms_job.submit(job,'pro_del;',sysdate,'TRUNC(ADD_MONTHS(SYSDATE,12),''q'')');
dbms_job.run(job);
end;
/exec pro_auto_del;--topic按类型进行分页
create or replace procedure pro_page_topic(v_classify number,v_pageIndex number,v_pageSize number,v_total OUT number,v_cursor OUT sys_refcursor)
as
v_version number(30);
v_partition varchar2(100);
v_year number(20);
v_month number(20);
v_sql varchar2(400);
begin
select max(to_number(id)) into v_version from bank where district='湖南' and subject=to_number(substr(v_classify,0,1)) and status=1 ;
if (v_version is not null) then v_month:=to_number(substr(v_version,5,2));v_year:=to_number(substr(v_version,1,4));if (v_month<4) thenv_partition:='P_'||substr(v_version,1,4)||'04_S'||substr(v_classify,0,1);elsif (v_month<7) thenv_partition:='P_'||substr(v_version,1,4)||'07_S'||substr(v_classify,0,1);elsif (v_month<10) thenv_partition:='P_'||substr(v_version,1,4)||'10_S'||substr(v_classify,0,1);elsev_partition:='P_'||to_char(v_year+1)||'01_S'||substr(v_classify,0,1);end if;v_sql:='select count(1) from topic subpartition ('||v_partition||') where version='||to_char(v_version)||' and classify='||v_classify;execute immediate v_sql into v_total;v_sql:='select * from ( select a.*,rownum rn from ( select title,answer,explain,image from topic subpartition ('||v_partition||')where version='||to_char(v_version)||' and classify='||v_classify||' ) a where rownum <=('||v_pageIndex||'+1)*'||v_pageSize||' ) where rn>'||v_pageIndex||'*'||v_pageSize;open v_cursor for v_sql;
end if;
end;
/--随机从topic取100个数据
create or replace procedure pro_exam(v_classify number,v_cursor OUT sys_refcursor)
as
v_version number(30);
v_partition varchar2(100);
v_year number(20);
v_month number(20);
v_sql varchar2(400);
begin
select max(to_number(id)) into v_version from bank where district='湖南' and subject=to_number(substr(v_classify,0,1)) and status=1 ;
if (v_version is not null) then v_month:=to_number(substr(v_version,5,2));v_year:=to_number(substr(v_version,1,4));if (v_month<4) thenv_partition:='P_'||substr(v_version,1,4)||'04_S'||substr(v_classify,0,1);elsif (v_month<7) thenv_partition:='P_'||substr(v_version,1,4)||'07_S'||substr(v_classify,0,1);elsif (v_month<10) thenv_partition:='P_'||substr(v_version,1,4)||'10_S'||substr(v_classify,0,1);elsev_partition:='P_'||to_char(v_year+1)||'01_S'||substr(v_classify,0,1);end if;v_sql:='select * from (select title,answer,explain,image from topic subpartition('||v_partition||') where version='||to_char(v_version)||' order by dbms_random.value) where rownum<101';open v_cursor for v_sql;
end if;
end;
/commit;
select partition_name,subpartition_name
from user_tab_subpartitions
where table_name='TOPIC';
总结一下:修改分区依据是high_value,存储过程的分区查询和分页查询都是动态查询,需要先写sql字符串,然后再执行sql字符串,写到这里大家会发现其实压根就不需要用interval分区,直接用存储过程新建分区会更简洁些,不过既然有interval分区,在这就用一下。对于初学者来说,看pl/sql语言会有点畏惧,我刚接触时也是一样,会觉得它很难看懂,其实不是,如果有上面这种情况,可以分开几天来看pl/sql代码,这样连续看同样脚本两三天就发现自己可以接受pl/sql了,以上纯个人建议。。。
关于oracle 11g自动分区+分区改名+定时任务综合使用实现自动分区后可以进行分区查询相关推荐
- oracle创建时分秒列,oracle 11g 分区表创建(年月日周时分秒)
oracle 11g 支持自动分区,不过得在创建表时就设置好分区. 如果已经存在的表需要改分区表,就需要将当前表 rename后,再创建新表,然后复制数据到新表,然后删除旧表就可以了. 一.为什么要分 ...
- 关于oracle 11G 分区表新特性Interval
oracle 11G新引入的新特性Interval,可以自动根据入库需求创建分区,而不需人工干预. ------------------------月------------------------- ...
- CentOS 5.10安装Oracle 11G R2
操作系统:CentOS 5.10 64位 数据库:Oracle 11G R2(版本号为11.02) 安装操作系统的时候,注意交换分区的大小不得低于2G 1.同步时钟及修改/etc/hosts yum ...
- oracle 11g 服务端下载地址及安装说明
1.oracle 11g官网(各个版本及服务端.客户端) http://www.oracle.com/technetwork/cn/database/enterprise-edition/downlo ...
- Oracle 11g版本下载及安装超详细教程图解
目录 一.11g版本下载 1.oracle 11g官网(各个版本及服务端.客户端) 2.oracle 11g 迅雷下载地址(两个文件,下载完成后解压到一起,安装即可) 二.Oracle安装步骤图解 1 ...
- 在Windows 7下安装Oracle 11g的解决方法
在Windows 7下安装Oracle 11g的解决方法 前不久卸载掉了自己的Windows Vista系统,装上了Windows7 旗舰版,在装机过程中也遇到了很多问题,有些问题是自己不曾遇到过的, ...
- 43、Windows 7下安装Oracle 11g相关问题解决方法
很多DBA目前还停留在Oracle 9i或者10g,究其原因有可能是Oracle 11g的价格问题.本文将为大家讲解在Windows 7下安装Oracle 11g,也希望大家能从中看到11g的一些新特 ...
- mac os苹果操作系统安装docker和oracle 11g
文章目录 mac os黑苹果操作系统安装docker和oracle 11g 第一步,安装brew工具 第二步,安装docker 概述 第一种安装方式,使用 Homebrew 来安装 Docker 第二 ...
- oracle 增长型分区,oracle 11g 分区表创建(自动按年分区)
标签: 前言:工作中有一张表一年会增长100多万的数据,量虽然不大,可是表字段多,所以一年下来也会达到 1G,而且只增不改,故考虑使用分区表来提高查询性能,提高维护性. oracle 11g 支持自动 ...
最新文章
- Mac OS X 下查看和设置JAVA_HOME
- mybatis3+struts2+oracle整合的一个好的demo
- gorm软删除_gorm踩坑:软删除与某个字段的唯一性
- python培训班 费用-参加线上python培训班要多少钱?
- Aggregate functions cannot be used in the select right after the flatAggregate
- webService上传图片
- 使用Struts接受Ajax请求并且返回Json数据
- SSM框架项目的pom导入包和xml配置
- 【笔试/面试】 —— 最长递增子序列
- mysql study_mysql_study_3
- 哨兵系列卫星介绍——总结与对比
- 深度学习项目实施流程
- MKS GEN_L V2.1 Klipper固件使用说明书
- 学员管理系统(完整版附带源码)
- 容联语音机器人入选“2019金融AI大数据十大解决方案”
- 《英语阅读教学与思维发展》读书笔记(三)
- JavaSE学习笔记_20:Java网络编程
- 后端优化 | VINS-Mono 论文公式推导与代码解析分讲
- 零和博弈-极大极小搜索Alpha-Beta剪枝(井字游戏)
- Google应用商店如何发布app
热门文章
- 经常用电脑辐射大怎么办?
- php网站为微信发信息,PHP发送微信模版消息代码
- C++Win10终结者病毒
- 【E2E】E2E通信保护协议学习2
- android 切换声道,如何将您的Android手机切换为单声道(以便您可以戴一副耳塞) | MOS86...
- sched_setscheduler
- 测试地势高低的手机软件_测海拔高度的手机软件下载
- 虚化背景 - 基于镜头模糊滤镜的深度映射
- arcgis根据7参转坐标_ArcGIS坐标转换操作实战(反算七参数)
- 什么是马丁格尔策略?