创建测试用分区表test_par
——选择范围分区
点击(此处)折叠或打开
-
SQL> create table test_par
-
2 (
-
3 nid number(10) constraint pk_test_p primary key,
-
4 idcard varchar2(18),
-
5 pdd date not null
-
6 )
-
7 partition by range (pdd)
-
8 (
-
9 partition par_2000 values less than (to_date('2001-01-01','yyyy-mm-dd')),
-
10 partition par_2001 values less than (to_date('2002-01-01','yyyy-mm-dd')),
-
11 partition par_2002 values less than (to_date('2003-01-01','yyyy-mm-dd')),
-
12 partition part_maxvalue values less than (maxvalue)
-
13 );
-
-
Table created.
- SQL>
创建全局索引
点击(此处)折叠或打开
-
SQL> create index idx_test_par on test_par(idcard);
-
-
Index created.
-
- SQL>
在不同的分区中插入测试用的数据
点击(此处)折叠或打开
-
SQL> insert into test_par values(500,'2000',to_date('20000101','yyyymmdd'));
-
-
1 row created.
-
-
SQL> insert into test_par values(501,'2001',to_date('20010201','yyyymmdd'));
-
-
1 row created.
-
-
SQL> insert into test_par values(502,'2002',to_date('20020301','yyyymmdd'));
-
-
1 row created.
-
-
SQL>
-
SQL> commit;
-
-
Commit complete.
-
- SQL>
查看索引,当前索引有效
点击(此处)折叠或打开
- SQL> select INDEX_NAME,STATUS from user_indexes where index_name='IDX_TEST_PAR';
INDEX_NAME STATUS
----------------------------- --------
IDX_TEST_PAR VALID
创建与分区表表结构相同的测试用表test,用于测试交换分区
点击(此处)折叠或打开
-
SQL> create table test
-
2 (
-
3 nid number(10) constraint pk_test primary key,
-
4 idcard varchar2(18),
-
5 pdd date not null
-
6 );
-
-
Table created.
-
- SQL>
向测试用test中插入数据
——数据范围选择日期为分区表中的par_2002分区中的日期范围
点击(此处)折叠或打开
-
SQL> insert into test select rownum,rpad(rownum,18,'0'),to_date('20020101','yyyymmdd')+rownum from dual connect by rownum<=100;
-
-
100 rows created.
-
-
SQL> commit;
-
-
Commit complete.
-
- SQL>
通过查看插入数据最大值和最小值,确认数据是否符合par_2002分区范围
点击(此处)折叠或打开
- SQL> select min(pdd),max(pdd) from test;
MIN(PDD) MAX(PDD)
--------- ---------
02-JAN-02 11-APR-02
分别确认test表和分区表test_par表的数据总数
点击(此处)折叠或打开
- SQL> select count(*) from test;
COUNT(*)
----------
100
点击(此处)折叠或打开
- SQL> select count(*) from test_par;
COUNT(*)
----------
3
SQL>
交换分区
——交换par_2002分区
点击(此处)折叠或打开
-
SQL> alter table test_par exchange partition par_2002 with table test;
-
-
Table altered.
-
- SQL>
验证
——查看test表中的数据,显示为之前par_2002分区中的一条记录
点击(此处)折叠或打开
- SQL> select * from test;
NID IDCARD PDD
--------- --------------- ---------
502 2002 01-MAR-02
——查看test_par中已经有102条记录,其中单独查看par_2002分区数据为100条
点击(此处)折叠或打开
- SQL> select count(*) from test_par;
COUNT(*)
----------
102
点击(此处)折叠或打开
- SQL> select count(*) from test_par partition (par_2002);
COUNT(*)
----------
100
注:交换分区后,全局索引会失效
点击(此处)折叠或打开
- SQL> select INDEX_NAME,STATUS from user_indexes where index_name='IDX_TEST_PAR';
INDEX_NAME STATUS
----------------------------- --------
IDX_TEST_PAR UNUSABLE
——如果使用的是本地索引,则交换的分区索引失效,其它正常
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/31399196/viewspace-2126132/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/31399196/viewspace-2126132/