Home > Database > Mysql Tutorial > body text

谈谈怎么实现Oracle数据库分区表

WBOY
Release: 2016-06-07 17:00:47
Original
950 people have browsed it

Oracle数据库分区是作为Oracle数据库性能优化的一种重要的手段和方法,做手头的项目以前,只聆听过分区的大名,感觉特神秘,看见

Oracle数据库分区是作为Oracle数据库性能优化的一种重要的手段和方法,做手头的项目以前,只聆听过分区的大名,感觉特神秘,看见某某高手在讨论会上夸夸其谈时,真是骂自己学艺不精,最近作GPS方面的项目,处理的数据量达到了几十GB,为了满足系统的实时性要求,必须提高数据的查询效率,这样就必须通过分区,以解燃眉之急!

先说说分区的好处吧!

1) 增强可用性:如果表的某个分区出现故障,表在其他分区的数据仍然可用;

2) 维护方便:如果表的某个分区出现故障,需要修复数据,只修复该分区即可;

3) 均衡I/O:可以把不同的分区映射到磁盘以平衡I/O,改善整个系统性能;

4) 改善查询性能:对分区对象的查询可以仅搜索自己关心的分区,提高检索速度。

Oracle数据库提供对表或索引的分区方法有三种:

ü 范围分区

ü Hash分区(散列分区)

ü 复合分区

一、范围分区详细说明

范围分区就是对数据表中的某个值的范围进行分区,根据某个值的范围,决定将该数据存储在哪个分区上。如根据序号分区,根据时间等来进行分区。根据序号,比如小于2000000的放在part01, 2000000~4000000的放在part02。。。

create table AAA
(
 id number primary key,
 indate date not null
)
partition by range(indate)
(
 partition part_01 values less than(to_date('2006-01-01','yyyy-mm-dd')) tablespace space01,
 partition part_02 values less than(to_date('2010-01-01','yyyy-mm-dd')) tablespace space02,
 partition part_03 values less than(maxvalue) tablespace space03
);

space01\ space02\ space03为建立的三个表空间,相当于把建立的一个大的表分在了3个不同的表空间的分区上了。

二、Hash分区(散列分区)详细说明

   散列分区为通过指定分区编号来均匀分布数据的一种分区类型,因为通过在I/O设备上进行散列分区,使得这些分区大小一致。也就是只命名分区名称,这样均匀进行数据分布。

三、复合分区详细说明

   有时候我们需要根据范围分区后,每个分区内的数据再散列地分布在几个表空间中,这样我们就要使用复合分区。复合分区是先使用范围分区,然后在每个分区内再使用散列分区的一种分区方法。

partition by range(indate)subpartition by hash(id)
subpartitions 3 store in (space01, space02, space03)
(
partition part_01 values less than(to_date(’2006-01-01’,’yyyy-mm-dd’)),
partition part_02 values less than(to_date(’2010-01-01’,’yyyy-mm-dd’)),
partition part_03 values less than(maxvalue)
 );

四、分区表操作

1、插入记录:insert into AAA values(1 ,sysdate);

2、查询分区表记录:select * from AAA partition(part_01);

3、更新分区表的记录:update AAA partition(part_01) t set indate=’’where id=1; 但是当更新的时候指定了分区,而根据查询的记录不在该分区中时,将不会更新数据

4、删除分区表记录:delete from AAA partition(part_02) t where id=4; 如果指定了分区,而条件中的数据又不在该分区中时,将不会删除任何数据。

5、增加一个分区:alter table AAA add partition part_04 values less than(to_date(’2012-01-01’,’yyyy-mm-dd’)) tablespace dinya_spa ce03; 增加一个分区的时候,,增加的分区的条件必须大于现有分区的最大值,否则系统将提示ORA-14074 partition bound must collate higher than that of the last partition 错误。

6、合并一个分区:alter table AAA merge partitions part_01,part_02 into partition part_02; ,如果在合并的时候把合并后的分区定为part_01的时候,系统将提示ORA-14275 cannot reuse lower-bound partition as resulting partition 错误。

7、删除分区:alter table AAA drop partition part_01; 删除分区表的一个分区后,查询该表的数据时显示,该分区中的数据已全部丢失,所以执行删除分区动作时要慎重,确保先备份数据后再执行,或将分区合并。

五、建立索引

    分区表和一般表一样可以建立索引,分区表可以创建局部索引和全局索引。当分区中出现许多事务并且要保证所有分区中的数据记录的唯一性时采用全局索引。

1.       局部索引分区的建立:create index idx_t on AAA(id)
 local
(
partition idx_1 tablespace space01,
partition idx_2 tablespace space02,
partition idx_3 tablespace space03
);

2.       全局索引建立时global 子句允许指定索引的范围值,这个范围值为索引字段的范围值:create index idx_t on AAA(id)
global partition by range(id)
(
partition idx_1 values less than (1000) tablespace space01,
partition idx_2 values less than (10000) tablespace space02,
partition idx_3 values less than (maxvalue) tablespace space03
);

当然也可以不指定索引分区名直接对整个表建立索引: create index idx_t on AAA(id);

linux

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!