当前位置:Gxlcms > 数据库问题 > Oracle中分区表的使用

Oracle中分区表的使用

时间:2021-07-01 10:21:17 帮助过:3人阅读

create tablespace HRPM0

datafile ‘/oradata/misdb/HRPM0.DBF‘ size 5m autoextend on next 10m maxsize unlimited

2)删除表空间(同时把数据文件也删除)

DROP TABLESPACE data01 INCLUDING CONTENTS AND DATAFILES;

   如果不想删除数据文件:

           Drop tablespace tablespace_name;

3) 修改表空间大小

alter database datafile ‘/path/NADDate05.dbf‘ resize 100M

4)添加数据文件(在建立表空间时,若是约束了表空间的大小,那么一段时间后,这个表空间就会被装满,无法再添加其他对象。则需要给表空间添加数据文件):

Alter tablespace tablespace_name add datafile’ ‘/path/NADDate06.dbf’ size 100M;

4) 备注:

4.1).--.禁止undo tablespace自动增长

alter database datafile ‘full_path\undotbs01.dbf‘ autoextend off;

4.2).-- 创建一个新的小空间的undo tablespace

create undo tablespace undotBS2 datafile ‘full_path\UNDOTBS02.DBF‘ size 100m;

4.3).-- 设置新的表空间为系统undo_tablespace

alter system set undo_tablespace=undotBS2;

4.4).-- Drop 旧的表空间

drop tablespace undotbs1 including contents;

4.5).--查看所有表空间的情况

select * from dba_tablespaces

5)查到一个最好用的表:dict

5.1)select *from dict where table_name like ‘%PART%‘

5.2)ALL_TAB_PARTITIONS:可以查出表所对应的分区内容;

5.3)dab_tab_partitons :与上2);

5.4)dba_ind_partitons:查询分区的索引;

5.5)子分区也是一样的(dba_tab_subpartitons,dba_ind_partitons)

一、使用分区的优点:

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

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

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

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

二、Oracle数据库提供对表或索引的分区方法有几种(收集到四种):

        1、范围分区

        2、列表分区

        3、Hash分区(散列分区)

        4、复合分区

三、详描述分区实例:

    1)下面将以实例的方式分别对这三种分区方法来说明分区表的使用。为了测试方便,我们先建三个表空间。

create tablespace dinya_space01 datafile ‘C:\表空间\dinya01.dbf‘ size 5M; create tablespace dinya_space02 datafile ‘C:\表空间\dinya02.dbf‘SIZE 5M; create tablespace dinya_space03 datafile ‘C:\表空间\dinya03.dbf‘ SIZE 5M;

select * from user_tablespaces

 

                   <表空间->三个>

1.1)范围分区

    范围分区就是对数据表中的某个值的范围进行分区,根据某个值的范围,决定将该数据存储在哪个分区上。如根据序号分区,根据业务记录的创建日期进行分区等。

    需求描述:有一个物料交易表,表名:material_transactions。该表将来可能有千万级的数据记录数。要求在建该表的时候使用分区表。这时候我们可以使用序号分区三个区,每个区中预计存储三千万的数据,也可以使用日期分区,如每五年的数据存储在一个分区上。

根据交易记录的序号分区建表:----为了测试需要做以下修改;

