av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

oracle分區(qū)表創(chuàng)建(自動(dòng)按年、月、日分區(qū))實(shí)戰(zhàn)記錄

瀏覽:3日期:2023-09-18 20:52:52
目錄前言:一、為什么要分區(qū)(Partition)二、oracle 11g 如何按天、周、月、年自動(dòng)分區(qū)2.1 按年創(chuàng)建2.2 按月創(chuàng)建2.3 按天創(chuàng)建2.4 按周創(chuàng)建2.5 測(cè)試三、numtoyminterval 和 numtodsinterval 的區(qū)別 3.1 numtodsinterval(<x>,<c>) ,x 是一個(gè)數(shù)字,c 是一個(gè)字符串。3.2 numtoyminterval (<x>,<c>)四、默認(rèn)分區(qū)4.1 partition part_t01 values less than(to_date('2018-11-01', 'yyyy-mm-dd'))。五、給已有的表分區(qū)六、全局索引和 Local 索引總結(jié)前言:

工作中有一張表一年會(huì)增長(zhǎng)100多萬的數(shù)據(jù),量雖然不大,可是表字段多,所以一年下來也會(huì)達(dá)到 1G,而且只增不改,故考慮使用分區(qū)表來提高查詢性能,提高維護(hù)性。

oracle 11g 支持自動(dòng)分區(qū),不過得在創(chuàng)建表時(shí)就設(shè)置好分區(qū)。

如果已經(jīng)存在的表需要改分區(qū)表,就需要將當(dāng)前表 rename后,再創(chuàng)建新表,然后復(fù)制數(shù)據(jù)到新表,然后刪除舊表就可以了。

一、為什么要分區(qū)(Partition)

  1、一般一張表超過2G的大小,ORACLE是推薦使用分區(qū)表的。

  2、這張表主要是查詢,而且可以按分區(qū)查詢,只會(huì)修改當(dāng)前最新分區(qū)的數(shù)據(jù),對(duì)以前的不怎么做刪除和修改。

  3、數(shù)據(jù)量大時(shí)查詢慢。

  4、便于維護(hù),可擴(kuò)展:11g 中的分區(qū)表新特性:Partition(分區(qū))一直是 Oracle 數(shù)據(jù)庫(kù)引以為傲的一項(xiàng)技術(shù),正是分區(qū)的存在讓 Oracle 高效的處理海量數(shù)據(jù)成為可能,在 Oracle 11g 中,分區(qū)技術(shù)在易用性和可擴(kuò)展性上再次得到了增強(qiáng)。

  5、與普通表的 sql 一致,不需要因?yàn)槠胀ū碜兎謪^(qū)表而修改我們的代碼。

二、oracle 11g 如何按天、周、月、年自動(dòng)分區(qū)2.1 按年創(chuàng)建numtoyminterval(1, 'year')--按年創(chuàng)建分區(qū)表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')));--創(chuàng)建主鍵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 按月創(chuàng)建numtoyminterval(1, 'month')--按月創(chuàng)建分區(qū)表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')));--創(chuàng)建主鍵alter table test_part add constraint test_part_pk primary key (ID) using INDEX;2.3 按天創(chuàng)建NUMTODSINTERVAL(1, 'day')--按天創(chuàng)建分區(qū)表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')));--創(chuàng)建主鍵alter table test_part add constraint test_part_pk primary key (ID) using INDEX;2.4 按周創(chuàng)建NUMTODSINTERVAL (7, 'day')--按周創(chuàng)建分區(qū)表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')));--創(chuàng)建主鍵alter table test_part add constraint test_part_pk primary key (ID) using INDEX;2.5 測(cè)試可以添加幾條數(shù)據(jù)來看看效果,oracle 會(huì)自動(dòng)添加分區(qū)。--查詢當(dāng)前表有多少分區(qū)select table_name,partition_name from user_tab_partitions where table_name='TEST_PART';--查詢這個(gè)表的某個(gè)(SYS_P21)里的數(shù)據(jù)select * from TEST_PART partition(SYS_P21);三、numtoyminterval 和 numtodsinterval 的區(qū)別 3.1 numtodsinterval(<x>,<c>) ,x 是一個(gè)數(shù)字,c 是一個(gè)字符串。

