oracle -- 分区(extent)

基本关系:数据库---表空间---数据段---分区---数据块


一、分区(extent)
分区extent是比数据块大一级的存储结构,是几个逻辑上相邻的data block的组合。我们知道,物理存储通常是随机的读写过程。即使在同一个文件里,我们也不能保证相同的一个信息是存储在绝对连续的物理存储空间的。Oracle数据存储同样如此。

分区extent是磁盘空间分配的最小单位。磁盘按区划分,每次至少分配一个区。区存储于段中,它由连续的数据块组成。区的分配过程中,每次至分配5个区。如果所剩的空闲空间不够5个区,就会出现错误:ORA-01653。

在进行存储数据信息的时候,Oracle将分配数据块进行存储,但是不能保证所有分配的数据块都是连续的结构。所以,出现分区extent的概念,表示一系列连续的数据块集合。

SQL>  desc dba_extents;
Name                  Type          Nullable     Default Comments                                                  
--------------- ------------- -------- ------- ---------------------------------------------------------
OWNER                VARCHAR2(128)    Y                Owner of the segment associated with the extent           
SEGMENT_NAME     VARCHAR2(128)    Y                Name of the segment associated with the extent            
PARTITION_NAME    VARCHAR2(128)    Y                Partition/Subpartition Name, if any, of the segment       
SEGMENT_TYPE      VARCHAR2(18)     Y                Type of the segment                                       
TABLESPACE_NAME VARCHAR2(30)    Y                Name of the tablespace containing the extent              
EXTENT_ID          NUMBER           Y                Extent number in the segment                              
FILE_ID             NUMBER          Y                Name of the file containing the extent                    
BLOCK_ID          NUMBER            Y                Starting block number of the extent                       
BYTES                 NUMBER           Y                Size of the extent in bytes                               
BLOCKS              NUMBER           Y                Size of the extent in ORACLE blocks                       
RELATIVE_FNO       NUMBER           Y                Relative number of the file containing the segment header

二、什么时候使用分区表:
1、表的大小超过2gb。
2、表中包含历史数据,新的数据被增加都新的分区中。
 

三、表分区有以下优点:
1、改善查询性能:对分区对象的查询可以仅搜索自己关心的分区,提高检索速度。
2、增强可用性:如果表的某个分区出现故障,表在其他分区的数据仍然可用;
3、维护方便:如果表的某个分区出现故障,需要修复数据,只修复该分区即可;
4、均衡i/o:可以把不同的分区映射到磁盘以平衡i/o,改善整个系统性能。



四、表分区的几种类型及操作方法
 一.范围分区:
范围分区将数据基于范围映射到每一个分区,这个范围是你在创建分区时指定的分区键决定的。这种分区方式是最为常用的,并且分区键经常采用日期。
当使用范围分区时,请考虑以下几个规则:
1、每一个分区都必须有一个values less then子句,它指定了一个不包括在该分区中的上限值。分区键的任何值等于或者大于这个上限值的记录都会被加入到下一个高一些的分区中。
2、所有分区,除了第一个,都会有一个隐式的下限值,这个值就是此分区的前一个分区的上限值。
3、在最高的分区中,maxvalue被定义。maxvalue代表了一个不确定的值。这个值高于其它分区中的任何分区键的值,也可以理解为高于任何分区中指定的value less then的值,同时包括空值。

