Posted on 2008-12-21 10:17
陜西BOY 閱讀(233)
評論(0) 編輯 收藏
在應用系統中,如果某個表的數據量很大時,為了保持數據庫的性能,一般都考慮使用分區表,分區表主要有三種分區方式,分別是范圍(RANGE)、列表(LIST)、哈希(HASH)分區,根據表的特性選擇分區類型,一般情況下用RANGE和LIST兩種就夠了
.................
下面就只說RANGE和LIST兩種分區表的創建和管理(列表LIST分區是9I之后才有,8I沒有的)
1、創建分區
--創建范圍(RANGE)分區表的語法:
create table test_tbl(emp_name varchar2(50), emp_loc varchar2(50), emp_hire_date date)
partition by range(emp_hire_date)(
partition p_hire_date1 values less than (to_date('2000-01-01','yyyy-mm-dd')) tablespace t_hire_date1,
partition p_hire_date2 values less than (to_date('2001-01-01','yyyy-mm-dd')) tablespace t_hire_date2,
partition p_hire_date3 values less than (to_date('2002-01-01','yyyy-mm-dd')) tablespace t_hire_date3
...
partition p_hire_dateN values less than (maxvalue) tablespace t_hire_dateN
)
--創建列表(LIST)分區表的語法:
create table test_tbl(emp_name varchar2(50), emp_loc varchar2(50), emp_hire_date date)
partition by list(emp_loc)(
partition p_loc1 values ('GZ') tablespace t_hire_date1,
partition p_loc2 values ('BJ') tablespace t_hire_date2,
partition p_loc3 values ('SH') tablespace t_hire_date3
)
2、管理分區
--增加分區(如果表分區有MAXVALUE,不能再增加分區)
alter table test_tbl add partition p_hire_date4 values less than(to_date('2002-06-01','yyyy-mm-dd'))
--刪除分區
alter table test_tbl drop partition p_hire_date4
--截斷分區(刪除分區的數據)
alter table test_tbl truncate partition p_hire_date4
--拆分分區(拆分后,數據以1999-01-01為臨界分別存放在兩個分區,p_hire_date1不再存在)
alter table test_tbl split partition p_hire_date1 at(to_date('1999-01-01','yyyy-mm-dd'))
into
(
partition p_hire_date11,
partition p_hire_date12
)
--合并分區(合并后,p_hire_date11和p_hire_date12兩個分區就不存在了)
alter table test_tbl merge partitions p_hire_date11,p_hire_date12 into partition p_hire_date1
--交換分區
alter table test_tbl exchange partition p_hire_date1 with table test_tbl2
當要把數據很大的表test_tbl2的數據插入分區表test_tbl中,如果用insert into是很低性能的,最好方法是用交換分區方法。但要注意,交換分區時,當使用without validation時,數據是不驗證test_tbl2表的數據是否都能滿足分區p_hire_date1的條件