92、分區表、分區索引和全局索引部分總結

 

分區表、分區索引和全局索引:

  在一個表的數據超過過2000萬條或佔用2G空間時,建議建立分區表。

 

      create table ta(c1 int,c2 varchar2(16),c3 varchar2(64),c4 int constraint pk_ta primary key (c1)) partition by range(c1)(partition p1 values less than (10000000),partition p2 values less than (20000000),partition p3 values less than (30000000),partition p4 values less than (maxvalue));

  分區索引和全局索引:

  分區索引就是在所有每個區上單獨創建索引,它能自動維護,在drop或truncate某個分區時不影響該索引的其他分區索引的使用,也就是索引不會失效,維護起來比較方便,但是在查詢性能稍微有點影響。

 

      create index idx_ta_c2 on ta(c2) local (partition p1,partition p2,partition p3,partition p4); 或者 create index idx_ta_c2 on ta(c2) local ;

  另外在create unique index idx_ta_c2 on ta(c2) local ;系統會報ORA-14039錯誤,這是因爲ta表的分區列是c1,oracle不支持在分區表上創建PK主鍵時主鍵列不包含分區列,創建另外的約束 (unique)也不可以。

  全局索引就是在全表上創建索引,它可以創建自己的分區,可以和分區表的分區不一樣,也就是它是獨立的索引。在drop或truncate某個分區時需要創建索引alter index idx_xx rebuild,也可以alter table table_name drop partition partition_name update global indexes;實現,但是要花很長時間在重建索引上。可以通過查詢user_indexes、user_part_indexes和 user_ind_partitions視圖來查看索引是否有效。

 

      create index idx_ta_c3 on ta(c3);

  或者把全局索引分成多個區(注意和分區表的分區不一樣):

 

      create index idx_ta_c4 on ta(c4) global partition by range(c4)(partition ip1 values less than(10000),partition ip2 values less than(20000),partition ip3 values less than(maxvalue));

  注意索引上的引導列要和range後列一致,否則會有ORA-14038錯誤。

  oracle會對主鍵自動創建全局索引

  如果想在主鍵的列上創建分區索引,除非主鍵包括分區鍵,還有就是主鍵建在兩個或以上列上。

  在頻繁刪除表的分區且數據更新比較頻繁時爲了維護方便避免使用全局索引。

 

顯示Oracle查看分區表信息

顯示數據庫所有分區表的信息:DBA_PART_TABLES

顯示當前用戶可訪問的所有分區表信息:ALL_PART_TABLES

顯示當前用戶所有分區表的信息:USER_PART_TABLES

顯示Oracle查看分區表信息 顯示數據庫所有分區表的詳細分區信息:DBA_TAB_PARTITIONS

顯示當前用戶可訪問的所有分區表的詳細分區信息:ALL_TAB_PARTITIONS

顯示當前用戶所有分區表的詳細分區信息:USER_TAB_PARTITIONS

顯示子分區信息 顯示數據庫所有組合分區表的子分區信息:DBA_TAB_SUBPARTITIONS

顯示當前用戶可訪問的所有組合分區表的子分區信息:ALL_TAB_SUBPARTITIONS

顯示當前用戶所有組合分區表的子分區信息:USER_TAB_SUBPARTITIONS

顯示分區列 顯示數據庫所有分區表的分區列信息:DBA_PART_KEY_COLUMNS

顯示當前用戶可訪問的所有分區表的分區列信息:ALL_PART_KEY_COLUMNS

顯示當前用戶所有分區表的分區列信息:USER_PART_KEY_COLUMNS

顯示子分區列 顯示數據庫所有分區表的子分區列信息:DBA_SUBPART_KEY_COLUMNS

顯示當前用戶可訪問的所有分區表的子分區列信息:ALL_SUBPART_KEY_COLUMNS

顯示當前用戶所有分區表的子分區列信息:USER_SUBPART_KEY_COLUMNS

---------------------------------------------------------------------------------------------------

怎樣查詢出Oracle數據庫中所有的的分區表

select * from user_tables a where a.partitioned='YES'刪除一個表的數據是truncate table table_name;
刪除分區表一個分區的數據是alter table table_name truncate partition p5;

如果我要將分區表中各個分區的數據都清空,可以用truncate table table_name;嗎?
還是必須從頭執行

  1. alter table table_name truncate partition p1;   
  2. alter table table_name truncate partition p2;   
  3. alter table table_name truncate partition p3;   
  4. alter table table_name truncate partition p4;   
  5. alter table table_name truncate partition p5;   
  6. alter table table_name truncate partition p6;   

答:truncate table table_name

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章