Oracle分區表的管理筆記(僅限于對普通表,即堆表的分區管理,IOT跟CLUSTER TABLE不再討論范圍內)
1. 增加分區(add partition)
語法是:alter table xxx add partition…
需要注意的是如果分區中存在maxvalue或default分區add partition會報錯,應使用split
-
如:
Alter table t_range add partition p5 values less than (50) [tablespace users];
--50 要大于之前分區的所有值
Alter table t_list add partition p5 values (7,8,9) [tablespace users];
--7,8,9均不能在之前分區中出現
Alter table t_hash add partition [p5] [tablespace users];
增加子分區:
Alter table xxx modify partition p1 add subpartition …
如:增加RANGE-HASH子分區
ALTER TABLE diving MODIFY PARTITION locations_us
ADD SUBPARTITION us_locs5 TABLESPACE us1;
Range,list增加分區不會影響索引(包括global 跟local),HASH增加分區會讓數據重新分配,產生IO,如果不指定update indexes 選項則會導致有數據移動的索引unusable,需要重新編譯。
當然,我們說的對索引的影響都是在表中有數據的情況下,沒數據當然影響不到索引了。
2. 合并分區(coalesce partition)
Alter table xxx coalesce partion [update indexes];
Alter table xxx modify partition p1 coalesce subpartition;
僅適用于HASH分區或子分區,合并一次會減少一個分區(最少能減少到1個),數據重新分配,產生IO,有數據移動的索引失效(如果不指定update indexes的話).
3. 刪除分區(drop partition)
Alter table xxx drop partition ppp;
刪除子分區:
Alter table xxx drop subpartition ppp;
此功能hash不支持。同時要注意,刪除分區會同時刪除該分區內數據。
同樣,如果不指定update indexes的話該操作會導致GLOBAL索引失效,而LOCAL不會,因為對應的LOCAL索引分區也被刪除了嘛,其他分區的LOCAL不會受到影響。
4. 交換分區(exchange partition)
Alter table tb1 exchange partition/subpartition p1 with table tb2;
據說是采用了更改數據字典的方式,所以速度比較快。
可以是分區跟非分區表交換,子分區跟非分區表交換,組合分區跟分區表交換。
例如:
組合分區跟分區表交換:
CREATE TABLE t1 (i NUMBER, j NUMBER)
PARTITION BY HASH(i)
(PARTITION p1, PARTITION p2);
CREATE TABLE t2 (i NUMBER, j NUMBER)
PARTITION BY RANGE(j)
SUBPARTITION BY HASH(i)
(PARTITION p1 VALUES LESS THAN (10)
SUBPARTITION t2_pls1
SUBPARTITION t2_pls2,
PARTITION p2 VALUES LESS THAN (20)
SUBPARTITION t2_p2s1
SUBPARTITION t2_p2s2));
ALTER TABLE t2 EXCHANGE PARTITION p1 WITH TABLE t1
WITH VALIDATION;
如果指定WITH VALIDATION(默認) 會對交換進來的數據進行合法檢查,看是否符合該分區的規則,WITHOUT VALIDATION 會忽略合法檢查(比如ID=12的記錄此時可以交換到ID VALUES LESS THAN (10)的分區里),但如果表上有primary key 或unique 約束的話,指定without validation會被忽略。
同樣,如果不指定UPDATE INDEXES ,GLOBAL 索引會失效,需要重新編譯。
5. 合并分區(merge partitions)
Alter table xxx merge partitions/subpartitions p1,p2 into partiton/subpartition p3 [TABLESPACE tablespace_name];
HASH不適用,因為它有COALESCE了嘛。
表分區必須是相鄰的。
跟COALESCE一樣,會產生IO,數據量大的話,IO也是相當大的。
同樣可以用UPDATE INDEXES 避免索引失效
6. 修改LIST分區—ADD VALUES
Alter table xxx modify partition/subpartition p1 add values(7,9);
要注意的是,增加的VALUES不能在其他分區列的VALUES值中存在,也不能在DEFAULT分區(如果有的話)中有相應VALUES.
不會影響索引
7. 修改LIST 分區—DROP VALUES
Alter table xxx modify partition/subpartition p1 drop values(8,9);
同樣,刪除的values 不能存在記錄.
不會影響索引
8. 拆分分區(split partitions)
功能與MERGE PARTITIONS相反。通常我們會用來拆分MAXVALUE/DEFAULT分區。
Range partition:
Alter table xxx split partition/subpartition p1 at (15) into (partition/subpartition p1_new1,partition/subpartition p1_new2);
List partition:
Alter table xxx split partition/subpartition p1 values(15,16) into (partition/subpartition p1_new1,partition/subpartition p1_new2);
原分區中符合新值定義的記錄會存入第一個分區,其他存入第二個分區,當然,在新分區后面可以指定屬性,比如TABLESPACE。
HASH分區不適用。
會產生IO
同樣,可用update indexes 來避免索引失效
9. 截斷分區(truncate partition)
跟TRUNCATE TABLE一樣,截斷該分區內的數據。
Alter table xxx truncate partition/subpartition p1;
同樣,可用update indexes 來避免索引失效
10. 移動分區(move partition)
Alter table xxx move partition/subpartition p1 …;
有些功能比如改變分區表空間,modify partition就做不到,此時就可以用move partition來做。
Use the MOVE PARTITION clause of the ALTER TABLE statement to:
• Re-cluster data and reduce fragmentation
• Move a partition to another tablespace
• Modify create-time attributes
• Store the data in compressed format using table compression
如:
ALTER TABLE parts MOVE PARTITION depot2
TABLESPACE ts094 NOLOGGING COMPRESS;
(如果指定compress,affects only future storage, but not existing data.)
同樣,可用update indexes 來避免索引失效
11. 重命名分區(rename partition)
Alter table xxx rename partition/subpartition p1 to p1_new;
跟重命名表差不多。
12. 修改分區默認屬性(modify default attributes)
修改表屬性:alter table xxx modify default attributes …
修改分區屬性(適用于組合分區):alter table xxx modify default attributes for partition p1 …
只對以后添加的分區產生影響,適用于所有分區,其中hash分區只能修改表空間屬性。
如:
Alter table xxx modify default attributes tablespace users;
13. 修改子分區模板屬性(set subpartition template)
Alter table xxx set subpartition template (…);
僅影響以后的子分區,當前的子分區屬性不會改變
如:
Alter table xxx set subpartition template
(partition p1 tablespace tbs_1,
Partition p2 tablespace tbs_2);
如果要取消掉子分區模板:
Alter table xxx set subpartition template ();
本文出自:億恩科技【www.vbseamall.com】
服務器租用/服務器托管中國五強!虛擬主機域名注冊頂級提供商!15年品質保障!--億恩科技[ENKJ.COM]
|