把 x 轉(zhuǎn)為 interval day to second 數(shù)據(jù)類型。

常用的單位有 ('day','hour','minute','second')。

測(cè)試一下:

select sysdate, sysdate + numtodsinterval(4,'hour') as res from dual;

結(jié)果:

3.2 numtoyminterval (<x>,<c>)

將 x 轉(zhuǎn)為 interval year to month 數(shù)據(jù)類型。

常用的單位有 ('year','month')。

測(cè)試一下:

select sysdate, sysdate + numtoyminterval(3, 'year') as res from dual;

結(jié)果:

四、默認(rèn)分區(qū)4.1 partition part_t01 values less than(to_date('2018-11-01', 'yyyy-mm-dd'))。

表示小于 2018-11-01 的都放在 part_t01 分區(qū)表中。

五、給已有的表分區(qū)

需要先備份表,然后新建這個(gè)表,拷貝數(shù)據(jù),刪除備份表。

-- 1. 重命名

alter table test_part rename to test_part_temp;

-- 2. 創(chuàng)建 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. 創(chuàng)建主鍵

alter table test_part add constraint test_part_pk_1 primary key (ID) using INDEX;

-- 4. 將 test_part_temp 表里的數(shù)據(jù)遷移到 test_part 表中

insert into test_part_temp select * from test_part;

-- 5. 為分區(qū)表設(shè)置索引

-- 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. 作用是:允許分區(qū)表的分區(qū)鍵是可更新。

-- 當(dāng)某一行更新時(shí),如果更新的是分區(qū)列,并且更新后的列植不屬于原來的這個(gè)分區(qū),

-- 如果開啟了這個(gè)選項(xiàng),就會(huì)把這行從這個(gè)分區(qū)中 delete 掉,并加到更新后所屬的分區(qū),此時(shí)就會(huì)發(fā)生 rowid 的改變。

-- 相當(dāng)于一個(gè)隱式的 delete + insert ,但是不會(huì)觸發(fā) insert/delete 觸發(fā)器。

alter table test_part enable row movement;六、全局索引和 Local 索引

我的理解是:

  當(dāng)查詢經(jīng)常跨分區(qū)查,則應(yīng)該使用全局索引,因?yàn)檫@是全局索引比分區(qū)索引效率高。

  當(dāng)查詢?cè)谝粋€(gè)分區(qū)里查詢時(shí),則應(yīng)該使用 local 索引,因?yàn)楸镜厮饕热炙饕矢摺?/p>總結(jié)

到此這篇關(guān)于oracle分區(qū)表創(chuàng)建(自動(dòng)按年、月、日分區(qū))的文章就介紹到這了,更多相關(guān)oracle分區(qū)表創(chuàng)建內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

主站蜘蛛池模板: 亚洲精品视频在线播放 | 午夜av免费 | 欧美久久网 | aaaa级片| 日本男人天堂 | 福利在线看 | 成人免费高清 | 国产激情小视频 | 亚洲一区在线看 | www.黄色 | 嫩草嫩草嫩草嫩草 | 国产成人精品视频 | 色一情一乱一乱一区91av | 久久精品视频网站 | 四虎看片 | 一级片大全 | 成人小视频在线 | 视频一二区 | 免费看毛片的网站 | 丁香婷婷在线 | a视频| 国产精品自拍小视频 | 亚洲最大的网站 | 蜜臀久久99精品久久久久久宅男 | 日韩一级黄色片 | 成人免费毛片片v | 九九热精品在线观看 | 天天天天天干 | 欧美国产在线观看 | 在线日韩视频 | 国产综合视频 | 欧美性猛交一区二区三区精品 | 精品久久久久久久久久久久久 | 欧美在线网址 | 日韩免费毛片 | 亚洲小视频在线观看 | 成人涩涩 | 久久在线免费观看 | 午夜天堂网| 日本综合久久 | 国产精品伦子伦免费视频 |