一:考虑重建索引的场合
1:表上频繁发生update,delete操作
2:表上发生了alter table ..move操作(move操作导致了rowid变化)
二:判断重建索引的标准
1,
Analyze index indexname validate structure;
2, 在执行步骤1的session中查询index_stats表,不要到别的session去查询
3, 在步骤2查询出来的height>=4或者DEL_LF_ROWS/LF_ROWS>0.2的场合,该索引考虑重建;
Example:
----------
SQL> analyze index pk_t_test validate structure;
Index analyzed
---------- -------------------
SQL> delete from test_index where rownum<250000;
---------- -------------------
SQL> select height,DEL_LF_ROWS/LF_ROWS from index_stats;
---------- -------------------
三:重建索引的方式
四:alter index rebuid内部过程和注意点
(1)
Rebuild以index fast full scan(or table full scan)方式读取原索引中的数据来构建一个新的索引,有排序的操作; rebuild online 执行表扫描获取数据,有排序的操作;
Rebuild
Eg1:
SQL> explain plan for alter index idx_policy_id2 rebuild;
Explained
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
---------------------------------------------------------------------
| Id
---------------------------------------------------------------------
|
|
|
|
---------------------------------------------------------------------
Eg2:
SQL>
Explained
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
---------------------------------------------------------------------
| Id
---------------------------------------------------------------------
|
|
|
|
Eg3: (注意和Eg1比较)
Rebuil online 方式:
SQL> explain plan for alter index idx_policy_id2 rebuild online;
Explained
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
---------------------------------------------------------------------
| Id
---------------------------------------------------------------------|
|
|
|
(2)
(3)
注意点:
1,
2,
3,
五:重建分区表上的分区索引
六:索引状态描述
在数据字典中查看索引状态,发现有三种:
valid:当前索引有效
N/A :分区索引有效
unusable:索引失效
七:术语
Select index_name,blevel from dba_indexes where blevel>=4.
SQL>anlyze index ------ validate structure
SQL>select (del_lf_rows_len/lf_rows_len)*100 from index_stats where name=’------‘
就能看到是否这个索引被删除的百分比。
上面只是判断,那么,怎样重建会更好呢?
建索引的办法:
a.
b.
c.
下面讨论一下这三种方法的优缺点:
1).删除并从头开始建索引:方法是最慢的,最耗时的。一般不建议。
2).Alter index ---- rebuild 快速重建索引的一种有效的办法,因为使用现有索引项来重建新索引,如果客户操作时有其他用户在对这个表操作,尽量使用带online参数来最大限度的减少索引重建时将会出现的任何加锁问题,alter index ------- rebuild online.但是,由于新旧索引在建立时同时存在,因此,使用这种技巧则需要有额外的磁盘空间可临时使用,当索引建完后把老索引删除,如果没有成功,也不会影响原来的索引。利用这种办法可以用来将一个索引以到新的表空间。
Alter index ------ rebuild
3).alter index ----- coalesce 使用带有coalesce参数时重建期间不需要额外空间,它只是在重建索引时将处于同一个索引分支内的叶块拼合起来,这最大限度的减少了与查询过程中相关的潜在的加锁问题,但是,coalesce选项不能用来讲一个索引转移到其他表空间。
八:其他
总结:
1、判断是否需要重建索引:
SQL>analyze index index_name validate structure;
SQL> select height,DEL_LF_ROWS/LF_ROWS from index_stats;
( 或 Select index_name,blevel from dba_indexes where blevel>=4 );
说明 : 当查询出来的 height>=4 或者 DEL_LF_ROWS/LF_ROWS>0.2 的场合 , 该索引考虑重建 ;
2 、重建索引方法 :
方法一、
Alter index index_name rebuild tablespace tablespace_name;
优点:是快速重建索引的一种有效的办法,可以用来将一个索引移到新的表空间。
缺点:重建期间需要额外空间。
方法二、
alter index index_name coalesce;
优点:重建期间不需要额外空间。
缺点:coalesce选项不能用来将一个索引转移到其他表空间。