Oracle批量rebuild所有索引的腳本


早期,項目的數據庫沒有搞分區表,rebuild索引的腳本很簡單:

begin
  for c1 in (select t.index_name, t.partitioned from user_indexes t where t.index_type<>'LOB' and t.index_type <> 'IOT - TOP')
  loop
    execute immediate 'alter index ' || c1.index_name || ' rebuild ';
  end loop;
end;

因數據量越來越大,近來把有些大表改為分區表,有些索引也改成了分區索引。但分區索引不能整個rebuild,必須一個分區一個分區地進行,因此腳本也隨之改變:

begin
  for c1 in (select t.index_name, t.partitioned from user_indexes t where t.index_type<>'LOB' and t.index_type <> 'IOT - TOP')
  loop
    if c1.partitioned='NO' then
      --對非分區索引直接rebuild
      execute immediate 'alter index ' || c1.index_name || ' rebuild';
    else
      --對分區索引,需對每個分區進行rebuild
      for c2 in (select partition_name from user_ind_partitions where index_name=c1.index_name)
      loop
        execute immediate 'alter index ' || c1.index_name || ' rebuild partition ' || c2.partition_name;
      end loop;
    end if;
  end loop;
end;

為提升性能,可進一步在rebuild時增加nologging、parallel 選項。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM