分区表
将已存在数据的普通表转变为分区表,没有办法通过修改属性的方式直接转化为分区表,必须通过重建的方式进行转变,一般可以有三种方法,视不同场景使用:
用例:
方法一:利用原表重建分区表。
CREATE TABLE T (ID NUMBER PRIMARY KEY, TIME DATE);
INSERT INTO T
SELECT ROWNUM, SYSDATE - ROWNUM FROM DBA_OBJECTS WHERE ROWNUM <= 5000;
COMMIT;
CREATE TABLE T_NEW (ID, TIME) PARTITION BY RANGE (TIME)
(PARTITION P1 VALUES LESS THAN (TO_DATE('2000-1-1', 'YYYY-MM-DD')),
PARTITION P2 VALUES LESS THAN (TO_DATE('2002-1-1', 'YYYY-MM-DD')),
PARTITION P3 VALUES LESS THAN (TO_DATE('2005-1-1', 'YYYY-MM-DD')),
PARTITION P4 VALUES LESS THAN (MAXVALUE))
AS SELECT ID, TIME FROM T;
RENAME T TO T_OLD;
RENAME T_NEW TO T;
SELECT COUNT(*) FROM T;
COUNT(*)
----------
5000
SELECT COUNT(*) FROM T PARTITION (P1);
COUNT(*)
----------
2946
SELECT COUNT(*) FROM T PARTITION (P2);
COUNT(*)
----------
731
SELECT COUNT(*) FROM T PARTITION (P3);
COUNT(*)
----------
1096
优点:方法简单易用,由于采用DDL语句,不会产生UNDO,且只产生少量REDO,效率相对较高,而且建表完成后数据已经在分布到各个分区中了。
不足:对于数据的一致性方面还需要额外的考虑。由于几乎没有办法通过手工锁定T表的方式保证一致性,在执行CREATE TABLE语句和RENAME T_NEW TO T语句直接的修改可能会丢
失,如果要保证一致性,需要在执行完语句后对数据进行检查,而这个代价是比较大的。另外在执行两个RENAME语句之间执行的对T的访问会失败。
适用于修改不频繁的表,在闲时进行操作,表的数据量不宜太大。
方法二:使用交换分区的方法。
Drop table t;
CREATE TABLE T (ID NUMBER PRIMARY KEY, TIME DATE);
INSERT INTO T
SELECT ROWNUM, SYSDATE - ROWNUM FROM DBA_OBJECTS WHERE ROWNUM <= 5000;
COMMIT;
CREATE TABLE T_NEW (ID NUMBER PRIMARY KEY, TIME DATE)
PARTITION BY RANGE (TIME)
(
PARTITION P1 VALUES LESS THAN (TO_DATE('2005-9-1', 'YYYY-MM-DD')),
PARTITION P2 VALUES LESS THAN (MAXVALUE)
);
ALTER TABLE T_NEW EXCHANGE PARTITION P1 WITH TABLE T;
RENAME T TO T_OLD;
RENAME T_NEW TO T;
优点:只是对数据字典中分区和表的定义进行了修改,没有数据的修改或复制,效率最高。如果对数据在分区中的分布没有进一步要求的话,实现比较简单。在执行完RENAME操作
后,可以检查T_OLD中是否存在数据,如果存在的话,直接将这些数据插入到T中,可以保证对T插入的操作不会丢失。
不足:仍然存在一致性问题,交换分区之后RENAME T_NEW TO T之前,查询、更新和删除会出现错误或访问不到数据。如果要求数据分布到多个分区中,则需要进行分区的SPLIT操
作,会增加操作的复杂度,效率也会降低。
适用于包含大数据量的表转到分区表中的一个分区的操作。应尽量在闲时进行操作。
分区管理:
增加分区:
alter table t add partition P5 VALUES LESS THAN (TO_DATE('2007-1-1', 'YYYY-MM-DD'));
如果已有maxvalue分区,不能增加分区,可以采取分裂分区的办法增加分区!
tstuser@RMIS> alter table t add partition P5 VALUES LESS THAN (TO_DATE('2007-1-1
', 'YYYY-MM-DD'));
alter table t add partition P5 VALUES LESS THAN (TO_DATE('2007-1-1', 'YYYY-MM-DD
'))
*
ERROR at line 1:
ORA-14074: partition bound must collate higher than that of the last partition
分裂分区:
alter table t split partition p4 at (TO_DATE('2007-1-1', 'YYYY-MM-DD')) into (partition p4,partition p5);
查看分区信息:
select a.table_name,a.partitioning_type,a.partition_count,a.partitioning_key_count from user_part_tables a ;
select a.table_name,a.partition_name,a.high_value from user_tab_partitions a where a.table_name='T';
截短分区:
alter table T truncate partition P5;
合并分区:
alter table t merge partitions p3, p4 into partition p4;
删除分区:
ALTER TABLE t DROP PARTITION p5;
测试过程:
tstuser@RMIS> CREATE TABLE T (ID NUMBER PRIMARY KEY, TIME DATE);
Table created.
tstuser@RMIS> INSERT INTO T
2 SELECT ROWNUM, SYSDATE - ROWNUM FROM DBA_OBJECTS WHERE ROWNUM <= 5000;
5000 rows created.
tstuser@RMIS> COMMIT;
Commit complete.
tstuser@RMIS> CREATE TABLE T_NEW (ID, TIME) PARTITION BY RANGE (TIME)
2 (PARTITION P1 VALUES LESS THAN (TO_DATE('2000-1-1', 'YYYY-MM-DD')),
3 PARTITION P2 VALUES LESS THAN (TO_DATE('2002-1-1', 'YYYY-MM-DD')),
4 PARTITION P3 VALUES LESS THAN (TO_DATE('2005-1-1', 'YYYY-MM-DD')),
5 PARTITION P4 VALUES LESS THAN (MAXVALUE))
6 AS SELECT ID, TIME FROM T;
Table created.
tstuser@RMIS> RENAME T TO T_OLD;
RENAME T TO T_OLD
*
ERROR at line 1:
ORA-00955: name is already used by an existing object
tstuser@RMIS> drop table t_old;
Table dropped.
tstuser@RMIS> RENAME T TO T_OLD;
Table renamed.
tstuser@RMIS> RENAME T_NEW TO T;
Table renamed.
tstuser@RMIS> SELECT COUNT(*) FROM T;
COUNT(*)
----------
5000
tstuser@RMIS> SELECT COUNT(*) FROM T PARTITION (P1);
COUNT(*)
----------
1574
tstuser@RMIS> SELECT COUNT(*) FROM T PARTITION (P2);
COUNT(*)
----------
731
tstuser@RMIS> SELECT COUNT(*) FROM T PARTITION (P3);
COUNT(*)
----------
1096
tstuser@RMIS> SELECT COUNT(*) FROM T PARTITION (P4);
COUNT(*)
----------
1599
tstuser@RMIS> alter table t add partition P5 VALUES LESS THAN (TO_DATE('2007-1-1', 'YYYY-MM-DD'));
alter table t add partition P5 VALUES LESS THAN (TO_DATE('2007-1-1', 'YYYY-MM-DD'))
*
ERROR at line 1:
ORA-14074: partition bound must collate higher than that of the last partition
tstuser@RMIS> select a.table_name,a.partitioning_type,a.partition_count,a.partitioning_key_count from user_part_tables a ;
TABLE_NAME PARTITIONING_T PARTITION_COUNT PARTITIONING_KEY_COUNT
------------------------------------------------------------ -------------- --------------- ----------------------
T RANGE 4 1
TEST_PARTION RANGE 5 1
tstuser@RMIS> select a.table_name,a.partition_name,a.high_value from user_tab_partitions a where a.table_name='T';
TABLE_NAME
------------------------------------------------------------
PARTITION_NAME
------------------------------------------------------------
HIGH_VALUE
--------------------------------------------------------------------------------
T
P1
TO_DATE(' 2000-01-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA
N')
T
P2
TO_DATE(' 2002-01-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA
N')
T
P3
TO_DATE(' 2005-01-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA
N')
T
P4
MAXVALUE
tstuser@RMIS> alter table t split partition p4 at (TO_DATE('2007-1-1', 'YYYY-MM-DD')) into (partition p4,partition p5);
Table altered.
tstuser@RMIS> SELECT COUNT(*) FROM T PARTITION (P4);
COUNT(*)
----------
730
tstuser@RMIS> SELECT COUNT(*) FROM T PARTITION (P5);
COUNT(*)
----------
869
tstuser@RMIS> alter table T truncate partition P5;
Table truncated.
tstuser@RMIS> SELECT COUNT(*) FROM T PARTITION (P5);
COUNT(*)
----------
0
tstuser@RMIS> alter table t merge partitions p3, p4 into partition p4;
Table altered.
tstuser@RMIS> SELECT COUNT(*) FROM T PARTITION (P3);
SELECT COUNT(*) FROM T PARTITION (P3)
*
ERROR at line 1:
ORA-02149: Specified partition does not exist
tstuser@RMIS> SELECT COUNT(*) FROM T PARTITION (P4);
COUNT(*)
----------
1826
tstuser@RMIS> ALTER TABLE t DROP PARTITION p5;
Table altered.
tstuser@RMIS> alter table t add partition P5 VALUES LESS THAN (TO_DATE('2007-1-1', 'YYYY-MM-DD'));
alter table t add partition P5 VALUES LESS THAN (TO_DATE('2007-1-1', 'YYYY-MM-DD'))
*
ERROR at line 1:
ORA-14074: partition bound must collate higher than that of the last partition
tstuser@RMIS> alter table t add partition P5 VALUES LESS THAN (TO_DATE('2008-1-1', 'YYYY-MM-DD'));
Table altered.
tstuser@RMIS> spool off;
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8102208/viewspace-600421/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/8102208/viewspace-600421/