oracle分區表創建(自動按年、月、日分區)實戰記錄
工作中有一張表一年會增長100多萬的數據,量雖然不大,可是表字段多,所以一年下來也會達到 1G,而且只增不改,故考慮使用分區表來提高查詢性能,提高維護性。
oracle 11g 支持自動分區,不過得在創建表時就設置好分區。
如果已經存在的表需要改分區表,就需要將當前表 rename后,再創建新表,然后復制數據到新表,然后刪除舊表就可以了。
一、為什么要分區(Partition)1、一般一張表超過2G的大小,ORACLE是推薦使用分區表的。
2、這張表主要是查詢,而且可以按分區查詢,只會修改當前最新分區的數據,對以前的不怎么做刪除和修改。
3、數據量大時查詢慢。
4、便于維護,可擴展:11g 中的分區表新特性:Partition(分區)一直是 Oracle 數據庫引以為傲的一項技術,正是分區的存在讓 Oracle 高效的處理海量數據成為可能,在 Oracle 11g 中,分區技術在易用性和可擴展性上再次得到了增強。
5、與普通表的 sql 一致,不需要因為普通表變分區表而修改我們的代碼。
二、oracle 11g 如何按天、周、月、年自動分區2.1 按年創建numtoyminterval(1, 'year')--按年創建分區表create table test_part(?? ID NUMBER(20) not null,?? REMARK VARCHAR2(1000),?? create_time DATE)PARTITION BY RANGE (CREATE_TIME) INTERVAL (numtoyminterval(1, 'year'))(partition part_t01 values less than(to_date('2018-11-01', 'yyyy-mm-dd')));--創建主鍵alter table test_part add constraint test_part_pk primary key (ID) using INDEX;-- Create/Recreate indexescreate index test_part_create_time on TEST_PART (create_time);2.2 按月創建numtoyminterval(1, 'month')--按月創建分區表create table test_part(?? ID NUMBER(20) not null,?? REMARK VARCHAR2(1000),?? create_time DATE)PARTITION BY RANGE (CREATE_TIME) INTERVAL (numtoyminterval(1, 'month'))(partition part_t01 values less than(to_date('2018-11-01', 'yyyy-mm-dd')));--創建主鍵alter table test_part add constraint test_part_pk primary key (ID) using INDEX;2.3 按天創建NUMTODSINTERVAL(1, 'day')--按天創建分區表create table test_part(?? ID NUMBER(20) not null,?? REMARK VARCHAR2(1000),?? create_time DATE)PARTITION BY RANGE (CREATE_TIME) INTERVAL (NUMTODSINTERVAL(1, 'day'))(partition part_t01 values less than(to_date('2018-11-12', 'yyyy-mm-dd')));--創建主鍵alter table test_part add constraint test_part_pk primary key (ID) using INDEX;2.4 按周創建NUMTODSINTERVAL (7, 'day')--按周創建分區表create table test_part(?? ID NUMBER(20) not null,?? REMARK VARCHAR2(1000),?? create_time DATE)PARTITION BY RANGE (CREATE_TIME) INTERVAL (NUMTODSINTERVAL (7, 'day'))(partition part_t01 values less than(to_date('2018-11-12', 'yyyy-mm-dd')));--創建主鍵alter table test_part add constraint test_part_pk primary key (ID) using INDEX;2.5 測試可以添加幾條數據來看看效果,oracle 會自動添加分區。--查詢當前表有多少分區select table_name,partition_name from user_tab_partitions where table_name='TEST_PART';--查詢這個表的某個(SYS_P21)里的數據select * from TEST_PART partition(SYS_P21);三、numtoyminterval 和 numtodsinterval 的區別 3.1 numtodsinterval(<x>,<c>) ,x 是一個數字,c 是一個字符串。把 x 轉為 interval day to second 數據類型。
常用的單位有 ('day','hour','minute','second')。
測試一下:
select sysdate, sysdate + numtodsinterval(4,'hour') as res from dual;結果:
將 x 轉為 interval year to month 數據類型。
常用的單位有 ('year','month')。
測試一下:
select sysdate, sysdate + numtoyminterval(3, 'year') as res from dual;結果:
表示小于 2018-11-01 的都放在 part_t01 分區表中。
五、給已有的表分區需要先備份表,然后新建這個表,拷貝數據,刪除備份表。
-- 1. 重命名
alter table test_part rename to test_part_temp;-- 2. 創建 partition table
create table test_part(?? ID NUMBER(20) not null,?? REMARK VARCHAR2(1000),?? create_time DATE)PARTITION BY RANGE (CREATE_TIME) INTERVAL (numtoyminterval(1, 'month'))(partition part_t1 values less than(to_date('2018-11-01', 'yyyy-mm-dd')));-- 3. 創建主鍵
alter table test_part add constraint test_part_pk_1 primary key (ID) using INDEX;-- 4. 將 test_part_temp 表里的數據遷移到 test_part 表中
insert into test_part_temp select * from test_part;-- 5. 為分區表設置索引
-- Create/Recreate indexescreate index test_part_create_time_1 on TEST_PART (create_time);-- 6. 刪除老的 test_part_temp 表
drop table test_part_temp purge;-- 7. 作用是:允許分區表的分區鍵是可更新。
-- 當某一行更新時,如果更新的是分區列,并且更新后的列植不屬于原來的這個分區,
-- 如果開啟了這個選項,就會把這行從這個分區中 delete 掉,并加到更新后所屬的分區,此時就會發生 rowid 的改變。
-- 相當于一個隱式的 delete + insert ,但是不會觸發 insert/delete 觸發器。
alter table test_part enable row movement;六、全局索引和 Local 索引我的理解是:
當查詢經常跨分區查,則應該使用全局索引,因為這是全局索引比分區索引效率高。
當查詢在一個分區里查詢時,則應該使用 local 索引,因為本地索引比全局索引效率高。
總結到此這篇關于oracle分區表創建(自動按年、月、日分區)的文章就介紹到這了,更多相關oracle分區表創建內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
