通過PL/SQL developer工具查看錶空間的情況
創建兩個表空間,參考《Oracle創建表空間和表》
SQL> create TABLESPACE TBSP_1 DATAFILE 'D:\app\Administrator\oradata\oracle\TBSP_1.dbf' SIZE 10M;
表空間已創建。
SQL> CREATE TABLESPACE TBSP_2 DATAFILE 'D:\app\Administrator\oradata\oracle\TBSP_2.dbf' SIZE 10M;
表空間已創建。
查看創建好的表空間
SQL> select * from v$tablespace;
TS# NAME INC BIG FLA ENC
---------- ------------------------------ --- --- --- ---
0 SYSTEM YES NO YES
1 SYSAUX YES NO YES
2 UNDOTBS1 YES NO YES
4 USERS YES NO YES
3 TEMP NO NO YES
6 TBSP_1 YES NO YES
7 TBSP_2 YES NO YES
分區
- 減少維護工作量
- 增強數據庫可用性
- 均衡I/O
- 分區對用戶透明,對其存在無感知
- 提高查詢速度
1 範圍分區(range)
- 數據根據分區鍵的範圍進行分佈
例如:日期分區鍵–“08-2019”就會包括從“01-08-2019”到“31-08-2019”之間的所有分區鍵值。
實例:
創建一個商品零售表,根據銷售日期創建四個範圍分區。
create table ware_retail_part(
id integer primary key,
retail_date date,
ware_name varchar2(50)
)
partition by range(retail_date)
(
partition par_01 values less than(to_date('2011-04-01','yyyy-mm-dd')) tablespace TBSP_1,
partition par_02 values less than(to_date('2011-07-01','yyyy-mm-dd')) tablespace TBSP_1,
partition par_03 values less than(to_date('2011-10-01','yyyy-mm-dd')) tablespace TBSP_2,
partition par_04 values less than(to_date('2012-01-01','yyyy-mm-dd')) tablespace TBSP_2
)
創建好後,插入若干條記錄:
SQL> insert into ware_retail_part values(1,to_date('2011-01-20','yyyy-mm-dd'),'平板電 腦');
SQL> insert into ware_retail_part values(2,to_date('2011-04-15','yyyy-mm-dd'),'智能手 機');
SQL> insert into ware_retail_part values(3,to_date('2011-07-25','yyyy-mm-dd'),'MP5');
查看分區和表格:
SQL> select * from ware_retail_part partition(par_02);
ID RETAIL_DATE WARE_NAME
---------- -------------- --------------------------------------------------
2 15-4月 -11 智能手 機
SQL> select * from ware_retail_part;
ID RETAIL_DATE WARE_NAME
---------- -------------- --------------------------------------------------
1 20-1月 -11 平板電 腦
2 15-4月 -11 智能手 機
3 25-7月 -11 MP5
如果是多範圍分區,比如根據銷售編號和銷售日期的組合創建三個分區:
參考《oracle 創建分區表以及自動添加分區》
SQL> create table ware_retail_part2(
2 id integer primary key,
3 retail_no integer,
4 retail_date date,
5 ware_name varchar2(50)
6 )
7 partition by range(retail_date,retail_no)
8 (
9 partition par_01 values less than(to_date('2011-04-01','yyyy-mm-dd'),5) tablespace TBSP_1,
10 partition par_02 values less than(to_date('2011-08-01','yyyy-mm-dd'),10) tablespace TBSP_1,
11 partition par_03 values less than(to_date('2011-12-01','yyyy-mm-dd'),15) tablespace TBSP_2
12 )
13 /
SQL> insert into ware_retail_part2 values(2,12,to_date('2011-10-25','yyyy-mm-dd'),'MP5');
SQL> select * from ware_retail_part2 partition(par_03);
ID RETAIL_NO RETAIL_DATE WARE_NAME
---------- ---------- -------------- ----------------------
2 12 25-10月-11 MP5
2 HASH列分區
即散列分區,列值無法確定時採用。
實例:
創建一個person表,爲該表創建hash分區(分區列爲id)
SQL> create table person
2 (
3 id number,
4 personname varchar2(50)
5 )
6 storage(initial 2084k)
7 partition by hash(id)
8 (
9 partition part1 tablespace tbsp_1,
10 partition part2 tablespace tbsp_2
11 );
表已創建。
SQL> insert into person values(55,'wj');
已創建 1 行。
SQL> select * from person partition(part1);
ID PERSONNAME
---------- --------------------------------------------------
55 wj
3 列表分區
創建一個用於保存客戶信息的clients,然後以PROVINCE列爲分區鍵創建列分區;
SQL> l
1 create table clients
2 (
3 id integer primary key,
4 name varchar2(50),
5 province varchar2(20)
6 )
7 partition by list(province)
8 (
9 partition shandong values('山東省'),
10 partition guangdong values('廣東省'),
11 partition yunnan values('雲南省')
12* )
SQL> /
表已創建。
SQL> insert into clients values (19,'東方','雲南省');
已創建 1 行。
SQL> select * from clients partition(yunnan);
ID NAME PROVINCE
---------- -------------------------------------------------- --------------------
19 東方 雲南省
列表分區這裏有一個小測試可以自我簡單測試下:鏈接🔗
分區表添加子分區
參考《對分區表添加子分區》
創建一個保存人員信息的數據表PERSON2,然後建立三個範圍分區,每個分區包含兩個子分區,子分區沒有名字,有系統自動生成,並要求其分佈在2個制定的表空間中。
create table persons2(
id integer primary key,
personname varchar2(50)
)
partition by range(id)
subpartition by hash(personname)
SUBPARTITIONS 2
(
partition part_1 values less than(5) tablespace tbsp_1,
partition part_2 values less than(10) tablespace tbsp_1,
partition part_3 values less than(15) tablespace tbsp_2
);
4 interval分區
創建的分區作爲元數據,只有最開始的分區是永久分區,隨着數據增加會分配更多,並自動創建新的分區和本地索引。
實例:
創建一個表saleRecord,然後爲該表創建一個Interval分區
SQL> create table saleRecord
2 (
3 id number primary key,
4 goodsname varchar2(50),
5 saledate date,
6 quantity number
7 )
8 partition by range(saledate)
9 interval (numtoyminterval(1,'year'))
10 (
11 partition par_first values less than (to_date('2020-01-01','yyyy-mm-dd'))
12 );
表已創建。
SQL> insert into saleRecord values(1,'MP5',sysdate,123);
SQL> select TABLE_NAME, PARTITION_NAME from user_tab_partitions where table_name='SALERECORD';
#查看錶中包含的分區
TABLE_NAME PARTITION_NAME
----------- ------------------------------
SALERECORD PAR_FIRST
SALERECORD SYS_P47
SQL> select * from saleRecord partition(par_first);
未選定行
SQL> select * from saleRecord partition(SYS_P47);
ID GOODSNAME SALEDATE QUANTITY
---------- -------------------------------------------------- -------------- ----------
1 MP5 02-4月 -20 123
5 添加分區
實例:
爲cilents添加一個省分爲“河北省”的表分區
SQL> alter table clients
2 add partition hebei values('河北省')
3 storage(initial 10K next 20k)tablespace tbsp_1
4 nologging;
表已更改。
SQL> select partition_name from user_tab_partitions where table_name ='CLIENTS';
PARTITION_NAME
------------------------------
SHANDONG
GUANGDONG
YUNNAN
HEBEI
6 併入表分區
- MERGE PARTITION語句,將相鄰的範圍分區合併在一起,變成新的分區;
- 合併分區爲空,則標識爲UNSABLE;
- 不能對HASH分區表執行MERGE PARTITION語句;
- 併入範圍分區是將兩個以上的分區合併到一個存在的分區中,合併後索引需重建
實例:
創建一個銷售記錄表sales,然後歲該表的記錄按照銷售日期分爲四個分區;再建立局部索引
SQL> create table sales
2 (
3 id number primary key,
4 goodsname varchar2(10),
5 saledate date
6 )
7 partition by range(saledate)
8 (
9 partition part_seal values less than (to_date('2011-04-01','yyyy-mm-dd')) tablespace tbsp_1,
10 partition part_sea2 values less than (to_date('2011-07-01','yyyy-mm-dd')) tablespace tbsp_2,
11 partition part_sea3 values less than (to_date('2011-10-01','yyyy-mm-dd')) tablespace tbsp_1,
12 partition part_sea4 values less than (to_date('2012-01-01','yyyy-mm-dd')) tablespace tbsp_2
13 )
14 /
表已創建。
SQL> create index index_3_4 on sales(saledate)
2 local
3 (
4 partition part_sea1 tablespace tbsp_1,
5 partition part_sea2 tablespace tbsp_2,
6 partition part_sea3 tablespace tbsp_1,
7 partition part_sea4 tablespace tbsp_2
8 );
索引已創建。
將第三個分區併入到第四個分區中,並重建局部索引:
# 這裏的merge partitions記得加s
SQL> alter table sales merge partitions part_sea3,part_sea4 into partition part_sea4;
表已更改。
SQL> alter table sales modify partition part_sea4 rebuild unusable local indexes;
表已更改。
SQL> select partition_name from user_tab_partitions where table_name ='SALES';
PARTITION_NAME
------------------------------
PART_SEAL
PART_SEA2
PART_SEA4
如果是將第二個和第三個分區併入到第四個分區中,可以合併兩次:
SQL> alter table sales2 merge partitions part_sea2,part_sea3 into partition part_sea3;
表已更改。
SQL> alter table sales2 merge partitions part_sea3,part_sea4 into partition part_sea4;
表已更改。
SQL> select partition_name from user_tab_partitions where table_name ='SALES2';
PARTITION_NAME
------------------------------
PART_SEAL
PART_SEA4
7 創建本地索引分區
本地索引分區與該表分區所採用的列是相同的。
1 準備表空間:
SQL> create TABLESPACE ts_1 DATAFILE 'D:\app\Administrator\oradata\oracle\ts_1.dbf' SIZE 10M extent management local autoallocate;
表空間已創建。
SQL> create TABLESPACE ts_2 DATAFILE 'D:\app\Administrator\oradata\oracle\ts_2.dbf' SIZE 10M extent management local autoallocate;
表空間已創建。
SQL> create TABLESPACE ts_3 DATAFILE 'D:\app\Administrator\oradata\oracle\ts_3.dbf' SIZE 10M extent management local autoallocate;
表空間已創建。
2 創建一個存儲學生成績的分區表studentgrade,該表有三個分區,分別位於三個不同表空間:
SQL> create table studentgrade
2 (
3 id number primary key,
4 name varchar2(10),
5 subject varchar2(10),
6 grade number
7 )
8 partition by range(grade)
9 (
#小於60分,不及格
10 partition par_nopass values less than(60) tablespace ts_1,
#小於70分,及格
11 partition par_pass values less than(70) tablespace ts_2,
#大於等於70分,優秀
12 partition par_good values less than(maxvalue) tablespace ts_3
13 );
表已創建。
3 根據表分區創建本地索引分區
SQL> create index grade_index on studentgrade(grade)
2 local
3 (
4 partition p1 tablespace ts_1,
5 partition p2 tablespace ts_2,
6 partition p3 tablespace ts_3
7 );
索引已創建。
4 根據dba_ind_partitions查看索引分區信息
SQL> select partition_name,tablespace_name from dba_ind_partitions where index_name ='GRADE_INDEX';
PARTITION_NAME TABLESPACE_NAME
------------------------------ ------------------------------
P3 TS_3
P2 TS_2
P1 TS_1
8 索引分區維護
SQL> create table books2(
2 BooksNo number(4) not null,
3 BookName varchar2(20),
4 Author varchar2(10),
5 SalePrice number(9,2),
6 PublisherNo varchar2(4) not null,
7 PublisherDate date,
8 ISBN varchar2(20) not null
9 );
表已創建。
SQL>
SQL> create index index_salesprice on Books2(SalePrice)
2 global partition by range(SalePrice)
3 (
4 partition p1 values less than (30),
5 partition p2 values less than (50),
6 partition p3 values less than (maxvalue)
7 );
索引已創建。
SQL> alter index index_salesprice drop partition p2;
索引已更改。
SQL> alter index index_salesprice drop partition p1;
索引已更改。
全局索引分區,不能刪除索引的最高分區,否則系統報錯
SQL> alter index index_salesprice drop partition p3;
alter index index_salesprice drop partition p3
*
第 1 行出現錯誤:
ORA-14078: 您不能刪除 GLOBAL 索引的最高分區
9 索引分區重命名
alter index index_name rename partition partition_old_name to parttion_new_name;