oracle表分區

一、 Oracle表分區的概念
Oracle 分區功能可以提高許多應用程序的可管理性、性能與可用性。通過分區功能,可以將表、索引和索引組織表進一步細分爲段,從而能夠更精確地管理和訪問這些數據庫對象。Oracle 提供了種類繁多的分區方案以滿足每種業務要求。

而且,因爲在 SQL 語句中分區是完全透明的,所以該功能幾乎可應用於任何應用程序。

分區功能的優勢

分區功能通過改善可管理性、性能和可用性,從而爲各式應用程序帶來了極大的好處。通常,分區可以使某些查詢以及維護操作的性能大大提高。此外,分區還可以極大簡化常見的管理任務。通過分區,數據庫設計人員和管理員能夠解決前沿應用程序帶來的一些難題。分區是構建千兆字節數據系統或超高可用性系統的關鍵工具。

分區功能的基本知識

分區功能能夠將表、索引或索引組織表進一步細分爲段。這些數據庫對象的段叫做分區。每個分區有自己的名稱,還可以選擇自己的存儲特性。從數據庫管理員的角度來看,一個分區後的對象具有多個段,這些段既可進行集體管理,也可單獨管理。這就使數據庫管理員在管理分區後的對象時有相當大的靈活性。但是,從應用程序的角度來看,分區後的表與非分區表完全相同,使用 SQL DML 命令訪問分區後的表時,無需任何修改。

表的分區是通過“分區鍵”來實現的,分區鍵指的是一些列,這些列決定了某一行所在的分區。Oracle 數據庫 10g 提供了六項技術用於對錶進行分區:

範圍分區

每個分區都由一個分區鍵值範圍指定(對於一個以日期列作爲分區鍵的表,“2005 年 1 月”分區包含分區鍵值爲從“2005 年 1 月 1 日”到“2005 年 1 月 31 日”的行)。

列表分區

每個分區都由一個分區鍵值列表指定(對於一個地區列作爲分區鍵的表,“北美”分區可能包含值“加拿大”“美國”和“墨西哥”)。

散列分區

將散列算法用於分區鍵來確定指定行所在的分區。

組合範圍散列分區

範圍和散列分區技術的組合,通過該組合,首先對錶進行範圍分區,然後針對每個單獨的範圍分區再使用散列分區技術進一步細分。索引組織表只能進行範圍分區。

組合範圍列表分區

範圍和列表分區技術的組合,通過該組合,首先對錶進行範圍分區,然後針對每個單獨的範圍分區再使用列表分區技術進一步細分。索引組織表可以按範圍、列表或散列進行分區。

Oracle 數據庫 10g 還提供了三種類型的分區索引:

本地索引

本地索引是其分區方式與其所在基礎表的分區方式一模一樣的索引。本地索引的每個分區僅對應於其所在基礎表的一個分區。

全局分區索引

全局分區索引是使用不同於其所在表的分區鍵進行分區的索引,其所在表可以是分區表或非分區表。全局分區的索引可以使用範圍或散列分區進行分區。例如,某個表可以按月份進行範圍分區,因此具有十二個分區,而該表上的索引則可以使用不同的分區鍵進行範圍分區,從而具有不同的分區數量。

全局非分區索引

全局非分區索引基本上和非分區表的索引一樣。索引結構是不分區的。

Oracle 提供了一系列豐富的技術,可用於對錶、索引和索引組織表進行分區,因此可以針對任何業務環境中的任何應用程序進行最佳的分區 Oracle 還提供一套完整的 SQL 命令,用於管理分區表。其中包括添加新分區、刪除分區、分拆分區以及合併分區的命令。

用分區功能提高可管理性

通過 Oracle 分區功能,可將表和索引分成更多、更小的可管理單元,從而使數據庫管理員能以“化整爲零,個個擊破”的方式管理數據。

使用分區功能,維護操作可集中於表的特定部分。例如,數據庫管理員可以只對表的一部分做備份,而不必對整個表做備份。對整個數據庫對象的維護操作,可以在每個分區的基礎上進行,從而將維護工作分解成更容易管理的小塊。

利用分區功能提高可管理性的一個典型用法是支持數據倉庫中的‘滾動視窗’加載進程。假設數據庫管理員每週向表中加載新數據。可以對該表進行範圍分區,使每個分區包含一週的數據。這樣加載進程只是簡單地添加新的分區。添加一個分區的操作比修改整個表效率高很多,因爲 DBA 不需要修改任何其他分區。

用分區功能提高性能