假设有一个customer表,表中有数据200000行,我们将此表进行分区,每个分区存储100000行,我们将每个分区保存到单独的表空间中,这样数据文件就可以跨越多个物理磁盘。下面是创建表和分区的代码,如下:
例一:按customer_id进行分区
create table customer
(
    customer_id  number   not null primary key,
    first_name  varchar2(30)  not null,
    last_name   varchar2(30)  not null,
    phone        varchar2(15)  not null,
    email        varchar2(80),
    status       char(1)
)
partition by range (customer_id)
(
    partition cus_part1 values less than (100000) tablespace cus_ts01,
    partition cus_part2 values less than (200000) tablespace cus_ts02
)
例二:按时间划分
create table order_activities
(
    order_id         number(7) not null,
    order_date     date,
    total_amount  number,
    custotmer_id   number(7),
    paid                 char(1)
)
partition by range (order_date)
(
  partition ord_act_part01 values less than (to_date('01- may -2003','dd-mon-yyyy')) tablespaceord_ts01,
  partition ord_act_part02 values less than (to_date('01-jun-2003','dd-mon-yyyy')) tablespace ord_ts02,
  partition ord_act_part02 values less than (to_date('01-jul-2003','dd-mon-yyyy')) tablespace ord_ts03
)
例三:maxvalue
create table rangetable
(
  idd       int   primary key ,
  iname  varchar(10),
  grade   int
)
partition  by  range (grade)
(
      partition  part1 values  less  then (1000) tablespace  part1_tb,
      partition  part2 values  less  then (maxvalue) tablespace  part2_tb
);
 


二.列表分区:
列表分区明确指定了根据某字段的某个具体值进行分区,而不是像范围分区那样根据字段的值范围来划分的。基于这样的特点我们可以采用列表分区。
例一
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
例二
create  table  listtable
(
    id    int  primary  key ,
    name  varchar (20),
    area  varchar (10)
)
partition  by  list (area)
(
    partition  part1 values ('guangdong','beijing') tablespace  part1_tb,
    partition  part2 values ('shanghai','nanjing')  tablespace  part2_tb
);
)
 


三.散列分区:
这类分区是在列值上使用散列算法,以确定将行放入哪个分区中。当列的值没有合适的条件时,建议使用散列分区。
散列分区为通过指定分区编号来均匀分布数据的一种分区类型,因为通过在i/o设备上进行散列分区,使得这些分区大小一致。
例一:
create table hash_table
(
  col number(8),
  inf varchar2(100)
)
partition by hash (col)
(
  partition part01 tablespace hash_ts01,
  partition part02 tablespace hash_ts02,
  partition part03 tablespace hash_ts03
)
简写:
create table emp
(
    empno number (4),
    ename varchar2 (30),
    sal   number
)
partition by  hash (empno) partitions 8
store in (emp1,emp2,emp3,emp4,emp5,emp6,emp7,emp8);
hash分区最主要的机制是根据hash算法来计算具体某条纪录应该插入到哪个分区中,hash算法中最重要的是hash函数,oracle中如果你要使用hash分区,只需指定分区的数量即可。建议分区的数量采用2的n次方,这样可以使得各个分区间数据分布更加均匀。
 


四.组合范围散列分区
这种分区是基于范围分区和列表分区,表首先按某列进行范围分区,然后再按某列进行列表分区,分区之中的分区被称为子分区。
create table sales
(
product_id varchar2(5),
sales_date date,
sales_cost number(10),
status varchar2(20)
)
partition by range(sales_date) subpartition by list (status)
(
   partition p1 values less than(to_date('2003-01-01','yyyy-mm-dd'))tablespace rptfact2009
          (
              subpartition p1sub1 values ('active') tablespace rptfact2009,
              subpartition p1sub2 values ('inactive') tablespace rptfact2009
          ),
   partition p2 values less than (to_date('2003-03-01','yyyy-mm-dd')) tablespace rptfact2009
          (
              subpartition p2sub1 values ('active') tablespace rptfact2009,
              subpartition p2sub2 values ('inactive') tablespace rptfact2009
          )
)
 


五.复合范围散列分区:
这种分区是基于范围分区和散列分区,表首先按某列进行范围分区,然后再按某列进行散列分区。
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_space01,dinya_space02,dinya_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)
 );

六、有关表分区的一些维护性操作:
一、添加分区
以下代码给sales表添加了一个p3分区
alter table sales add partition p3 values less than(to_date('2003-06-01','yyyy-mm-dd'));
注意:以上添加的分区界限应该高于最后一个分区界限。
以下代码给sales表的p3分区添加了一个p3sub1子分区
alter table sales modify partition p3 add subpartition p3sub1 values('complete');

