时间:2023-10-31来源:系统城装机大师作者:佚名
工作中有一张表一年会增长100多万的数据,量虽然不大,可是表字段多,所以一年下来也会达到 1G,而且只增不改,故考虑使用分区表来提高查询性能,提高维护性。
oracle 11g 支持自动分区,不过得在创建表时就设置好分区。
如果已经存在的表需要改分区表,就需要将当前表 rename后,再创建新表,然后复制数据到新表,然后删除旧表就可以了。
1、一般一张表超过2G的大小,ORACLE是推荐使用分区表的。
2、这张表主要是查询,而且可以按分区查询,只会修改当前最新分区的数据,对以前的不怎么做删除和修改。
3、数据量大时查询慢。
4、便于维护,可扩展:11g 中的分区表新特性:Partition(分区)一直是 Oracle 数据库引以为傲的一项技术,正是分区的存在让 Oracle 高效的处理海量数据成为可能,在 Oracle 11g 中,分区技术在易用性和可扩展性上再次得到了增强。
5、与普通表的 sql 一致,不需要因为普通表变分区表而修改我们的代码。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
numtoyminterval(1, 'year')--按年创建分区表create table test_part( ID NUMBER(20) not null, REMARK VARCHAR2(1000), create_time DATE)PARTITION BY RANGE (CREATE_TIME) INTERVAL (numtoyminterval(1, 'year'))(partition part_t01 values less than(to_date('2018-11-01', 'yyyy-mm-dd')));--创建主键alter table test_part add constraint test_part_pk primary key (ID) using INDEX;-- Create/Recreate indexescreate index test_part_create_time on TEST_PART (create_time); |
| 1 2 3 4 5 6 7 8 9 10 11 12 |
numtoyminterval(1, 'month')--按月创建分区表create table test_part( ID NUMBER(20) not null, REMARK VARCHAR2(1000), create_time DATE)PARTITION BY RANGE (CREATE_TIME) INTERVAL (numtoyminterval(1, 'month'))(partition part_t01 values less than(to_date('2018-11-01', 'yyyy-mm-dd')));--创建主键alter table test_part add constraint test_part_pk primary key (ID) using INDEX; |
| 1 2 3 4 5 6 7 8 9 10 11 12 |
NUMTODSINTERVAL(1, 'day')--按天创建分区表create table test_part( ID NUMBER(20) not null, REMARK VARCHAR2(1000), create_time DATE)PARTITION BY RANGE (CREATE_TIME) INTERVAL (NUMTODSINTERVAL(1, 'day'))(partition part_t01 values less than(to_date('2018-11-12', 'yyyy-mm-dd')));--创建主键alter table test_part add constraint test_part_pk primary key (ID) using INDEX; |
| 1 2 3 4 5 6 7 8 9 10 11 12 |
NUMTODSINTERVAL (7, 'day')--按周创建分区表create table test_part( ID NUMBER(20) not null, REMARK VARCHAR2(1000), create_time DATE)PARTITION BY RANGE (CREATE_TIME) INTERVAL (NUMTODSINTERVAL (7, 'day'))(partition part_t01 values less than(to_date('2018-11-12', 'yyyy-mm-dd')));--创建主键alter table test_part add constraint test_part_pk primary key (ID) using INDEX; |
| 1 2 3 4 5 |
可以添加几条数据来看看效果,oracle 会自动添加分区。--查询当前表有多少分区select table_name,partition_name from user_tab_partitions where table_name='TEST_PART';--查询这个表的某个(SYS_P21)里的数据select * from TEST_PART partition(SYS_P21); |
把 x 转为 interval day to second 数据类型。
常用的单位有 ('day','hour','minute','second')。
测试一下:
| 1 | select sysdate, sysdate + numtodsinterval(4,'hour') as res from dual; |
结果:

将 x 转为 interval year to month 数据类型。
常用的单位有 ('year','month')。
测试一下:
| 1 | select sysdate, sysdate + numtoyminterval(3, 'year') as res from dual; |
结果:

表示小于 2018-11-01 的都放在 part_t01 分区表中。
需要先备份表,然后新建这个表,拷贝数据,删除备份表。
-- 1. 重命名
| 1 | alter table test_part rename to test_part_temp; |
-- 2. 创建 partition table
| 1 2 3 4 5 6 7 8 |
create table test_part( ID NUMBER(20) not null, REMARK VARCHAR2(1000), create_time DATE)PARTITION BY RANGE (CREATE_TIME) INTERVAL (numtoyminterval(1, 'month'))(partition part_t1 values less than(to_date('2018-11-01', 'yyyy-mm-dd'))); |
-- 3. 创建主键
| 1 | alter table test_part add constraint test_part_pk_1 primary key (ID) using INDEX; |
-- 4. 将 test_part_temp 表里的数据迁移到 test_part 表中
| 1 | insert into test_part_temp select * from test_part; |
-- 5. 为分区表设置索引
| 1 2 |
-- Create/Recreate indexescreate index test_part_create_time_1 on TEST_PART (create_time); |
-- 6. 删除老的 test_part_temp 表
| 1 | drop table test_part_temp purge; |
-- 7. 作用是:允许分区表的分区键是可更新。
-- 当某一行更新时,如果更新的是分区列,并且更新后的列植不属于原来的这个分区,
-- 如果开启了这个选项,就会把这行从这个分区中 delete 掉,并加到更新后所属的分区,此时就会发生 rowid 的改变。
-- 相当于一个隐式的 delete + insert ,但是不会触发 insert/delete 触发器。
| 1 | alter table test_part enable row movement; |
我的理解是:
当查询经常跨分区查,则应该使用全局索引,因为这是全局索引比分区索引效率高。
当查询在一个分区里查询时,则应该使用 local 索引,因为本地索引比全局索引效率高。
2023-10-31
Oracle如何编写一个sqlldr实例2023-10-31
Oracle的SQLLDR用法简介2023-10-31
Oracle中的高效SQL编写PARALLEL解析1.Oracle数据库系统结构概述 2.Oracle数据库存储结构 物理存储结构 控制文件 数据文件 重做日志文件 归档日志文件 Oracle数据库逻辑结构 数据块(Data Block) (盘)区(Extent) 段(Segment) 表空间(Tablespace) 本地管...
2023-10-31
windows下的Oracle19c 一、官网下载Oracle19c数据库 二、安装Oracle数据库 1.解压安装包 2.运行setup.exe安装 三、配置 四、安装完Oracle数据库,给scott用户解锁 1.解决Oracle数据库中没有scott账户的问题 2.给scott...
2023-10-31