由於限制了所檢查或操作的數據數量,同時支持並行任務執行,Oracle 分區功能實現了性能上增益。這些特性包括:

分區修整

分區修整是用分區功能提高性能的最簡單最有價值的手段。分區修整常常能夠將查詢性能提高几個數量級。例如,假設某個應用程序包含一個存儲訂單歷史記錄的 Orders 表,並且此表已按周分區。查詢一週的訂單隻需訪問該訂單表的一個分區。如果該訂單表包含兩年的歷史記錄,這個查詢只需要訪問一個分區而不是一百零四個。該查詢的執行速度因爲分區修整而有可能快一百倍。分區修整能與所有其他 Oracle 性能特性協作。Oracle 能將分區修整功能與任何索引技術、聯接技術或並行訪問方法結合使用。

分區智能聯接

分區功能可以通過稱爲分區智能聯接的技術提高多表聯接的性能。當兩個表要聯接在一起,而且每個表都用聯接鍵來分區時,就可以使用分區智能聯接。分區智能聯接將大型聯接分解成較小的發生在各個分區間的聯接,從而用較少的時間完成全部聯接。這就給串行和並行的執行都能帶來顯著的性能改善。

用分區功能提高可用性

分區的數據庫對象具有分區獨立性。該分區獨立性特點可能是高可用性戰略的一個重要部分,例如,如果分區表的一個分區不能用,但該表的所有其他分區仍然保持在線並可用。那麼這個應用可以繼續針對該分區表執行查詢和事務處理,只要不是訪問不可用的分區,數據庫操作仍然能夠成功運行。

數據庫管理員可以指定各分區存放在不同的表空間裏,從而讓管理員隔離其它表分區針對單個分區進行備份與恢復操作。還有,分區功能可以減少計劃停機時間。由於分區功能改善了性能,使數據庫管理員能用相對較少的時間完成大型數據庫對象的維護工作。

未來發展方向

自從引入分區技術以來,Oracle 公司在每次推出重要版本時都會增加新的分區方法。Oracle8 引入了範圍分區功能,Oracle8i 引入了散列和組合範圍散列分區功能,Oracle9i 引入了列表分區功能。在最新版本 Oracle 數據庫 10g 中,則增強了用於索引組織表和全局分區索引的分區策略,並且擴展了其用於所有分區維護操作的併發索引維護功能。Oracle 公司致力於不斷完善分區技術,確保滿足所有的業務需求。

結論

具有 Oracle 分區功能的 Oracle 數據庫 10g 可以顯著增強幾乎任何數據庫應用程序的可管理性、性能和可用性。分區功能可用於前沿應用程序,分區功能確實能夠成爲保障這些應用程序成功的關鍵技術成分。同時,分區功能也可用於較爲普通的數據庫應用,來簡化這些應用的管理工作,降低管理成本。

