您好,登錄后才能下訂單哦!
這篇文章主要講解了“Oracle的分區表有什么用”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Oracle的分區表有什么用”吧!
下面對分區表進行簡單的實驗:
先查詢所需要進行分區表的表結構:
sys@FX1TEST>desc fqtbb Name Null? Type -------------------------------------------------------------------------- -------- -------------------------------------------------- ID NUMBER YXTIME DATE HASH VARCHAR2(50) GOODLOOK VARCHAR2(20)
查詢表當前的數據:
sys@FX1TEST>select * from fqtbb; ID YXTIME HASH GOODLOOK ---------- ------------------- -------------------- ---------- 1 2011-01-02 00:00:00 java111 no 1 2011-04-02 00:00:00 java121 no 1 2011-05-02 00:00:00 java131 no 1 2012-02-02 00:00:00 java141 no 1 2013-01-02 00:00:00 java1ds no 1 2012-06-04 00:00:00 java1da no 1 2013-12-21 00:00:00 java771 no 1 2013-02-02 00:00:00 java771 no 1 2014-06-25 00:00:00 java661 no 1 2014-01-02 00:00:00 java156 no 1 2015-05-22 00:00:00 java132 no 1 2015-06-07 00:00:00 java134 no 1 2016-04-21 00:00:00 java152 yes 1 2016-11-11 00:00:00 java761 yes 1 2016-01-21 00:00:00 java171 yes 1 2017-04-21 00:00:00 java711 yes 1 2017-05-05 00:00:00 java717 yes 1 2017-01-02 00:00:00 java174 yes 1 2018-03-01 00:00:00 java172 yes 1 2018-01-23 00:00:00 java176 yes 1 2018-12-22 00:00:00 java166 yes 21 rows selected.
下面便是一些常用的分區方法
范圍分區:利用分區鍵定義范圍,根據實際取值,進行分區選擇,這種范圍一般以數字形式存儲,例如時間,分數,工資等等,所以不可避免的這種分區方法做不到均勻分區。
這里我們用時間范圍進行劃分 :
create table fqtbb_part ( id number, yxtime date, hash varchar(50), goodlook varchar(20)) PARTITION BY RANGE (yxtime) ( PARTITION fqtbb_part1 VALUES LESS THAN (TO_DATE('2011-01-01','yyyy-mm-dd')) TABLESPACE test, PARTITION fqtbb_part2 VALUES LESS THAN (TO_DATE('2014-01-02','yyyy-mm-dd')) TABLESPACE test1, PARTITION fqtbb_part3 VALUES LESS THAN (MAXVALUE) TABLESPACE test2 ); Table created.
將大表中的數據插到新建表中:
sys@FX1TEST>insert into fqtbb_part select * from fqtbb; 21 rows created. sys@FX1TEST>commit; Commit complete.
接下來查詢單獨分區的數據信息
sys@FX1TEST>select * from fqtbb_part partition(fqtbb_part3); ID YXTIME HASH GOODLOOK ---------- ------------------- -------------------- ---------- 1 2014-06-25 00:00:00 java661 no 1 2014-01-02 00:00:00 java156 no 1 2015-05-22 00:00:00 java132 no 1 2015-06-07 00:00:00 java134 no 1 2016-04-21 00:00:00 java152 yes 1 2016-11-11 00:00:00 java761 yes 1 2016-01-21 00:00:00 java171 yes 1 2017-04-21 00:00:00 java711 yes 1 2017-05-05 00:00:00 java717 yes 1 2017-01-02 00:00:00 java174 yes 1 2018-03-01 00:00:00 java172 yes 1 2018-01-23 00:00:00 java176 yes 1 2018-12-22 00:00:00 java166 yes 13 rows selected.
sys@FX1TEST>select * from fqtbb_part partition(fqtbb_part2); ID YXTIME HASH GOODLOOK ---------- ------------------- -------------------- ---------- 1 2011-01-02 00:00:00 java111 no 1 2011-04-02 00:00:00 java121 no 1 2011-05-02 00:00:00 java131 no 1 2012-02-02 00:00:00 java141 no 1 2013-01-02 00:00:00 java1ds no 1 2012-06-04 00:00:00 java1da no 1 2013-12-21 00:00:00 java771 no 1 2013-02-02 00:00:00 java771 no 8 rows selected.
當然如果查詢分區表的信息。也有相對應的表供用戶查詢。例如:
sys@FX1TEST>select TABLE_NAME,PARTITIONING_TYPE,PARTITION_COUNT,STATUS from dba_part_tables where table_name='FQTBB_PART'; TABLE_NAME PARTITIONING_TYPE PARTITION_COUNT STATUS ------------------------------ ------------------ --------------- ---------------- FQTBB_PART RANGE3 VALID
查詢表分區所處的表空間:
sys@FX1TEST>select PARTITION_NAME,NUM_ROWS,TABLESPACE_NAME,SEGMENT_CREATED from dba_tab_partitions where table_name='FQTBB_PART'; PARTITION_NAME NUM_ROWS TABLESPACE SEGMENT_ -------------------- ---------- ---------- -------- FQTBB_PART1TEST YES FQTBB_PART2TEST1 YES FQTBB_PART3TEST2 YES
列分區
列分區根據分區鍵指定的取值列表進行分區,但是這種分區方法適合的比較少,例如部門列表、所屬項目組名稱等等
這里根據goodlook鍵進行定義:
create table fqtbb_parta ( id number, yxtime date, hash varchar(50), goodlook varchar(20)) PARTITION BY list(goodlook) ( PARTITION fqtbb_part1 VALUES ('yes') TABLESPACE test, PARTITION fqtbb_part2 VALUES ('no') TABLESPACE test1, PARTITION fqtbb_part3 VALUES (default) TABLESPACE test2 ); Table created.
插入數據
sys@FX1TEST>insert into fqtbb_parta select * from fqtbb; 21 rows created. sys@FX1TEST>commit; Commit complete.
分區表創建完成之后進行查詢驗證:
sys@FX1TEST>select * from fqtbb_parta partition(fqtbb_part2); ID YXTIME HASH GOODLOOK ---------- ------------------- -------------------- ---------- 1 2011-01-02 00:00:00 java111 no 1 2011-04-02 00:00:00 java121 no 1 2011-05-02 00:00:00 java131 no 1 2012-02-02 00:00:00 java141 no 1 2013-01-02 00:00:00 java1ds no 1 2012-06-04 00:00:00 java1da no 1 2013-12-21 00:00:00 java771 no 1 2013-02-02 00:00:00 java771 no 1 2014-06-25 00:00:00 java661 no 1 2014-01-02 00:00:00 java156 no 1 2015-05-22 00:00:00 java132 no 1 2015-06-07 00:00:00 java134 no 12 rows selected. sys@FX1TEST>select * from fqtbb_parta partition(fqtbb_part1); ID YXTIME HASH GOODLOOK ---------- ------------------- -------------------- ---------- 1 2016-04-21 00:00:00 java152 yes 1 2016-11-11 00:00:00 java761 yes 1 2016-01-21 00:00:00 java171 yes 1 2017-04-21 00:00:00 java711 yes 1 2017-05-05 00:00:00 java717 yes 1 2017-01-02 00:00:00 java174 yes 1 2018-03-01 00:00:00 java172 yes 1 2018-01-23 00:00:00 java176 yes 1 2018-12-22 00:00:00 java166 yes 9 rows selected. sys@FX1TEST>select * from fqtbb_parta partition(fqtbb_part3); no rows selected
下面是oracle 分區表常用的管理操作,如下:
由于我們之前再范圍分區時,設置了default值,所以無法增加分區,所以這里先演示刪除分區
sys@FX1TEST>alter table fqtbb_part drop partition fqtbb_part3; Table altered. sys@FX1TEST>select PARTITION_NAME,NUM_ROWS,TABLESPACE_NAME,SEGMENT_CREATED from dba_tab_partitions where table_name='FQTBB_PART'; PARTITION_NAME NUM_ROWS TABLESPACE_NAME SEGMENT_ ------------------------------------------------------------ ---------- ------------------------------------------------------------ -------- FQTBB_PART1 0 TEST YES FQTBB_PART2 8 TEST1 YES
添加分區
sys@FX1TEST>alter table fqtbb_part add partition p3 values less than (to_date('2019-09-09','yyyy-mm-dd')); Table altered.
截斷分區:截斷分區是指清楚數據,保留分區結構,如下
sys@FX1TEST>alter table fqtbb_part truncate partition p3; Table truncated.
合并分區:指將相鄰的分區合并成一個分區,例如2019、2018 這兩個分區,合并之后,分區將采用2019這個界限,并且合并是要合并到2019這個分區里;如下
sys@FX1TEST>alter table fqtbb_part merge partitions fqtbb_part1,fqtbb_part2 into partition fqtbb_part2; Table altered. sys@FX1TEST>select PARTITION_NAME,NUM_ROWS,TABLESPACE_NAME,SEGMENT_CREATED from dba_tab_partitions where table_name='FQTBB_PART'; PARTITION_NAME NUM_ROWS TABLESPACE_NAME SEGMENT_ ------------------------------------------------------------ ---------- ------------------------------------------------------------ -------- FQTBB_PART2SYSTEM YES P3SYSTEM YES
拆分分區:有合并當然就有拆分,意思就是將一個分區拆成兩個分區,而之前的分區將不存在;如下
sys@FX1TEST>alter table fqtbb_part split partition fqtbb_part2 at(to_date('2014-01-01','yyyy-mm-dd')) into (partition p1,partition p2); Table altered. sys@FX1TEST>select PARTITION_NAME,NUM_ROWS,TABLESPACE_NAME,SEGMENT_CREATED from dba_tab_partitions where table_name='FQTBB_PART'; PARTITION_NAME NUM_ROWS TABLESPACE_NAME SEGMENT_ ------------------------------------------------------------ ---------- ------------------------------------------------------------ -------- P1SYSTEM YES P2SYSTEM YES P3SYSTEM YES
重命名分區:顧名思義,就是讓分區換一個名字
sys@FX1TEST>alter table fqtbb_part rename partition p1 to p11; Table altered.
感謝各位的閱讀,以上就是“Oracle的分區表有什么用”的內容了,經過本文的學習后,相信大家對Oracle的分區表有什么用這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。