SQL> CREATE TABLE T (ID NUMBER PRIMARY KEY, TIME DATE);
Table created
SQL> INSERT INTO T SELECT ROWNUM, CREATED FROM DBA_OBJECTS;
52269 rows inserted
SQL> commit;
Commit complete
SQL>
SQL> CREATE TABLE T_NEW (ID, TIME) PARTITION BY RANGE (TIME)
2 (PARTITION P1 VALUES LESS THAN (TO_DATE('2004-7-1', 'YYYY-MM-DD')),
3 PARTITION P2 VALUES LESS THAN (TO_DATE('2006-7-1', 'YYYY-MM-DD')),
4 PARTITION P3 VALUES LESS THAN (TO_DATE('2008-7-1', 'YYYY-MM-DD')),
5 PARTITION P4 VALUES LESS THAN (MAXVALUE))
6 AS SELECT ID, TIME FROM T;
Table created
SQL> RENAME T TO T_OLD;
Table renamed
SQL> RENAME T_NEW TO T;
Table renamed
SQL> SELECT COUNT(*) FROM T;
COUNT(*)
----------
52269
SQL> SELECT COUNT(*) FROM T PARTITION (P1);
COUNT(*)
----------
0
SQL> SELECT COUNT(*) FROM T PARTITION (P2);
COUNT(*)
----------
49697
SQL> SELECT COUNT(*) FROM T PARTITION (P3);
COUNT(*)
----------
0
SQL> SELECT COUNT(*) FROM T PARTITION (P4);
COUNT(*)
----------
2572
优点:方法简单易用,由于采用DDL语句,不会产生UNDO,且只产生少量REDO,效率相对较高,而且建表完成后数据已经在分布到各个分区中了。
不足:对于数据的一致性方面还需要额外的考虑。由于几乎没有办法通过手工锁定T表的方式保证一致性,在执行CREATE TABLE语句和RENAME T_NEW TO T语句直接的修改可能会丢失,如果要保证一致性,需要在执行完语句后对数据进行检查,而这个代价是比较大的。另外在执行两个RENAME语句之间执行的对T的访问会失败。
适用于修改不频繁的表,在闲时进行操作,表的数据量不宜太大。