亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2023年06月07日 08:40:55   作者:力哥講技術(shù)  
Oracle的表分區(qū)功能通過(guò)改善可管理性、性能和可用性,從而為各式應(yīng)用程序帶來(lái)了極大的好處,下面這篇文章主要給大家介紹了關(guān)于oracle分區(qū)表創(chuàng)建(自動(dòng)按年、月、日分區(qū))的相關(guān)資料,需要的朋友可以參考下

前言:

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

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

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

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

  1、一般一張表超過(guò)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 indexes
create 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ù)來(lái)看看效果,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 indexes
create 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ū)列,并且更新后的列植不屬于原來(lái)的這個(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)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論