分区表需要数据备份吗oracle,Oracle分区表 学习笔记

本文详细介绍了在Oracle数据库中创建和使用分区表的过程,包括建立表空间、创建分区表、数据迁移、索引建立以及查询优化。通过案例展示了如何将普通表转化为分区表,以提升数据库的读写性能,并利用分区索引提高查询效率。
摘要由CSDN通过智能技术生成

今天,学习了Oralce分区表的相关问题,对其进行了一些研究。

首先,末尾来看一下在Oracle数据库中采用分区表的必要性。Oracle在空间数据方面主要采用分区表的形式来平衡大表的I/O,同时避免建议超大索引占用空间,有利于数据库的读写性能。

为了能够体现分区表在执行SQL查询时的重要性,我们可以采用一张存储2000行数据的表作为案例进行试验。在这里,以mzl表为例说明:

select * from mzl;

1 13000000004 13000000004 20090305000000 0 0 20090304000000

2 13000000008 13000000008 20090305000000 0 0 20090304000000

3 13000000012 13000000012 20090305000000 0 0 20090304000000

4 13000000015 13000000015 20090305000000 0 0 20090304000000

5 13000000019 13000000019 20090305000000 0 0 20090304000000

6 13000000023 13000000023 20090305000000 0 0 20090304000000

7 13000000027 13000000027 20090305000000 0 0 20090304000000

8 13000000031 13000000031 20090305000000 0 0 20090304000000

9 13000000034 13000000034 20090305000000 0 0 20090304000000

......

这是一张普通类型的表,我们可以把它转化为我们所需要的分区表。与此同时,我们需要做好原表的备份。

1.建立表空间

为了提高数据库的读写性能,我们可以把不同分区存放在不同的表空间里,首先需要预先建立好相应的表空间,否则在创建分区表时,可能会出问题。

create tablespace one   datafile 'd:\one.dbf' size 5M;

create tablespace two   datafile 'd:\two.dbf' size 5M;

create tablespace three datafile 'f:\three.dbf' size 5M;

create tablespace four  datafile 'f:\four.dbf' size 5M;

create tablespace five   datafile 'e:\five.dbf' size 5M;

2.建立分区表

create table mzl_p

(

id char(11) not null,

sid char(11) not null,

time varchar(20),

status int,

status2 int,

log varchar(20)

)

partition by range(sid)    #按照sid范围进行分区(

partition s1 values less than (13000004000) tablespace one,

partition s2 values less than (13000008000) tablespace two,

partition s3 values less than (13000012000) tablespace three,

partition s4 values less than (13000016000) tablespace four,

partition s5 values less than (MAXVALUE) tablespace five

);

3.重命名表

alter table mzl     rename to mzl_bak;  #将原表作为备份表

alter table mzl_p rename to mzl;

4.导入原表数据到新表中。

insert /*+append*/ into mzl select * from mzl_bak;

5.删除备份表

drop table mzl_bak;

如果没有出现错误,则表明转换工作没有问题,下面,我们来谈谈分区表的查询和维护。

6.分区表查询

select * from mzl partition(s2);  #查询s2分区的情况。

7.分区表分裂

alter table mzl

split partition s1 at('13000002000')

into (partition s1a compress tablespace one,partition s1);

8.分区表索引建立

create index mzl_indx on mzl(time,log)

local

(

partition s1 tablespace one,

partition s1a tablespace one,

partition s2 tablespace two,

partition s3 tablespace three,

partition s4 tablespace four,

partition s5 tablespace five

);

验证分区表索引对数据库查询的影响。

select * from  mzl;

按F5键查看语句执行的开销,发现此时开销为48.此时进行的是全表扫描。

select *  from mzl where time ='20090306000000';

按F5键查看语句执行的开销,发现此时开销为48.此时通过刚刚建立的索引进行查询,可见,分区索引可以提高查询效率。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值