Oracle使用hash分區優化分析函數查詢
在ORACLE中的分析函數都是基于某幾個字段劃分計算窗口,然后在窗口內進行聚合,排名,等等計算。我想如果我們數據表的hash分區字段與分析函數中的partition by 字段一致的時候,應該可以大大加快分析函數的運行效率。因為每個分區上的數據可以單獨進行運算。互不干涉,下面試驗來驗證我的想法.
***步:創建一個分區表和普通表,表結構與DBA_OBJECTS一致:
create table t_partition_hash(
object_name varchar2(128),
subobject_name varchar2(30),
object_id number,
data_object_id number,
object_type varchar2(19),
created date,
last_ddl_time date,
timestamp varchar2(19),
status varchar2(7),
temporary varchar2(1),
generated varchar2(1),
secondary varchar2(1)
)
partition by hash(object_type)(
partition t_hash_p1 tablespace USERS,
partition t_hash_p2 tablespace USERS,
partition t_hash_p3 tablespace USERS,
partition t_hash_p4 tablespace USERS,
partition t_hash_p5 tablespace USERS,
partition t_hash_p6 tablespace USERS,
partition t_hash_p7 tablespace USERS,
partition t_hash_p8 tablespace USERS
);create table t_big_hash(
object_name varchar2(128),
subobject_name varchar2(30),
object_id number,
data_object_id number,
object_type varchar2(19),
created date,
last_ddl_time date,
timestamp varchar2(19),
status varchar2(7),
temporary varchar2(1),
generated varchar2(1),
secondary varchar2(1)
);
第二步:準備數據,從dba_object中把數據插入到兩個表。總共插入數據1610880。
insert into t_partition_hash select * from dba_objects; |
第三步:本采用RANK函數對兩個表進行查詢。
begin |
使用hash分區表總共執行5次的運行時間分別為:46.156s,33.39s,40.516s 34.875s 38.938s.
begin |
由此可見采用有效的HASH分區表可以有效提升分析函數在oracle中的執行效率。我相信隨著數據量的增加,將會有更明顯的效果,回頭再測試一個項目中遇到的類似問題。
【編輯推薦】