|
摘录部分我的笔记的中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>
|