create table dinya_test

   (

        transaction_id number primary key,

        item_id number(8) not null,

        item_description varchar2(300),

        transaction_date date not null

    )

    partition by range (transaction_id)

    (

       partition part_01 values less than(2) tablespace dinya_space01,-----2条以下的交易在此分区上:part_01

      partition part_02 values less than(3) tablespace dinya_space02,-----等于+大于2而小于3的交易在此分区:part_02

      partition part_03 values less than(maxvalue) tablespace dinya_space03----大于3的交易在此分区:part_03

-----------------以上在pl/sql测试成功;

---------------- 以下没有在pl/sql测试!

根据交易日期分区建表:

 

SQL> create table dinya_test

   (

        transaction_id number primary key,

        item_id number(8) not null,

       item_description varchar2(300),

       transaction_date date not null  

 )

    partition by range (transaction_date)

    (

   partition part_01 values less than(to_date(‘2006-01-01‘,‘yyyy-mm-dd‘)) tablespace dinya_space01,

   partition part_02 values less than(to_date(‘2010-01-01‘,‘yyyy-mm-dd‘)) tablespace dinya_space02,

 partition part_03 values less than(maxvalue) tablespace dinya_space03

 );

这样我们就分别建了以交易序号和交易日期来分区的分区表。每次插入数据的时候,系统将根据指定的字段的值来自动将记录存储到制定的分区(表空间)中。

当然,我们还可以根据需求,使用两个字段的范围分布来分区,如partition by range ( transaction_id ,transaction_date),分区条件中的值也做相应的改变,请读者自行测试。

---------------------------------以上没有在pl/sql测试!

1.2) 范围分区创建成功之后的相关操作测试;

 a)向表添加测试数据:

insert into dinya_test values(1,12,‘BOOKS‘,sysdate);

insert into dinya_test values(2,12, ‘BOOKS‘,sysdate+30);

insert into dinya_test values(3,12, ‘BOOKS‘,to_date(‘2006-05-30‘,‘yyyy-mm-dd‘));

insert into dinya_test values(4,12, ‘BOOKS‘,to_date(‘2007-06-23‘,‘yyyy-mm-dd‘));

insert into dinya_test values(5,12, ‘BOOKS‘,to_date(‘2011-02-26‘,‘yyyy-mm-dd‘));

insert into dinya_test values(6,12, ‘BOOKS‘,to_date(‘2011-04-30‘,‘yyyy-mm-dd‘));

b)查询

   b.1)如果查询全表数据

select * from dinya_test;如下图:

 

                                < 全表数据>

select * from dinya_test partition(part_01);如下图:

 

                              <Part_01分区的数据>

select * from dinya_test partition(part_02);如下图:

 

                               <Part_02分区的数据>

select * from dinya_test partition(part_03);如下图:

 

                               <Part_03分区的数据>

update dinya_test partition(part_01) t set t.item_description=‘DESK‘ where t.transaction_id=1;

select * from dinya_test partition(part_01);BOOKS->DESK(发生变化)

 

                  

select * from dinya_test(此结果就不用查看了,肯定变了);

---删除part_03分区中transaction_id=4的记录:

delete from dinya_test partition(part_03) t where t.transaction_id=4;

select * from dinya_test partition(part_03)

 

             少了transaction_id=4的记录(与上图对比)

c)索引的创建:

   c.1)局部索引的创建:

create index dinya_idx_t on dinya_test(item_id)

 local

   (

     partition idx_1 tablespace dinya_space01,---分区名为:idx_1

     partition idx_2 tablespace dinya_space02, ---分区名为:idx_2

     partition idx_3 tablespace dinya_space03---分区名为:idx_3

 );  ---pl/sql测试成功

注:

select *from ALL_TAB_PARTITIONS where table_name =‘DINYA_TEST‘

 

    select *From dba_ind_partitions where partition_name=‘IDX_1‘

 

   c.2)全局索引的创建:

全局索引建立时global 子句允许指定索引的范围值,这个范围值为索引字段的范围值:

create index dinya_idx_t on dinya_test(item_id)

   global partition by range(item_id)

 (

     partition idx_1 values less than (1000) tablespace dinya_space01,

     partition idx_2 values less than (10000) tablespace dinya_space02,

     partition idx_3 values less than (maxvalue) tablespace dinya_space03

 );----PL/SQL末测试[参照以上local]

整个表创建索引:

   Create index dinya_idx_t on dinya_test(item_id);

备注: select *from all_indexes(dba_indexesall_ind_columns user_ind_columns dba_ind_columns)

1.3) Hash分区(散列分区)