二、删除分区
以下代码删除了p3表分区:
alter table sales drop partition p3;
在以下代码删除了p4sub1子分区:
alter table sales drop subpartition p4sub1;
注意:如果删除的分区是表中唯一的分区,那么此分区将不能被删除,要想删除此分区,必须删除表。

三、截断分区
截断某个分区是指删除某个分区中的数据,并不会删除分区,也不会删除其它分区中的数据。当表中即使只有一个分区时,也可以截断该分区。通过以下代码截断分区:
alter table sales truncate partition p2;
通过以下代码截断子分区:
alter table sales truncate subpartition p2sub2;

四、合并分区
合并分区是将相邻的分区合并成一个分区,结果分区将采用较高分区的界限,值得注意的是,不能将分区合并到界限较低的分区。以下代码实现了p1 p2分区的合并:
alter table sales merge partitions p1,p2 into partition p2;

五、拆分分区
拆分分区将一个分区拆分两个新分区,拆分后原来分区不再存在。注意不能对hash类型的分区进行拆分。
alter table sales sblit partition p2 at(to_date('2003-02-01','yyyy-mm-dd')) into (partition p21,partition p22);

六、接合分区(coalesca)
结合分区是将散列分区中的数据接合到其它分区中,当散列分区中的数据比较大时,可以增加散列分区,然后进行接合,值得注意的是,接合分区只能用于散列分区中。通过以下代码进行接合分区:
alter table sales coalesca partition;

七、重命名表分区
以下代码将p21更改为p2
alter table sales rename partition p21 to p2;

八、相关查询
跨分区查询
select sum( *) from
(select count(*) cn from t_table_ss partition (p200709_1)
union all
select count(*) cn from t_table_ss partition (p200709_2)
);

查询表上有多少分区
select * from user_tab_partitions where table_name='tablename'

查询索引信息
select object_name,object_type,tablespace_name,sum(value)
from v$segment_statistics
where statistic_name in ('physical reads','physical write','logical reads')and object_type='index'
group by object_name,object_type,tablespace_name
order by 4 desc
 
--显示数据库所有分区表的信息:
select * from dba_part_tables
 
--显示当前用户可访问的所有分区表信息:
select * from all_part_tables
 
--显示当前用户所有分区表的信息:
select * from user_part_tables
 
--显示表分区信息 显示数据库所有分区表的详细分区信息:
select * from dba_tab_partitions
 
--显示当前用户可访问的所有分区表的详细分区信息:
select * from all_tab_partitions
 
--显示当前用户所有分区表的详细分区信息:
select * from user_tab_partitions
 
--显示子分区信息 显示数据库所有组合分区表的子分区信息:
select * from dba_tab_subpartitions
 
--显示当前用户可访问的所有组合分区表的子分区信息:
select * from all_tab_subpartitions
 
--显示当前用户所有组合分区表的子分区信息:
select * from user_tab_subpartitions
 
--显示分区列 显示数据库所有分区表的分区列信息:
select * from dba_part_key_columns
 
--显示当前用户可访问的所有分区表的分区列信息:
select * from all_part_key_columns
 
--显示当前用户所有分区表的分区列信息:
select * from user_part_key_columns
 
--显示子分区列 显示数据库所有分区表的子分区列信息:
select * from dba_subpart_key_columns
 
--显示当前用户可访问的所有分区表的子分区列信息:
select * from all_subpart_key_columns
 
--显示当前用户所有分区表的子分区列信息:
select * from user_subpart_key_columns
 
--怎样查询出oracle数据库中所有的的分区表
select * from user_tables a where a.partitioned='yes'
 
--删除一个表的数据是
truncate table table_name;
 
--删除分区表一个分区的数据是
alter table table_name truncate partition p5;

猜你喜欢

转载自www.cnblogs.com/sunziying/p/8972392.html