oracle索引重构


select t.index_name,t.index_type,t.include_column,t.tablespace_name,t.table_name
  FROM USER_INDEXES T
 WHERE t.table_owner = 'SNCC'
   AND t.tablespace_name = 'SNCC';

查询用户SNCC且表空间名为SNCC的所有索引(索引名,索引类型,索引列,表空间名,表名)

法一:
alter index ... rebuild online的机制
alter index T_MY_COUNT_IN1 rebuild online;

当我们对索引进行rebuild时,如果不加online选项,oracle则直接读取原索引的数据;当我们添加online选项时,oracle是直接扫描表中的数据,那如何维护索引段数据的一致性呢?就是从引开始创建到索引创建完成这段时间的数据改变...

从索引开始rebuild online开始的那一刻起,oracle会先创建一个SYS_JOURNAL_xxx的系统临时日志表,结构类似于mlog$_表,通过内部触发器,记录了开始rebuild索引时表上所发生的改变的记录,当索引已经创建好之后,新数据将直接写入索引,只需要把SYS_JOURNAL_xxx日志表中的改变维护到索引中即可.

1。drop & create 的话,当前Table 无法使用该index ,可能会严重影响应用,只能在应用不需用到该 Index时使用 , 而 alter index .. rebuild online 则没有这个限制,只是会消耗DB资源,不至于严重影响应用

2。Drop & create 只需占用一份index的space , 不像alter index .. rebuild online ,会在创建过程中需要占用新老两个index的space, 在free space不足的环境中,也许只能选用Drop & create  

3. 至于 alter index .. rebuild ,或者alter index .. rebuild online 是否会使用 INDEX SCAN 代替 TABLE SCAN ,似乎不同环境有不同的结果。(RBO状态下)
我的一套环境中,alter index .. rebuild ==> INDEX FAST FULL SCAN  
alter index .. rebuild online==> TABLE FULL SCAN  
而另一套,则是alter index .. rebuild /alter index .. rebuild online 都采用了TABLE FULL SCAN  

例如:
alter index t_id rebuild online compute statistics; 

法二:
1.alter index xxx rebuild [online];
是否加online,要看你的系统需求。因为不加online时rebuild会阻塞一切DML操作。

2.rebuild不是“将索引删除然后再创建”。rebuild时不会为了排序去走fts,
而是遍历旧索引,然后在临时段中建立相应结构,完了后移到新索引中。

“将索引删除然后再创建”,是最不好的方法。 


法三:  生产执行的SQLl (from:http://blog.csdn.net/sscsgss/article/details/4468580)

create or replace procedure p_rebuild_all_index
   (tablespace_name in varchar2,--这里是表空间名,如果不改变表空间,可以传入null
   only_unusable in boolean)    --是否仅对无效的索引操作
as
   sqlt varchar(200);
begin
    --只取非临时索引
    for idx in (select index_name, tablespace_name, status from user_indexes where temporary = 'N') loop
        --如果是如重建无效的索引,且当索引不是无效时,则跳过
        if only_unusable = true and idx.status <> 'UNUSABLE' then
           goto continue;
        end if;
        if (tablespace_name is null) or idx.status = 'UNUSABLE' then
           --如果没有指定表空间,或索引无效,则在原表空间重建
           sqlt := 'alter index ' || idx.index_name || ' rebuild ';
        elsif upper(tablespace_name) <> idx.tablespace_name then
           --如果指定的不同的表空间,则在指定表空间待建索引
           sqlt := 'alter index ' || idx.index_name || ' rebuild tablespace ' || tablespace_name;
        else
           --如果表空间相同,则跳过
           goto continue;
        end if;
        dbms_output.put_line(idx.index_name);
        EXECUTE IMMEDIATE sqlt;
        <<continue>>
        null;
     end loop;
end;
/*
功能:重建索引。
说明:如果表空间参数传入null,则在原表空间内重建索引,否则在目标表空间重建索引。
      如果表空间相同,则跳过。
      only_unusable表示是否只对无效的索引进行重建
 2007年6月26日
*/





  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值