——————————以下没有在机器上测试

    散列分区为通过指定分区编号来均匀分布数据的一种分区类型,因为通过在I/O设备上进行散列分区,使得这些分区大小一致。如将物料交易表的数据根据交易ID散列地存放在指定的三个表空间中:

create table dinya_test

    (

       transaction_id number primary key,

        item_id number(8) not null,

        item_description varchar2(300),

        transaction_date date

   )

   partition by hash(transaction_id)

   (

       partition part_01 tablespace dinya_space01,

       partition part_02 tablespace dinya_space02,

       partition part_03 tablespace dinya_space03

 );

建表成功,此时插入数据,系统将按transaction_id将记录散列地插入三个分区中,这里也就是三个不同的表空间中。

——————————以上没有在机器上测试;

 

1.4) 列表分区:该分区的特点是某列的值只有几个,基于这样的特点我们可以采用列表分区。

示例1

CREATE TABLE PROBLEM_TICKETS

(

PROBLEM_ID NUMBER(7) NOT NULL PRIMARY KEY,

DESCRIPTION VARCHAR2(2000),

CUSTOMER_ID NUMBER(7) NOT NULL,

DATE_ENTERED DATE NOT NULL,

STATUS VARCHAR2(20)

)

PARTITION BY LIST (STATUS)

(

PARTITION PROB_ACTIVE VALUES (‘ACTIVE‘) TABLESPACE PROB_TS01,

                  

PARTITION PROB_INACTIVE VALUES (‘INACTIVE‘) TABLESPACE PROB_TS02

)     

备注:active和inactive是列status的值!谨记与range和hash分区的区别;

1.4.1)测试如下:

   insert into PROBLEM_TICKETS values(1,‘BOOKS‘,1,sysdate,‘ACTIVE‘);

insert into PROBLEM_TICKETS values(2,‘son‘,2,sysdate+30,‘INACTIVE‘);

insert into PROBLEM_TICKETS values(3,‘son‘,3,to_date(‘2006-05-30‘,‘yyyy-mm-dd‘),‘INACTIVE‘);

insert into PROBLEM_TICKETS values(4,‘BOOKS‘,4,to_date(‘2007-06-23‘,‘yyyy-mm-dd‘),‘INACTIVE‘);

insert into PROBLEM_TICKETS values(5,‘old‘,5,to_date(‘2011-02-26‘,‘yyyy-mm-dd‘),‘ACTIVE‘);

insert intoPROBLEM_TICKETSvalues(6,‘test‘,6,to_date(‘2011-04-30‘,‘yyyy-mm-dd‘),‘INACTIVE‘);

select * from PROBLEM_TICKETS

 

                                     <查询全表>

1.4.2)

   select * from PROBLEM_TICKETS partition(PROB_ACTIVE)

 

1.4.2)

   select * from PROBLEM_TICKETS partition(PROB_INACTIVE)

 

 在测试中遇到这样的情况。如果表创建了分区,如果要删除数据文件(表空间文件),则要先删除分区,然后才能删除数据文件(但是在删除数据文件时,必须要保留一个分区才能最终删除数据文件>表空间文件,)

当然,也可以直接就删除表也行,刚所有的全删除,但是表空间文件还在!

 

1.5) 复合分区

 有时候我们需要根据范围分区后,每个分区内的数据再散列地分布在几个表空间中,这样我们就要使用复合分区。复合分区是先使用范围分区,然后在每个分区内再使用散列分区的一种分区方法,如将物料交易的记录按时间分区,然后每个分区中的数据分三个子分区,将数据散列地存储在三个指定的表空间中:

create table dinya_test

    (

        transaction_id number primary key,

        item_id number(8) not null,

       item_description varchar2(300),

      transaction_date date

    )

   partition by range(transaction_date)subpartition by hash(transaction_id)

       subpartitions 3 store in (dinya_space07,dinya_space08,dinya_space09)

 (

      partition part_07 values less than(to_date(‘2006-01-01‘,‘yyyy-mm-dd‘)),

      partition part_08 values less

人气教程排行