分区表、分区索引

   为了简化数据库大表的管理.ORACLE8以后推出了分区选项.分区可将表分离在不同的表空间上,用分而治之的方法来支撑元限膨胀的大表.将大表分割成较小的分区可以改善表的维护、备份、恢复、事务及查询性能。

分区的优点:
1、 增强可用性:如果表的一个分区由于系统故障而不能使用,表的其余好的分区仍可以使用;
2、 减少修复时间:如果系统故障只影响表的一部份分区,那么只有这部份分区需要修复,可能比整个大表修复花的时间更少;
3、 维护轻松:管理每个公区比管理单个大表要轻松得多;
4、 均衡I/O:可以把表的不同分区分配到不同的磁盘来平衡I/O改善性能;
5、 改善性能:对大表的查询、增加、修改等操作可以分解到表的不同分区来并行执行,可使运行速度更快。
6、 分区对用户透明,最终用户感觉不到分区的存在。

创建测试表空间:

 1 SQL> CREATE TABLESPACE wf1 DATAFILE '/disk4/ora10/oradata/ora1/wf1.dbf' SIZE 10M

 2 2 AUTOEXTEND ON NEXT 1M MAXSIZE 15M

 3 3 EXTENT MANAGEMENT LOCAL UNIFORM SIZE 128K;

 4 

 5 Tablespace created.

 6 

 7 SQL> CREATE TABLESPACE wf2 DATAFILE '/disk4/ora10/oradata/ora1/wf2.dbf' SIZE 10M

 8 2 AUTOEXTEND ON NEXT 1M MAXSIZE 15M

 9 3 EXTENT MANAGEMENT LOCAL UNIFORM SIZE 128K;

10 

11 Tablespace created.

12 

13 SQL>

一、按范围分区:固名思义就是按一定range来分区,看下面的例子:

 1 SQL> create table wf_part

 2 2 (

 3 3 wf_id integer primary key,

 4 4 wf_date date,

 5 5 wf_dec varchar2(50)

 6 6 )TABLESPACE userdata

 7 7 partition by range(wf_date)(

 8 8 partition part_01 values less than(to_date('2006-01-01','yyyy-mm-dd')) tablespace wf1,

 9 9 partition part_02 values less than(to_date('2007-01-01','yyyy-mm-dd')) tablespace wf2,

10 10 partition part_03 values less than(maxvalue) tablespace userdata);

11 

12 Table created.

13 

14 SQL> 

注意:这里将表,和表分区建立在了不同的表空间中。

分别向三个分区插入数据

 1 SQL> insert into wf_part values(1,to_date('2005-12-30','yyyy-mm-dd'),'less 2006-01-01');

 2 

 3 1 row created.

 4 

 5 SQL> commit;

 6 

 7 Commit complete.

 8 

 9 SQL> insert into wf_part values(2,to_date('2006-01-01','yyyy-mm-dd'),'equal 2007-01-01');

10 

11 1 row created.

12 

13 SQL> insert into wf_part values(3,sysdate,'sysdate');

14 

15 1 row created.

16 

17 SQL> commit;

18 

19 Commit complete.

20 

21 SQL>

做查询:

 1 SQL> select * from wf_part partition(part_01);

 2 

 3 WF_ID WF_DATE WF_DEC

 4 ---------- --------- --------------------------------------------------

 5 1 30-DEC-05 less 2006-01-01

 6 

 7 SQL> select * from wf_part partition(part_02);

 8 

 9 WF_ID WF_DATE WF_DEC

10 ---------- --------- --------------------------------------------------

11 2 01-JAN-06 equal 2007-01-01

12 

13 SQL> select * from wf_part partition(part_03);

14 

15 WF_ID WF_DATE WF_DEC

16 ---------- --------- --------------------------------------------------

17 3 19-MAR-12 sysdate

18 

19 SQL>

二、索引分区:
局部索引分区的建立:(注意:表必须存在分区,此分区的个数必须和分区表的分区个数一样,不然是建立不起来的)

1 SQL>create index idx_part_id on wf_part(wf_dec) local (

2 2 partition idx_part_id01 tablespace userdata,

3 3 partition idx_part_id02 tablespace userdata

4 4 );

5 create index idx_part_id on wf_part(wf_dec) local (

6               *

7 ERROR at line 1:

8 ORA-14024: number of partitions of LOCAL index must equal that of the

9 underlying table

以分区字段建立索引

 1 SQL> CREATE INDEX idx_part_id on wf_part(wf_date) LOCAL

 2 2 (

 3 3 partition idx_part_id01 tablespace userdata,

 4 4 partition idx_part_id02 tablespace userdata, 

 5 5 partition idx_part_id03 tablespace userdata

 6 6 );

 7 

 8 Index created.

 9 

10 SQL> select owner,index_name,table_name from dba_part_indexes where table_name

11 2 ='WF_PART';

12 

13 OWNER     INDEX_NAME     TABLE_NAME

14 -------------- ---------------     ------------------------------

15 SYS      IDX_PART_ID   WF_PART

不指定指定索引的分区:

 1 SQL> drop index IDX_PART_ID;

 2 

 3 Index dropped.

 4 

 5 SQL> CREATE INDEX idx_part_id ON wf_part(wf_date) LOCAL;

 6 

 7 Index created.

 8 

 9 SQL>select owner,index_name,table_name from dba_part_indexes where table_name ='WF_PART';

10 

11 OWNER     INDEX_NAME     TABLE_NAME

12 -------------- ---------------     ------------------------------

13 SYS      IDX_PART_ID   WF_PART

这个操作要求较大的临时表空间和排序区。(未理解)

三.分区维护:(只对范围分区)

(1)增加一个分区:分区范围只能往上增,不能增加一个少于原有的分区:
  alter table niegc_part add partition part_03 values less than(maxvalue)
(2)合并分区:(合并后的分区必须指下最后一个大value的分区)
  alter table niegc_part merge partitions part_02,part_03 into partition part_03
(3)删除一个分区:
  alter table niegc_part drop partition part_01

四、总结:

  分区表是将大表的数据分成称为分区的许多小的子集,提供四种分区方法:列表分区,范围分区,哈希分区和混合分区;

你可能感兴趣的:(分区表)