Oracle12c:自动分区表

Posted yy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Oracle12c:自动分区表相关的知识,希望对你有一定的参考价值。

为什么要创建oracle分区表?

一般情况下,如果不分区,则每次查询的对象都是一整张表,如果采用了表分区,那么可以根据具体的分区字段当作条件来避免扫描整张表,减少IO的扫描以提高表的查询速度。

新建(按照日期自动分区)分区表

SQL> create table test_partion(
  2  pk_id number(38) generated as identity (start with 1 increment by 1),
  3  P_day date,
  4  words varchar2(200),
  5  constraint pk_test_partition_id primary key (pk_id)
  6  )
  7  partition by range(P_day)
  8  interval (numtodsinterval(1,\'day\'))
  9  (
 10  partition part_20170622 values less than (to_date(\'20170622\',\'yyyyMMdd\'))
 11  );
Table created
  • 尝试插入数据,之后查看动态分区表的分区个数:
SQL> select * from test_partion ;
                                  PK_ID P_DAY       WORDS
--------------------------------------- ----------- ------
SQL> insert into test_partion (p_day,words) values (to_date(\'20170622\',\'yyyyMMdd\'),\'00001\');
1 row inserted
SQL> insert into test_partion (p_day,words) values (to_date(\'20170623\',\'yyyyMMdd\'),\'00002\');
1 row inserted
SQL> insert into test_partion (p_day,words) values (to_date(\'20170623\',\'yyyyMMdd\'),\'00003\');
1 row inserted
SQL> insert into test_partion (p_day,words) values (to_date(\'20170624\',\'yyyyMMdd\'),\'00004\');
1 row inserted
SQL> insert into test_partion (p_day,words) values (to_date(\'20170623\',\'yyyyMMdd\'),\'00005\');
1 row inserted
SQL> insert into test_partion (p_day,words) values (to_date(\'20170625\',\'yyyyMMdd\'),\'00006\');
1 row inserted
SQL> insert into test_partion (p_day,words) values (to_date(\'20170626\',\'yyyyMMdd\'),\'00006\');
1 row inserted
SQL> insert into test_partion (p_day,words) values (to_date(\'20170627\',\'yyyyMMdd\'),\'00006\');
1 row inserted
SQL> insert into test_partion (p_day,words) values (to_date(\'20170628\',\'yyyyMMdd\'),\'00006\');
1 row inserted
SQL> insert into test_partion (p_day,words) values (to_date(\'20170629\',\'yyyyMMdd\'),\'00006\');
1 row inserted
SQL> select table_name,partition_name,high_value from user_tab_partitions where table_name=\'TEST_PARTION\';
TABLE_NAME      PARTITION_NAME   HIGH_VALUE
--------------- ---------------- --------------------------------------------------------------------------------
TEST_PARTION    PART_20170622    TO_DATE(\' 2017-06-22 00:00:00\', \'SYYYY-MM-DD HH24:MI:SS\', \'NLS_CALENDAR=GREGORIA
TEST_PARTION    SYS_P2252        TO_DATE(\' 2017-06-23 00:00:00\', \'SYYYY-MM-DD HH24:MI:SS\', \'NLS_CALENDAR=GREGORIA
TEST_PARTION    SYS_P2253        TO_DATE(\' 2017-06-24 00:00:00\', \'SYYYY-MM-DD HH24:MI:SS\', \'NLS_CALENDAR=GREGORIA
TEST_PARTION    SYS_P2254        TO_DATE(\' 2017-06-25 00:00:00\', \'SYYYY-MM-DD HH24:MI:SS\', \'NLS_CALENDAR=GREGORIA
TEST_PARTION    SYS_P2255        TO_DATE(\' 2017-06-26 00:00:00\', \'SYYYY-MM-DD HH24:MI:SS\', \'NLS_CALENDAR=GREGORIA
TEST_PARTION    SYS_P2256        TO_DATE(\' 2017-06-27 00:00:00\', \'SYYYY-MM-DD HH24:MI:SS\', \'NLS_CALENDAR=GREGORIA
TEST_PARTION    SYS_P2257        TO_DATE(\' 2017-06-28 00:00:00\', \'SYYYY-MM-DD HH24:MI:SS\', \'NLS_CALENDAR=GREGORIA
TEST_PARTION    SYS_P2258        TO_DATE(\' 2017-06-29 00:00:00\', \'SYYYY-MM-DD HH24:MI:SS\', \'NLS_CALENDAR=GREGORIA
TEST_PARTION    SYS_P2259        TO_DATE(\' 2017-06-30 00:00:00\', \'SYYYY-MM-DD HH24:MI:SS\', \'NLS_CALENDAR=GREGORIA
9 rows selected

从上边表中我们基本可以看出,除了分区PART_20170622是我们动态创建的分区外,其他SYS_*分区都是动态创建的。

接下来查看每个分区表的数据分区情况:

SQL> select * from test_partion partition(PART_20170622);
                                  PK_ID P_DAY       WORDS
--------------------------------------- ----------- --------

SQL> select * from test_partion partition(SYS_P2252);
                                  PK_ID P_DAY       WORDS
--------------------------------------- ----------- --------
                                      1 2017/6/22   00001

SQL> select * from test_partion partition(SYS_P2253);
                                  PK_ID P_DAY       WORDS
--------------------------------------- ----------- --------
                                      2 2017/6/23   00002
                                      3 2017/6/23   00003
                                      5 2017/6/23   00005

SQL> select * from test_partion partition(SYS_P2254);
                                  PK_ID P_DAY       WORDS
--------------------------------------- ----------- -------
                                      4 2017/6/24   00004

SQL> select * from test_partion partition(SYS_P2255);
                                  PK_ID P_DAY       WORDS
--------------------------------------- ----------- -------
                                      6 2017/6/25   00006

SQL> select * from test_partion partition(SYS_P2256);
                                  PK_ID P_DAY       WORDS
--------------------------------------- ----------- -------
                                      7 2017/6/26   00006

SQL> select * from test_partion partition(SYS_P2257);
                                  PK_ID P_DAY       WORDS
--------------------------------------- ----------- -------
                                      8 2017/6/27   00006

SQL> select * from test_partion partition(SYS_P2258);
                                  PK_ID P_DAY       WORDS
--------------------------------------- ----------- -------
                                      9 2017/6/28   00006

SQL> select * from test_partion partition(SYS_P2259);
                                  PK_ID P_DAY       WORDS
--------------------------------------- ----------- ------
                                     10 2017/6/29   00006

分区操作:

1)删除某个分区表中的数据

SQL> delete from test_partion partition(SYS_P2259);
1 row deleted

SQL> select * from test_partion partition(SYS_P2259);
                                  PK_ID P_DAY       WORDS
--------------------------------------- ----------- ------

2)删除某个分区

SQL> alter table test_partion drop partition SYS_P2259;
Table altered

 删除分区之后,查看表分区情况。

SQL> select table_name,partition_name,high_value from user_tab_partitions where table_name=\'TEST_PARTION\';
TABLE_NAME      PARTITION_NAME   HIGH_VALUE
--------------- ---------------- --------------------------------------------------------------------------------
TEST_PARTION    PART_20170622    TO_DATE(\' 2017-06-22 00:00:00\', \'SYYYY-MM-DD HH24:MI:SS\', \'NLS_CALENDAR=GREGORIA
TEST_PARTION    SYS_P2252        TO_DATE(\' 2017-06-23 00:00:00\', \'SYYYY-MM-DD HH24:MI:SS\', \'NLS_CALENDAR=GREGORIA
TEST_PARTION    SYS_P2253        TO_DATE(\' 2017-06-24 00:00:00\', \'SYYYY-MM-DD HH24:MI:SS\', \'NLS_CALENDAR=GREGORIA
TEST_PARTION    SYS_P2254        TO_DATE(\' 2017-06-25 00:00:00\', \'SYYYY-MM-DD HH24:MI:SS\', \'NLS_CALENDAR=GREGORIA
TEST_PARTION    SYS_P2255        TO_DATE(\' 2017-06-26 00:00:00\', \'SYYYY-MM-DD HH24:MI:SS\', \'NLS_CALENDAR=GREGORIA
TEST_PARTION    SYS_P2256        TO_DATE(\' 2017-06-27 00:00:00\', \'SYYYY-MM-DD HH24:MI:SS\', \'NLS_CALENDAR=GREGORIA
TEST_PARTION    SYS_P2257        TO_DATE(\' 2017-06-28 00:00:00\', \'SYYYY-MM-DD HH24:MI:SS\', \'NLS_CALENDAR=GREGORIA
TEST_PARTION    SYS_P2258        TO_DATE(\' 2017-06-29 00:00:00\', \'SYYYY-MM-DD HH24:MI:SS\', \'NLS_CALENDAR=GREGORIA
8 rows selected

 注:drop partition时,该分区内存储的数据也将同时删除,你的本意是希望删除掉指定的分区但保留数据,你应该使用merge partition,执行该语句会导致glocal索引的失效需要重建全局索引

3)合并分区

相邻的分区可以通过命令merge partition合并为一个分区,同时分区中的数据也将会被合并到同一个分区中。

新分区的下边界为原来边界值较低的分区,上边界为原来边界值较高的分区,原先的局部索引相应也会合并,全局索引会失效,需要rebuild。

 参考:http://www.cnblogs.com/wangfg/p/5286519.html

SQL> alter table test_partion merge partitions SYS_P2253,SYS_P2254 into partition  SYS_P2253_to_P2254;
Table altered

SQL> select table_name,partition_name,high_value from user_tab_partitions where table_name=\'TEST_PARTION\';
TABLE_NAME                                                                       PARTITION_NAME                                                                   HIGH_VALUE
-------------------------------------------------------------------------------- -------------------------------------------------------------------------------- --------------------------------------------------------------------------------
TEST_PARTION                                                                     PART_20170622                                                                    TO_DATE(\' 2017-06-22 00:00:00\', \'SYYYY-MM-DD HH24:MI:SS\', \'NLS_CALENDAR=GREGORIA
TEST_PARTION                                                                     SYS_P2252                                                                        TO_DATE(\' 2017-06-23 00:00:00\', \'SYYYY-MM-DD HH24:MI:SS\', \'NLS_CALENDAR=GREGORIA
TEST_PARTION                                                                     SYS_P2253_TO_P2254                                                               TO_DATE(\' 2017-06-25 00:00:00\', \'SYYYY-MM-DD HH24:MI:SS\', \'NLS_CALENDAR=GREGORIA
TEST_PARTION                                                                     SYS_P2255                                                                        TO_DATE(\' 2017-06-26 00:00:00\', \'SYYYY-MM-DD HH24:MI:SS\', \'NLS_CALENDAR=GREGORIA
TEST_PARTION                                                                     SYS_P2256                                                                        TO_DATE(\' 2017-06-27 00:00:00\', \'SYYYY-MM-DD HH24:MI:SS\', \'NLS_CALENDAR=GREGORIA
TEST_PARTION                                                                     SYS_P2257                                                                        TO_DATE(\' 2017-06-28 00:00:00\', \'SYYYY-MM-DD HH24:MI:SS\', \'NLS_CALENDAR=GREGORIA
TEST_PARTION                                                                     SYS_P2258                                                                        TO_DATE(\' 2017-06-29 00:00:00\', \'SYYYY-MM-DD HH24:MI:SS\', \'NLS_CALENDAR=GREGORIA
7 rows selected

SQL> select * from test_partion partition(SYS_P2253_TO_P2254);
                                  PK_ID P_DAY       WORDS
--------------------------------------- ----------- --------------------------------------------------------------------------------
                                      2 2017/6/23   00002
                                      3 2017/6/23   00003
                                      5 2017/6/23   00005
                                      4 2017/6/24   00004

SQL> insert into test_partion (p_day,words) values (to_date(\'20170623\',\'yyyyMMdd\'),\'06665\');
insert into test_partion (p_day,words) values (to_date(\'20170623\',\'yyyyMMdd\'),\'06665\')
ORA-01502: 索引 "NETPLAN_ONLINE_APPUSER.PK_TEST_PARTITION_ID" 或这类索引的分区处于不可用状态

SQL> alter index PK_TEST_PARTITION_ID rebuild;
Index altered

SQL> insert into test_partion (p_day,words) values (to_date(\'20170623\',\'yyyyMMdd\'),\'06665\');
1 row inserted

SQL> insert into test_partion (p_day,words) values (to_date(\'20170624\',\'yyyyMMdd\'),\'06665\');
1 row inserted

SQL> select * from test_partion partition(SYS_P2253_TO_P2254);
                                  PK_ID P_DAY       WORDS
--------------------------------------- ----------- --------------------------------------------------------------------------------
                                      2 2017/6/23   00002
                                      3 2017/6/23   00003
                                      5 2017/6/23   00005
                                      4 2017/6/24   00004
                                     12 2017/6/23   06665
                                     13 2017/6/24   06665
6 rows selected

4)分区重命名

SQL> 
SQL> alter table test_partion rename partition SYS_P2253_to_P2254 TO SYS_P2253_to_P2254___;
Table altered

SQL> insert into test_partion (p_day,words) values (to_date(\'20170624\',\'yyyyMMdd\'),\'76665\');
1 row inserted

SQL> select table_name,partition_name,high_value from user_tab_partitions where table_name=\'TEST_PARTION\';
TABLE_NAME                                                                       PARTITION_NAME                                                                   HIGH_VALUE
-------------------------------------------------------------------------------- -------------------------------------------------------------------------------- --------------------------------------------------------------------------------
TEST_PARTION                                                                     PART_20170622                                                                    TO_DATE(\' 2017-06-22 00:00:00\', \'SYYYY-MM-DD HH24:MI:SS\', \'NLS_CALENDAR=GREGORIA
TEST_PARTION                                                                     SYS_P2252                                                                        TO_DATE(\' 2017-06-23 00:00:00\', \'SYYYY-MM-DD HH24:MI:SS\', \'NLS_CALENDAR=GREGORIA
TEST_PARTION                                                                     SYS_P2253_TO_P2254___                                                            TO_DATE(\' 2017-06-25 00:00:00\', \'SYYYY-MM-DD HH24:MI:SS\', \'NLS_CALENDAR=GREGORIA
TEST_PARTION                                                                     SYS_P2255                                                                        TO_DATE(\' 2017-06-26 00:00:00\', \'SYYYY-MM-DD HH24:MI:SS\', \'NLS_CALENDAR=GREGORIA
TEST_PARTION                                                                     SYS_P2256                                                                        TO_DATE(\' 2017-06-27 00:00:00\', \'SYYYY-MM-DD HH24:MI:SS\', \'NLS_CALENDAR=GREGORIA
TEST_PARTION                                                                     SYS_P2257                                                                        TO_DATE(\' 2017-06-28 00:00:00\', \'SYYYY-MM-DD HH24:MI:SS\', \'NLS_CALENDAR=GREGORIA
TEST_PARTION                                                                     SYS_P2258                                                                        TO_DATE(\' 2017-06-29 00:00:00\', \'SYYYY-MM-DD HH24:MI:SS\', \'NLS_CALENDAR=GREGORIA
7 rows selected

SQL> insert into test_partion (p_day,words) values (to_date(\'20170624\',\'yyyyMMdd\'),\'76665\');
1 row inserted

SQL> select * from test_partion partition(SYS_P2253_TO_P2254___);
                                  PK_ID P_DAY       WORDS
--------------------------------------- ----------- --------------------------------------------------------------------------------
                                      2 2017/6/23   00002
                                      3 2017/6/23   00003
                                      5 2017/6/23   00005
                                      4 2017/6/24   00004
                                     12 2017/6/23   06665
                                     13 2017/6/24   06665
                                     14 2017/6/24   76665
                                     15 2017/6/24   76665
8 rows selected

因为这里是动态分区,所以就不介绍怎么添加分区。

 

以上是关于Oracle12c:自动分区表的主要内容,如果未能解决你的问题,请参考以下文章

Oracle 分区和本地 Indexex (12c)

Oracle动态创建时间分区,以及Oracle12c中快速创建自增列

在 oracle sql 12c 中可以绕过分区吗?

如何在Oracle 12C中添加多个分区 (Doc ID 1482456.1)

Oracle 12C 新特性之表分区带 异步全局索引异步维护(一次addtruncatedropspiltmerge多个分区)

Oracle12c中性能优化&功能增强新特性之全局索引DROP和TRUNCATE 分区的异步维护