日期:2014-05-16  浏览次数:20416 次

计算索引碎片的一个脚本

???? 今天在网上看到了一个估计索引碎片的方法,所以写了个小过程,对用户下的所有索引进行一次计算,挑选二元高度大于4的或者碎片率大于10%的索引进行输出。 需要说明的是,这种估计索引碎片的方法来自网上,还没有查询官方文档上的相关部分,仅供参考,我不对分析出的结果负责。
????? 我在一些OCP的教材上看到了有关analyze validate的说明,据称可以分析出碎片数,但是现在还没在官方文档上找到确切的证据,希望知道的人给我讲一下,以下是我从官方文档上找到的一些关于analyze validate的说明:?
????? For an index, Oracle Database verifies the integrity of each data block in the index and checks for block corruption. This clause does not confirm that each row in the table has an index entry or that each index entry points to a row in the table. You can perform these operations by validating the structure of the table with the CASCADE clause.
????? Oracle 验证每一个索引的数据块完整性以及检查block corruption。

declare
cursor cur_ind is
    select ui.index_name from user_indexes ui;
  ind_name  user_indexes.index_name%type;
  v_name    index_stats.name%type;
  v_height  index_stats.height%type;
  v_percent number;
begin
  dbms_output.enable(10000000000);
  open cur_ind;
  loop
    fetch cur_ind
      into ind_name;
    exit when cur_ind%notfound;
    execute immediate 'analyze index ' || ind_name || ' validate structure'; --分析每个索引
    select ist.name,
           ist.height,
           round((del_lf_rows / (lf_rows + 0.0000000001)) * 100)
      into v_name, v_height, v_percent
      from index_stats ist;
    if (v_height > 4 or v_percent > 10) then
      dbms_output.put_line('索引名:' || v_name || ', ' || '高度:' || v_height || ', ' ||
                           '百分比:' || v_percent || ', ' || '需要重建');
    end if;
  end loop;
  close cur_ind;
end;

?