Thomas Zhang的杂货铺
17 12, 2007
Oracle 11g Reference Partitioning
作者 tomszrp 19:08 | Permalink 静态链接网址 | Comments 最新回复 (0) | Trackback 引用 (0) | 磨刀石

摘录部分我的笔记的中doc,和大家一起感受Oracle 11g在分区方面的增强--引用分区

引用分区,也有人叫外键分区。

引用分区的特点

●外键表可根据主键表来分区
外键表不需要包含分区键值
●外键分区可与主键分区保持一定的关系
●主键与外键的连接会是分区感知的
●能很好的进行数据生命周期管理(ILM)

引用分区的适用场景

假设系统中有这样2个表:sales和customers

create table customers
(
  cust_id number primary key,
  cust_name varchar2(64),
  rating varchar2(1) not null
)
partition by list(rating)
(
  partition p_1 values('A'),
  partition p_2 values('B')
);

create table sales
(
  sales_id number primary key,
  cust_id number not null,
  sales_amt number,
  constraint fk_sales
  foreign key(cust_id)
  references customers
);
一般情况下,我们都会期望采用相同的方式对sales和customers表分区,比如都通过rating列进行list分区。但现在的情况是:sales表中没有名为
rating这样的列(减少冗余),那么就提出这样一个问题,能否根据这个不存在的列进行分区呢?

这个问题,在Oracle 11g之前,是没有办法来解决的,我们只能采用其他的方式来规避实现我们的目标,在11g中,我们可以使用Reference分区的特性,下面的示例就是针对上面的问题的一个解决方法(11g)

Connected to Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 
Connected as study

SQL> create table customers
  2  (
  3    cust_id number primary key,
  4    cust_name varchar2(64),
  5    rating varchar2(1) not null
  6  )
  7  partition by list(rating)
  8  (
  9    partition p_1 values('A'),
 10    partition p_2 values('B')
 11  );

Table created

SQL> create table sales
  2  (
  3    sales_id number primary key,
  4    cust_id number not null,
  5    sales_amt number,
  6    constraint fk_sales
  7    foreign key(cust_id)
  8    references customers
  9  )
 10  partition by reference(fk_sales);

Table created

SQL> 
SQL> select table_name,partitioning_type,partition_count
  2  from user_part_tables
  3  where table_name='CUSTOMERS';

TABLE_NAME        PARTITIONING_TYPE PARTITION_COUNT
----------------- ----------------- ---------------
CUSTOMERS         LIST                            2

SQL> select table_name,partition_name,high_value
  2  from user_tab_partitions
  3  where table_name='CUSTOMERS';

TABLE_NAME      PARTITION_NAME      HIGH_VALUE
--------------- ------------------- -----------------
CUSTOMERS       P_1                 'A'
CUSTOMERS       P_2                 'B'

SQL> select table_name,partitioning_type,partition_count,ref_ptn_constraint_name
  2  from user_part_tables
  3  where table_name='SALES';

TABLE_NAME    PARTITIONING_TYPE PARTITION_COUNT REF_PTN_CONSTRAINT_NAME
------------- ----------------- --------------- ---------------------------
SALES         REFERENCE                       2 FK_SALES

SQL> select table_name,partition_name
  2  from user_tab_partitions
  3  where table_name='SALES';

TABLE_NAME      PARTITION_NAME
--------------- ------------------
SALES           P_1
SALES           P_2

SQL> insert into customers values(1001,'Oracle','A');
1 row inserted

SQL> insert into customers values(1002,'IBM','B');
1 row inserted

SQL> commit;
Commit complete

SQL> insert into sales values(101,1001,1);
1 row inserted

SQL> insert into sales values(102,1001,2);
1 row inserted

SQL> insert into sales values(103,1002,1);
1 row inserted

SQL> commit;
Commit complete

SQL> select * from sales partition (p_1);

  SALES_ID    CUST_ID  SALES_AMT
---------- ---------- ----------
       101       1001          1
       102       1001          2

SQL> select * from sales partition (p_2);

  SALES_ID    CUST_ID  SALES_AMT
---------- ---------- ----------
       103       1002          1

SQL> 

Comments
博客日历
« 八月 2008 »
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
搜索
最新发表
文章分类
文章归档
网站链接
新闻聚合