首页 > 代码库 > Oracle12c:自动分区表
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 insertedSQL> insert into test_partion (p_day,words) values (to_date(‘20170623‘,‘yyyyMMdd‘),‘00002‘);1 row insertedSQL> insert into test_partion (p_day,words) values (to_date(‘20170623‘,‘yyyyMMdd‘),‘00003‘);1 row insertedSQL> insert into test_partion (p_day,words) values (to_date(‘20170624‘,‘yyyyMMdd‘),‘00004‘);1 row insertedSQL> insert into test_partion (p_day,words) values (to_date(‘20170623‘,‘yyyyMMdd‘),‘00005‘);1 row insertedSQL> insert into test_partion (p_day,words) values (to_date(‘20170625‘,‘yyyyMMdd‘),‘00006‘);1 row insertedSQL> insert into test_partion (p_day,words) values (to_date(‘20170626‘,‘yyyyMMdd‘),‘00006‘);1 row insertedSQL> insert into test_partion (p_day,words) values (to_date(‘20170627‘,‘yyyyMMdd‘),‘00006‘);1 row insertedSQL> insert into test_partion (p_day,words) values (to_date(‘20170628‘,‘yyyyMMdd‘),‘00006‘);1 row insertedSQL> 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=GREGORIATEST_PARTION SYS_P2252 TO_DATE(‘ 2017-06-23 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIATEST_PARTION SYS_P2253 TO_DATE(‘ 2017-06-24 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIATEST_PARTION SYS_P2254 TO_DATE(‘ 2017-06-25 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIATEST_PARTION SYS_P2255 TO_DATE(‘ 2017-06-26 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIATEST_PARTION SYS_P2256 TO_DATE(‘ 2017-06-27 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIATEST_PARTION SYS_P2257 TO_DATE(‘ 2017-06-28 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIATEST_PARTION SYS_P2258 TO_DATE(‘ 2017-06-29 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIATEST_PARTION SYS_P2259 TO_DATE(‘ 2017-06-30 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIA9 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 00001SQL> 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 00005SQL> select * from test_partion partition(SYS_P2254); PK_ID P_DAY WORDS--------------------------------------- ----------- ------- 4 2017/6/24 00004SQL> select * from test_partion partition(SYS_P2255); PK_ID P_DAY WORDS--------------------------------------- ----------- ------- 6 2017/6/25 00006SQL> select * from test_partion partition(SYS_P2256); PK_ID P_DAY WORDS--------------------------------------- ----------- ------- 7 2017/6/26 00006SQL> select * from test_partion partition(SYS_P2257); PK_ID P_DAY WORDS--------------------------------------- ----------- ------- 8 2017/6/27 00006SQL> select * from test_partion partition(SYS_P2258); PK_ID P_DAY WORDS--------------------------------------- ----------- ------- 9 2017/6/28 00006SQL> 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 deletedSQL> 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=GREGORIATEST_PARTION SYS_P2252 TO_DATE(‘ 2017-06-23 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIATEST_PARTION SYS_P2253 TO_DATE(‘ 2017-06-24 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIATEST_PARTION SYS_P2254 TO_DATE(‘ 2017-06-25 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIATEST_PARTION SYS_P2255 TO_DATE(‘ 2017-06-26 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIATEST_PARTION SYS_P2256 TO_DATE(‘ 2017-06-27 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIATEST_PARTION SYS_P2257 TO_DATE(‘ 2017-06-28 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIATEST_PARTION SYS_P2258 TO_DATE(‘ 2017-06-29 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIA8 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 alteredSQL> 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=GREGORIATEST_PARTION SYS_P2252 TO_DATE(‘ 2017-06-23 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIATEST_PARTION SYS_P2253_TO_P2254 TO_DATE(‘ 2017-06-25 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIATEST_PARTION SYS_P2255 TO_DATE(‘ 2017-06-26 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIATEST_PARTION SYS_P2256 TO_DATE(‘ 2017-06-27 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIATEST_PARTION SYS_P2257 TO_DATE(‘ 2017-06-28 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIATEST_PARTION SYS_P2258 TO_DATE(‘ 2017-06-29 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIA7 rows selectedSQL> 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 00004SQL> 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 alteredSQL> insert into test_partion (p_day,words) values (to_date(‘20170623‘,‘yyyyMMdd‘),‘06665‘);1 row insertedSQL> insert into test_partion (p_day,words) values (to_date(‘20170624‘,‘yyyyMMdd‘),‘06665‘);1 row insertedSQL> 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 066656 rows selected
4)分区重命名
SQL> SQL> alter table test_partion rename partition SYS_P2253_to_P2254 TO SYS_P2253_to_P2254___;Table alteredSQL> insert into test_partion (p_day,words) values (to_date(‘20170624‘,‘yyyyMMdd‘),‘76665‘);1 row insertedSQL> 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=GREGORIATEST_PARTION SYS_P2252 TO_DATE(‘ 2017-06-23 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIATEST_PARTION SYS_P2253_TO_P2254___ TO_DATE(‘ 2017-06-25 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIATEST_PARTION SYS_P2255 TO_DATE(‘ 2017-06-26 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIATEST_PARTION SYS_P2256 TO_DATE(‘ 2017-06-27 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIATEST_PARTION SYS_P2257 TO_DATE(‘ 2017-06-28 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIATEST_PARTION SYS_P2258 TO_DATE(‘ 2017-06-29 00:00:00‘, ‘SYYYY-MM-DD HH24:MI:SS‘, ‘NLS_CALENDAR=GREGORIA7 rows selectedSQL> insert into test_partion (p_day,words) values (to_date(‘20170624‘,‘yyyyMMdd‘),‘76665‘);1 row insertedSQL> 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 766658 rows selected
因为这里是动态分区,所以就不介绍怎么添加分区。
Oracle12c:自动分区表
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。