设为首页 加入收藏

TOP

Oracle 11g新特性:引用分区(reference partitioning)(一)
2017-01-24 08:15:11 】 浏览:365
Tags:Oracle 11g 特性 引用 分区 reference partitioning

现在来创建ORDER_LINE_ITEMS表,并插入一些数据指向ORDERS表:
zx@ORCL>create table order_line_items
? 2? (?
? 3? ? order#? ? ? number,
? 4? ? line#? ? ? number,
? 5? ? order_date? date, -- manually copied from ORDERS!
? 6? ? data? ? ? varchar2(30),
? 7? ? constraint c1_pk primary key(order#,line#),
? 8? ? constraint c1_fk_p foreign key(order#) references orders
? 9? )
?10? enable row movement
?11? PARTITION BY RANGE (order_date)
?12? (
?13? ? PARTITION part_2016 VALUES LESS THAN (to_date('01-01-2017','dd-mm-yyyy')) ,
?14? ? PARTITION part_2017 VALUES LESS THAN (to_date('01-01-2018','dd-mm-yyyy'))?
?15? )
?16? /
?
Table created.
?
zx@ORCL>insert into order_line_items values
? 2? ( 1, 1, to_date( '01-jun-2016', 'dd-mon-yyyy' ), 'yyy' );
?
1 row created.
?
zx@ORCL>insert into order_line_items values
? 2? ( 2, 1, to_date( '01-jun-2017', 'dd-mon-yyyy' ), 'yyy' );
?
1 row created.
?
zx@ORCL>commit;
?
Commit complete.


现在如果要删除包含2016年数据的ORDER_LINE_ITEMS分区,也可以删除对应2016年的ORDERS分区而不会违反引用完整性约束。尽管我们都很清楚这一点,但数据库并不知道:
zx@ORCL>alter table order_line_items drop partition part_2016;
?
Table altered.
?
zx@ORCL>alter table orders? ? ? ? ? drop partition part_2016;
alter table orders? ? ? ? ? drop partition part_2016
*
ERROR at line 1:
ORA-02266: unique/primary keys in table referenced by enabled foreign keys


所以,对数据逆规范化的做活很笨拙,会耗费资源,而且可能破坏数据的完整性。不仅如此,它还会妨碍管理分区表时经常需要做的一项工作:清除老信息。


下面来看引用分区。采用引用分区,子表会继承父表的分区机制,而不必对分区键逆规范化,而且更重要的是,它会让数据库了解这个子表与父表之间存在对等分区特点。也就是说,截除或删除生意人子表分区时,也能删除或截除父表分区。
要重新实现前面的例子,语法很简单,如下所示,这里将重用现胡的你表ORDERS,只需要截除这个表:
zx@ORCL>drop table order_line_items cascade constraints;
?
Table dropped.
?
zx@ORCL>truncate table orders;
?
Table truncated.
?
zx@ORCL>insert into orders values
? 2? ( 1, to_date( '01-jun-2016', 'dd-mon-yyyy' ), 'xxx' );
?
1 row created.
?
zx@ORCL>insert into orders values
? 2? ( 2, to_date( '01-jun-2017', 'dd-mon-yyyy' ), 'xxx' );
?
1 row created.
?
zx@ORCL>commit;
?
Commit complete.


创建一个新的子表:
zx@ORCL>create table order_line_items
? 2? (?
? 3? ? order#? ? ? number NOT NULL,
? 4? ? line#? ? ? number NOT NULL,
? 5? ? data? ? ? varchar2(30),
? 6? ? constraint c1_pk primary key(order#,line#),
? 7? ? constraint c1_fk_p foreign key(order#) references orders
? 8? )
? 9? enable row movement
?10? partition by reference(c1_fk_p)
?11? /
?
Table created.
?
zx@ORCL>insert into order_line_items values
? 2? ( 1, 1, 'yyy' );
?
1 row created.
?
zx@ORCL>insert into order_line_items values
? 2? ( 2, 1, 'yyy' );
?
1 row created.
?
zx@ORCL>commit;
?
Commit complete.


神奇之处就在CREATE TABLE语句的第10行。在这里,我们将区间分区语句替换为PARTITION BY REFERENCE。
这允许我们指定要使用的外键约束,从而发现分区机制。在这里可以看到外键指向ORDERS表——数据库读取ORDERS表的结构,并发现它有两个分区。因此,子表会有两个分区。实际上,如果现在查询数据字典可以得到:
zx@ORCL>set linesize 200
zx@ORCL>col table for a20
zx@ORCL>col partition_name for a20
zx@ORCL>select table_name, partition_name
? 2? ? from user_tab_partitions
? 3? where table_name in ( 'ORDERS', 'ORDER_LINE_ITEMS' )
? 4? order by table_name, partition_name
? 5? /
?
TABLE_NAME? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? PARTITION_NAME
------------------------------------------------------------------------------------------ --------------------
ORDERS? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? PART_2016
ORDERS? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? PART_2017
ORDER_LINE_ITEMS? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? PART_2016
ORDER_LINE_ITEMS? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? PART_2017


可以看到两个表的结构完全相同。另外,由于数据库知道这两个表是相关联的,可以删除父表分区,并让它自动清除相关的子表分区(因为子表从父

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Linux下搭建MongoDB环境笔记 下一篇使用sysbench对MySQL压力测试

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目