二、Oracle表分區的類型
1)範圍分區
範圍分區就是對數據表中的某個值的範圍進行分區,根據某個值的範圍,決定將該數據存儲在哪個分區上。如根據 序號分區,根據業務記錄的創建日期進行分區等。
2)哈希分區
散列分區爲通過指定分區編號來均勻分佈數據的一種分區類型,因爲通過在I/O設備上進行散列分區,使得這些分區大小一致。如將物料交易表的數據根據交易 ID散列地存放在指定的三個表空間中。
3)列表分區
當你需要明確地控制如何將行映射到分區時,就使用列表分區方法。可以在每個分區的描述中爲該分區列指定一列離散值,這不同於範圍分區,在那裏一個範 圍與一個分區相關,這也不同於散列分區,在那裏用戶不能控制如何將行映射到分區。列表分區方法是特意爲遵從離散值的模塊化數據劃分而設計的。範圍分區或散 列分區不那麼容易做到這一點。進一步說列表分區可以非常自然地將無序的和不相關的數據集進行分組和組織到一起。
與範圍分區和散列分區所不同,列表分區不支持多列分區。如果要將表按列分區,那麼分區鍵就只能由表的一個單獨的列組成,然而可以用範圍分區或散列分 區方法進行分區的所有的列,都可以用列表分區方法進行分區。
4)組合分區
有時候我們需要根據範圍分區後,每個分區內的數據再散列地分佈在幾個表空間中,這樣我們就要使用複合分區。複合分區是先使用範圍分區,然後在每個分區內再 使用散列分區的一種分區方法,如將物料交易的記錄按時間分區,然後每個分區中的數據分三個子分區,將數據散列地存儲在三個指定的表空間中。
三、Oracle表分區的實際操作
--查看s數據文件信息
select * from dba_data_files;
--查看用戶表空間信息
select * from user_tablespaces;
--這裏創建三個表空間,並建立相應的數據文件
create tablespace xgtest_space01 datafile 'D:\ORACLE\ORADATA\ORCL\xg_partition01.DBF' size 50M;
create tablespace xgtest_space02 datafile 'D:\ORACLE\ORADATA\ORCL\xg_partition02.DBF' size 50M;
create tablespace xgtest_space03 datafile 'D:\ORACLE\ORADATA\ORCL\xg_partition03.DBF' size 50M;
1) 範圍分區實例
--創建一個產品表,這個表一產品表主鍵作爲範圍分區字段,
--設置小於100000的產品主鍵記錄放到product_p01分區表,100000-500000的記錄放在product_p02分區,大於500000放到product_p03分區
create table test_product(
product_id number(11) primary key,
product_name varchar2(64) not null,
created_date date not null,
company_name varchar2(128)
)
partition by range (product_id)(
partition product_p01 values less than(100000) tablespace xgtest_space01,
partition product_p02 values less than(500000) tablespace xgtest_space02,
partition product_p03 values less than(maxvalue) tablespace xgtest_space03
);
--查詢建立的產品表信息
select * from user_tables t where t.table_name = upper('test_product');
--查詢建立的產品表信息
select * from user_part_tables t where t.table_name = upper('test_product');
--查詢建立的產品表的分區信息
select * from user_tab_partitions t where t.table_name = upper('test_product');
--插入幾條產品表信息並做事務提交
insert into test_product values(100,'IBM便攜式電腦T43',sysdate,'IBM');
insert into test_product values(1000,'佳能A720相機',sysdate,'Canon');
insert into test_product values(105000,'聯想X200',sysdate,'聯想');
insert into test_product values(300000,'Iphone4',sysdate,'Apple');
insert into test_product values(700000,'windows7系列',sysdate,'microsoft');
commit;
--查詢每個分區的分表記錄信息
select * from test_product partition(product_p01);
select * from test_product partition(product_p02);
select * from test_product partition(product_p03);
--查詢表信息
select * from test_product;
--分區表條件查詢
select * from test_product partition(product_p01) t where t.product_id = 1000;
--表條件查詢
select * from test_product t where t.product_id = 1000;
--在分區表上做更新操作
update test_product partition(product_p01) t set t.company_name='佳能公司' where t.product_id=1000;
commit;
--在分區表上做刪除操作
delete from test_product partition(product_p01) t where t.product_id=300000;
commit;
2) 哈希分區實例
--建立一個銷售訂單記錄表,並按照銷售時間做哈希分區字段
create table test_sale_order(
sale_order_id number(11) primary key,
product_name varchar2(64) not null,
saled_date date not null,
operator_name varchar2(32)
)
partition by hash(saled_date)(
partition sale_order_p01 tablespace xgtest_space01,
partition sale_order_p02 tablespace xgtest_space02,
partition sale_order_p03 tablespace xgtest_space03
);
--查詢表信息以及分區信息
select * from user_part_tables t where t.table_name = upper('test_sale_order');
select * from user_tab_partitions t where t.table_name = upper('test_sale_order');
--插入銷售訂單記錄
insert into test_sale_order values(1000, '羅技鼠標',to_date('2007-06-23','yyyy-mm-dd'),'許果');
insert into test_sale_order values(1001, 'Thinkpad T60筆記本',to_date('2007-06-23','yyyy-mm-dd'),'fruitking');
insert into test_sale_order values(1002, '海爾冰箱',to_date('2008-06-20','yyyy-mm-dd'),'劉德華');
insert into test_sale_order values(1003, '格力空調',to_date('2007-10-09','yyyy-mm-dd'),'劉若英');
insert into test_sale_order values(1004, '浪莎絲襪',to_date('2009-09-05','yyyy-mm-dd'),'孫儷');
insert into test_sale_order values(1005, '美的電扇',to_date('2010-03-19','yyyy-mm-dd'),'小張');
insert into test_sale_order values(1006, '安琪兒自行車',to_date('2015-07-28','yyyy-mm-dd'),'小林');
commit;
--查詢分區記錄以及表記錄,分區和表的條件查詢
select * from test_sale_order partition(sale_order_p01);
select * from test_sale_order partition(sale_order_p02);
select * from test_sale_order partition(sale_order_p03);
select * from test_sale_order;
select * from test_sale_order partition(sale_order_p01) t where t.sale_order_id = 1000;
select * from test_sale_order t where t.sale_order_id = 1000;
3) 列表分區實例
--創建公司表,並已列表分區
create table test_company(
company_id number(11) primary key,
company_name varchar2(64) not null,
created_date date not null,
city_name varchar2(32)
)
partition by list (city_name)(
partition company_p01 values ('杭州') tablespace xgtest_space01,
partition company_p02 values ('上海') tablespace xgtest_space02,
partition company_p03 values (default) tablespace xgtest_space03
);
--查詢表和分區信息
select * from user_part_tables t where t.table_name = upper('test_company');
select * from user_tab_partitions t where t.table_name = upper('test_company');
--插入公司記錄信息
insert into test_company values(1000, '阿里巴巴',sysdate,'杭州');
insert into test_company values(1001, '華爲',sysdate,'深圳');
insert into test_company values(1002, '恆生電子',sysdate,'杭州');
insert into test_company values(1003, '盛大',sysdate,'上海');
insert into test_company values(1004, '浦發銀行',sysdate,'上海');
insert into test_company values(1005, '信雅達',sysdate,'杭州');
insert into test_company values(1006, '富shi康',sysdate,'深圳');
insert into test_company values(1007, '東風汽車',sysdate,'武漢');
commit;
--查詢分區記錄,表記錄,分區和表的條件查詢
select * from test_company partition(company_p01);
select * from test_company partition(company_p02);
select * from test_company partition(company_p03);
select * from test_company;
select * from test_company partition(company_p01) t where t.company_id = 1000;
select * from test_company t where t.company_id = 1000;
4) 複合分區實例
--創建一個話單表,並已組合分區(先範圍分區,再哈希分區的方式)
create table test_phone_order(
phone_order_id number(11) primary key,
phone_no varchar2(64) not null,
consumed_date date not null,
consumed_address varchar2(32)
)
partition by range(consumed_date)subpartition by hash(phone_order_id)
subpartitions 3 store in (xgtest_space01,xgtest_space02,xgtest_space03)(
partition phone_order_p01 values less than(to_date('2006-01-01','yyyy-mm-dd')),
partition phone_order_p02 values less than(to_date('2010-01-01','yyyy-mm-dd')),
partition phone_order_p03 values less than(maxvalue)
);
--查看錶信息和分區信息
select * from user_part_tables t where t.table_name = upper('test_phone_order');
select * from user_tab_partitions t where t.table_name = upper('test_phone_order');
--插入話單記錄
insert into test_phone_order values(1000, '13588879410',to_date('2001-06-23','yyyy-mm-dd'),'杭州');
insert into test_phone_order values(1001, '13588879411',to_date('2007-06-23','yyyy-mm-dd'),'南昌');
insert into test_phone_order values(1002, '13588879412',to_date('2004-06-20','yyyy-mm-dd'),'成都');
insert into test_phone_order values(1003, '13588879413',to_date('2025-10-09','yyyy-mm-dd'),'重慶');
insert into test_phone_order values(1004, '13588879414',to_date('2009-09-05','yyyy-mm-dd'),'北京');
insert into test_phone_order values(1005, '13588879415',to_date('2010-03-19','yyyy-mm-dd'),'南京');
insert into test_phone_order values(1006, '13588879416',to_date('2015-07-28','yyyy-mm-dd'),'蘇州');
commit;
--查看話單的分區記錄和表記錄,分區和表的條件查詢
select * from test_phone_order partition(phone_order_p01);
select * from test_phone_order partition(phone_order_p02);
select * from test_phone_order partition(phone_order_p03);
select * from test_phone_order;
select * from test_phone_order partition(phone_order_p01) t where t.phone_order_id = 1000;
select * from test_phone_order t where t.phone_order_id = 1000;
四)Oracle表分區對應用系統程序開發的影響和優化策略
1)JDBC的增刪改操作
增刪改這樣的信息一般可以直接在表上操作,而不必指定特定的分區,在實際應用中,增刪改的操作都是部分的,而且是小數據量的
2)JDBC的查詢操作
查詢操作,一般如果明確知道一個查詢範圍,我們可以指定到某個分區去查詢,這樣提高查詢效率,如果不知道在哪個分區則直接使用表查詢
3)JDBC應用策略
使用表分區主要是表的數據量非常大,一般應用中表的增刪改操作比較少,查詢次數多,而且花費時間長,故分區時,查詢操作按照分區去查詢更能提高查詢速度
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章