ITPub博客

首页 > 数据库 > Oracle > 创建分区表一

创建分区表一

原创 Oracle 作者:skyin_1603 时间:2016-10-15 21:30:20 0 删除 编辑
对于一个数据量非常大的表来说,将它转化成分区表时很有必要的,已达到一些优化效果。
很容易理解,如果一张表,包括了不同地区的数据,或者表按照年月日为日期单位的数据量巨大,
在没有创建分区表的情况下,查询数据时如此的费劲耗时。

以下是讲述有关分区表的一些内容,包括分区表的分类,与索引的区别,分区管理,创建分区表等。

1、创建测试单表:
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.

2、给测试表创建索引:
SQL> create index idx_test on test(idcard);

Index created.

3、往表中插入数据:
SQL> insert into test select rownum,rpad(rownum,18,'0'),add_months(sysdate,0-rownum) from dual connect by rownum<=1000;

1000 rows created.

4、创建分区表:
SQL> create table test_p
  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 part_maxvalue values less than (maxvalue)
 10  );

Table created.

5、给分区表创建本地索引:
SQL> create index idx_test_p on test_p(idcard) local;

Index created.

6、交换分区:
SQL> alter table test_p exchange partition part_maxvalue with table test;

Table altered.

SQL> lock table test_p in EXCLUSIVE mode;

Table(s) Locked.

SQL> alter table test_p split partition part_maxvalue at (to_date('1940-01-01','yyyy-mm-dd')) into (partition part1940,partition part_maxvalue);
Table altered.

SQL> alter table test_p split partition part_maxvalue at (to_date('1960-01-01','yyyy-mm-dd')) into (partition part1960,partition part_maxvalue);
Table altered.

alter table test_p split partition part_maxvalue at (to_date('1980-01-01','yyyy-mm-dd')) into (partition part1980,partition part_maxvalue);
Table altered.

alter table test_p split partition part_maxvalue at (to_date('2000-01-01','yyyy-mm-dd')) into (partition part2000,partition part_maxvalue);
Table altered.


SQL>  alter table test_p split partition part_maxvalue at (to_date('2020-01-01','yyyy-mm-dd')) into (partition part2020,partition part_maxvalue);
Table altered.

7、给分区表重建索引,因在分区表是索引失效:
SQL> alter index idx_test_p rebuild partition part1940 tablespace users nologging online;
alter index idx_test_p rebuild partition part1960 tablespace users nologging online;
alter index idx_test_p rebuild partition part1980 tablespace users nologging online;
alter index idx_test_p rebuild partition part2000 tablespace users nologging online;
alter index idx_test_p rebuild partition part2020 tablespace users nologging online;
alter index pk_test_p rebuild tablespace users nologging online;

设置不生成日志文件。

8、查看分区表数据:
select * from test_p;
      NID IDCARD             PDD
---------- ------------------ ---------
       181 181000000000000000 11-SEP-01
       182 182000000000000000 11-AUG-01
       183 183000000000000000 11-JUL-01
       184 184000000000000000 11-JUN-01
       185 185000000000000000 11-MAY-01
       186 186000000000000000 11-APR-01
       187 187000000000000000 11-MAR-01
       188 188000000000000000 11-FEB-01
       189 189000000000000000 11-JAN-01
       190 190000000000000000 11-DEC-00
       191 191000000000000000 11-NOV-00


       NID IDCARD             PDD
---------- ------------------ ---------
       192 192000000000000000 11-OCT-00
       193 193000000000000000 11-SEP-00
       194 194000000000000000 11-AUG-00
       195 195000000000000000 11-JUL-00
       196 196000000000000000 11-JUN-00
       197 197000000000000000 11-MAY-00
       198 198000000000000000 11-APR-00
       199 199000000000000000 11-MAR-00
       200 200000000000000000 11-FEB-00
       201 201000000000000000 11-JAN-00
... ...
1000 rows selected.

在这里截取部分数据,没有看到分区表的效果。按照这个分区表的分区设置,数据应该是按照分区设置
同一个日期范围的数据放在同一个分区。


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31392094/viewspace-2126477/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 管理日志文件
下一篇: 创建分区表二
请登录后发表评论 登录
全部评论

注册时间:2016-09-06

  • 博文量
    231
  • 访问量
    215114