ORACLE中index的rebuild(轉)

來源:互聯網
上載者:User

標籤:des   blog   http   io   使用   ar   資料   div   sp   

Oracle裡大量刪除記錄後,表和索引裡佔用的資料區塊空間並沒有釋放。 
table move可以釋放已刪除記錄表佔用的資料區塊空間,整理片段。如果將表格用move方式整理片段後,索引將失效,這時需要將索引重建。 
重建索引可以釋放已刪除記錄索引佔用的資料區塊空間。重建索引不僅能增加索引資料表空間空閑空間大小,還能夠提高查詢效能。 

Sql代碼  
  1. --table move  
  2. alter table tbl move;   
  3.   
  4. --rebuild索引  
  5. alter index idx_tbl_col rebuild;  
  6. alter index idx_tbl_col rebuild online;  
  7.   
  8. --rename  
  9. ALTER INDEX employee_idx RENAME TO employee_index_Newname;  


普通情況下建立索引或者rebuild索引時,oracle會對基表加share鎖,由於share鎖和 row-X是不相容的,也就是說,在建立索引期間,無法對基表進行DML操作。 
是否加online,要看你的系統需求。因為不加online時rebuild會阻塞一切DML操作。 
當我們對索引進行rebuild時,如果不加online選項,oracle則直接讀取原索引的資料;當我們添加online選項時,oracle是直接掃描表中的資料 
索引在重建時,查詢仍然可以使用舊索引。實際上,oracle在rebuild時,在建立新索引過程中,並不會刪除舊索引,直到新索引rebuild成功。 
從這點可以知道rebuild比刪除索引然後重建索引的一個好處是不會影響原有的SQL查詢,但也正由於此,用rebuild方式建立索引需要相應資料表空間的空閑空間是刪除重建方式的2倍。 

可以通過如下的sql看到rebuild前後的索引空間大小 

Sql代碼  
  1. Select Segment_Name, Sum(bytes) / 1024 / 1024/1024 as Gsize  
  2. From User_Extents  
  3. Group By Segment_Name  
  4. order by Gsize desc  



不能直接rebuild整個分區索引 
對於非複合式索引,需要rebuild每個分區(partition),不能直接rebuild整個索引 
對於複合式索引,需要rebuild每個子分區(subpartition),不能直接rebuild整個索引,也不能直接rebuild分區 
使用下面的sql可以產生相應的rebuild語句,注意是否加上online 
非複合式索引: 

Sql代碼  
  1. select ‘alter index ‘ || index_owner || ‘.‘ ||index_name ||‘ rebuild partition ‘ || PARTITION_NAME || ‘ nologging parallel ;‘  
  2. from dba_ind_partitions  
  3. where index_owner = ‘USER_NAME‘  
  4. AND INDEX_NAME = ‘idx_tbl_col‘  



複合式索引: 

Sql代碼  
  1. select ‘alter index ‘ || index_owner || ‘.‘ ||index_name ||‘ rebuild subpartition ‘ || subpartition_name || ‘  parallel ;‘  
  2. from dba_ind_subpartitions where index_owner=‘&index_owner‘ and index_name=‘&index_name‘;  



參考rebuild 分區索引 

ORACLE中index的rebuild(轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.